发现了个问题:
2023/3/2 Windows 10操作系统中
利用FileOutputStream()创建文件并且添加内容后,里面的\n是可以被Windows自带的文本文档识别的,也就是说存入的只是一个\n(转行)字符,如下代码:
FileOutputStream fox = new FileOutputStream("File_and_IO\\fileOut04.txt");
byte[] bytes = "Hello\n".getBytes();
byte[] bytes1 = "Learn\n".getBytes();
fox.write(bytes);
fox.write(bytes1);
fox.close();
之后通过FileInputStream()去读取该程序时,若我们使用byte字符数组输出,识别到的也只是一个\n
FileInputStream fis = new FileInputStream("File_and_IO\\fileOut04.txt");
byte[] bytes = new byte[5];
int by = fis.read(bytes);
System.out.println(by);
System.out.println(new String(bytes));
by = fis.read(bytes);
System.out.println(by);
System.out.println(new String(bytes));
如果我们直接新建txt文件,内容也是hello换行learn,则通过上述代码读取该文件,它会多识别出\r\n
操作系统中识别换行符:
windows:\r\n
linux:\n
mac:\r
总结:FileOutputStream在写入数据时:\n也能被识别(通过系统记事本打开它也是换行的);而我们如果在记事本中输入后直接换行,在FIleInputStream中读取的时候会读取\r\n;所以在我们写程序的时候需要注意,假如你txt写入的数据是通过FileOutputStream写的,换行只是一个字符。如果用记事本写的,换行会是两个字符