您的位置:首页 > 编程语言 > Java开发

文件创建 file txt java

2016-04-19 16:57 585 查看
创建文件夹

fPath="F:"+File.separator+"tomcat6work"+File.separator+"nihao1";

public static void getFileExists(String fPath) {

File file = new File(fPath);
// 如果文件夹不存在则创建
if (!file.exists() && !file.isDirectory()) {

file.mkdir();

}

//文件写入txt

public static void createTxt(String filePath, String content, boolean b) {
String str = new String(); // 原有txt内容
String s1 = new String();// 内容更新
try {
File f = new File(filePath);
if (!f.exists()) {
f.createNewFile();// 不存在则创建
}
if (false == b) {
BufferedReader input = new BufferedReader(new FileReader(f));
while ((str = input.readLine()) != null) {
s1 += str + "\n";
}
System.out.println(s1);
input.close();
}
s1 += content;

BufferedWriter output = new BufferedWriter(new FileWriter(f));
output.write(s1);
output.close();
} catch (Exception e) {
e.printStackTrace();
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  file txt java