//windows 中使用反斜杠:“\”,linux 中使用正斜杠:“/”
//File file=new File("d:/ioPractice/text.txt");
File file=new File("d:"+
File.separator+"ioPractice"+
File.separator+"text.txt");
//文件不存在创建
if(!file.exists()){
try {
new FileWriter(asnTxtPath);//创建po.txt
} catch (IOException e) {
e.printStackTrace();
}
}
Writer fw=new FileWriter(file,true);
BufferedWriter bw=new BufferedWriter(fw);
String str="这是第1行\r\n这是第2行"; //\r\n换行
bw.write(str);
bw.flush();
bw.close();
fw.close();
//判断文件夹是存在
File dir=new File("d:\\test_dir");
if (file.exists()) {
if (file.isDirectory()) {
System.out.println("dir exists是文件夹");
}
} else {
//文件夹不存在,创建
file.mkdir();
}
//判断文件是存在
File file = new File("d:\\test_file.txt");
if (!file.exists()){
try {
//文件不存在,创建
file.createNewFile();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}