Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

create a text file in a folder

Tags:

java

file

I want to create a text file into that folder that I am creating here.

File dir = new File("crawl_html");  
dir.mkdir(); 
String hash = MD5Util.md5Hex(url1.toString());
System.out.println("hash:-"  + hash);
File file = new File(""+dir+"\""+hash+".txt");

But this code doesn't create the text file into that folder..Instead it makes the text file outside that folder..

like image 681
arsenal Avatar asked Jul 11 '11 21:07

arsenal


People also ask

How do I save a .TXT file in Windows?

Open the workbook you want to save. Click File > Save As.

How do I create a list of files in a folder?

Press and hold the SHIFT key and then right-click the folder that contains the files you need listed. Click Open command window here on the new menu. A new window with white text on a black background should appear. o To the left of the blinking cursor you will see the folder path you selected in the previous step.


1 Answers

One of java.io.File's constructors takes a parent directory. You can do this instead:

final File parentDir = new File("crawl_html");
parentDir.mkdir();
final String hash = "abc";
final String fileName = hash + ".txt";
final File file = new File(parentDir, fileName);
file.createNewFile(); // Creates file crawl_html/abc.txt
like image 112
hoipolloi Avatar answered Nov 09 '22 12:11

hoipolloi