Java基础知识强化之IO流笔记20:FileOutputStream写出数据实现换行和追加写入

1.  如何实现数据的换行

(1)

 1 package com.himi.fileoutputstream;
 2 
 3 import java.io.FileNotFoundException;
 4 import java.io.FileOutputStream;
 5 import java.io.IOException;
 6 
 7 
 8 /**
 9  * 
10  *     如何实现数据换行?
11  *
12  */
13 
14 
15 public class FileOutputStreamDemo4 {
16 
17     public static void main(String[] args) throws IOException {
18         //创建字节输出流对象
19         FileOutputStream fos = new FileOutputStream("fos3.txt");
20         //写入数据
21         for(int i=0; i<10; i++) {
22             fos.write(("Love"+i).getBytes());
23         }
24             
25         //释放资源
26         fos.close();
27         
28         
29     }
30 
31 }

运行效果如下:我们这里没有换行,不是我们想要的。

这里没有换行是因为你写入数据的时候,没有写入换行符,写入换行符即可。

(2)如何写入换行符?,如下:

 1 import java.io.FileOutputStream;
 2 import java.io.IOException;
 3 
 4 
 5 /**
 6  * 
 7  *     如何实现数据换行?
 8  *        这里没有换行是因为你写入数据的时候,没有写入换行符,写入换行符即可。    
 9  *
10  *不同系统的换行符不一样:
11  *        windows:

12  *        linux:

13  *        Mac:
14  *  而常见的高级记事本软件是可以识别任意换行符的(比如Eclipse自带记事本软件)
15  */
16 
17 
18 public class FileOutputStreamDemo4 {
19 
20     public static void main(String[] args) throws IOException {
21         //创建字节输出流对象
22         FileOutputStream fos = new FileOutputStream("fos3.txt");
23         //写入数据
24         for(int i=0; i<10; i++) {
25             fos.write(("Love"+i).getBytes());
26             fos.write("
".getBytes());
27         }
28             
29         //释放资源
30         fos.close();
31         
32         
33     }
34 
35 }

运行效果如下:

追溯到工程目录下,如下:

使用Notepad++打开,如下:

使用windows自带记事本打开,如下:

(3)修改代码如下:

 1 package com.himi.fileoutputstream;
 2 
 3 import java.io.FileOutputStream;
 4 import java.io.IOException;
 5 
 6 
 7 /**
 8  * 
 9  *     如何实现数据换行?
10  *        这里没有换行是因为你写入数据的时候,没有写入换行符,写入换行符即可。    
11  *
12  *不同系统的换行符不一样:
13  *        windows:

14  *        linux:

15  *        Mac:
16  *  而常见的高级记事本软件是可以识别任意换行符的(比如Eclipse自带记事本软件)
17  */
18 
19 
20 public class FileOutputStreamDemo4 {
21 
22     public static void main(String[] args) throws IOException {
23         //创建字节输出流对象
24         FileOutputStream fos = new FileOutputStream("fos3.txt");
25         //写入数据
26         for(int i=0; i<10; i++) {
27             fos.write(("Love"+i).getBytes());
28             fos.write("
".getBytes());
29         }
30             
31         //释放资源
32         fos.close();
33         
34         
35     }
36 
37 }

运行之后发现,上面所有的记事本软件都换行了,包括windows自带的记事本软件,如下:

2. 如何实现数据的追加

使用构造方法:FileOutputStream(String name, boolean append)

 参数append为true,表示追加;反之false,为不追加

代码如下:

 1 package com.himi.fileoutputstream;
 2 
 3 import java.io.FileOutputStream;
 4 import java.io.IOException;
 5 
 6 
 7 /**
 8  *  如何实现数据的追加写入?
 9  *    使用构造方法:FileOutputStream(String name, boolean append)
10  *    参数append为true,表示追加;反之false为不追加
11  */
12 
13 
14 public class FileOutputStreamDemo5 {
15 
16     public static void main(String[] args) throws IOException {
17         //创建字节输出流对象
18         //FileOutputStream fos = new FileOutputStream("fos3.txt");
19         
20         //FileOutputStream(String name, boolean append)
21         FileOutputStream fos = new FileOutputStream("fos3.txt",true);
22         //写入数据
23         for(int i=0; i<10; i++) {
24             fos.write(("Love"+i).getBytes());
25             fos.write("
".getBytes());
26         }
27             
28         //释放资源
29         fos.close();
30         
31         
32     }
33 
34 }

运行效果如下:

原文地址:https://www.cnblogs.com/hebao0514/p/4850773.html