正常写入
方法一
String info = "12345678";
String path = "C:\\Users\\admin\\Desktop\\";
try {
FileOutputStream fileOutputStream = new FileOutputStream(path + "123.txt");
byte[] bytes = info.getBytes();
for (int i = 0; i < bytes.length; i++) {
try {
fileOutputStream.write(bytes[i]);
System.out.println(bytes[i]);
} catch (IOException e) {
e.printStackTrace();
}
}
fileOutputStream.close();
} catch (Exception e){
e.printStackTrace();
}
方法二
String info = "12345678";
String path = "C:\\Users\\admin\\Desktop\\";
try {
FileOutputStream fileOutputStream = new FileOutputStream(path + "123.txt");
byte[] bytes = info.getBytes();
fileOutputStream.write(info.getBytes());
fileOutputStream.close();
} catch (Exception e){
e.printStackTrace();
}
追加写入
创建FileOutputStream对象时末尾加上ture
FileOutputStream fileOutputStream = new FileOutputStream(path + "123.txt",true);
写入换行
fileOutputStream.write("\r\n".getBytes());