首页 > 技术文章 > 文件处理(如果文件存在则追加,不存在则生成多级文件夹以及txt目录)

wth21-1314 2018-10-16 13:07 原文

public static void writeFile(String path,String fileName,String content) throws IOException {
File file = new File(path);
if(!file.exists()){
try {
boolean mkdirs = file.mkdirs();//按照该目录创建文件夹
if(mkdirs==true){
File filetxt = new File(fileName);
filetxt.createNewFile();//创建文件
}

} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
FileWriter writer = null;
try {
writer = new FileWriter(path+"/"+fileName, true);
writer.write(content);
} catch (IOException e) {
e.printStackTrace();
} finally {
if (writer != null) {
writer.close();
}
}
}
public static void main(String[] args){
try {
writeFile("D://wth/my","tu.txt","i love you");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

推荐阅读