我想在我在这里创建的文件夹中创建一个文本文件.
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");
Run Code Online (Sandbox Code Playgroud)
但是这段代码不会将文本文件创建到该文件夹中.相反,它会使文本文件位于该文件夹之外.
你需要的是什么
File file = new File(dir, hash + ".txt");
Run Code Online (Sandbox Code Playgroud)
这里的关键是File(File parent, String child)构造函数.它在提供的父目录下创建一个具有指定名称的文件(当然,前提是该目录存在).
java.io.File的构造函数之一采用父目录.你可以这样做:
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
Run Code Online (Sandbox Code Playgroud)