java 字符串写入文件
FileOutputStream
String str="hello world!";
String filePath = "\\test\\123.json";
File json = new File(filePath);
if (!json.exists())
json.createNewFile();
byte[] strBytes = str.getBytes(StandardCharsets.UTF_8);
FileOutputStream fos = new FileOutputStream(json);
fos.write(strBytes , 0, strBytes.length);
fos.close();
FileWriter
不追加,不换行
String str="hello world!";
FileWriter fw = new FileWriter(file);
fw.write(str);
fw.flush();
fw.close();
追加内容并换行
String str="hello world!";
FileWriter fw = new FileWriter(file,true);
fw.write(str+System.getProperty("line.separator"));
fw.flush();
fw.close();