链接:https://www.iteye.com/blog/nbh219-1865476
(侵删)
/**
* @param args
*/
public static void main(String s[]) {
try {
String url = "F:\\gsm\\";
String FileName = "info.dat";
File file = new File(url);
file.mkdirs();//创建文件夹
File fil = new File(url);
fil.createNewFile();
FileWriter fileWriter=new FileWriter(url+FileName);
fileWriter.write("gsmc");//写入文件
fileWriter.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
方法:
public void writeFile(String result) {
try {
String fileName = "C:/data/Data.dat";
File file = new File(fileName);
File fileParent = file.getParentFile();
if(!fileParent.exists()){
fileParent.mkdirs();
}
file.createNewFile();
// false:覆盖之前写的 如果是true则为追加
BufferedWriter writer = new BufferedWriter (new OutputStreamWriter (new FileOutputStream (file,false),"UTF-8"));
writer.write(result);
writer.flush();
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}