注:回车换行
windows中:"\r\n"
linux:"\n"
1.字节输出流
FileOutputStream fos=new FileOutputStream("charstream\\a.txt");
fos.write(97);
fos.write("\r\n".getBytes());
fos.write(98);
2.字符输出流
Scanner sc=new Scanner(System.in);
System.out.println("请输入用户名:");
String username=sc.next();
System.out.println("请输入密码:");
String password=sc.next();
FileWriter fw=new FileWriter(new File("charstream\\a.txt"));
fw.write(username);
fw.write("\n");
fw.write(password);
fw.flush();
fw.close();
3. .newLine();跨平台回车换行


这篇博客介绍了在Windows和Linux操作系统中使用字节输出流和字符输出流进行文件写入的差异,包括回车换行的表示方式。示例代码展示了如何使用FileOutputStream和FileWriter在文件中写入字符,并在不同系统下处理换行符。
通过字节流 fos.write(
.getBytes()) 和字符流 fw.write(
) 实现跨平台的换行操作,确保文件内容在不同系统间的一致性。
1057

被折叠的 条评论
为什么被折叠?



