要想换行写,只需在两个 write() 方法之间再次写出一个换行符就可以了
Windows:\r\n
Linux:\n
Mac:\r
细节:在 Windows 操作系统当中,Java 对回车换行进行了优化,虽然完整的是 \r\n,但是我们写其中一个 \r 或者 \n,Java 也能实现换行,因为 Java 在底层会补全
换行代码:
String str = "\r\n";
byte[] bytes = str.getBytes();
fos.write(bytes);
续写:续写功能打开后,创建对象时,文件就不会清空了。续写也叫追加写入
步骤:在创建对象的第二个参数传入 true,代表打开续写(默认是 false,表示关闭续写,此时创建对象会清空文件)
FileOutputStream fos = new FileOutputStream("E:\\Java基础资料\\a.txt", true);