在文件夹中创建一个文本文件 [英] create a text file in a folder

查看:201
本文介绍了在文件夹中创建一个文本文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在此处创建的文件夹中创建一个文本文件.

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..

推荐答案

java.io.File的构造函数之一带有一个父目录.您可以改为:

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

这篇关于在文件夹中创建一个文本文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆