Home FileOutputStream类的换行写入和追加写入
Post
Cancel

FileOutputStream类的换行写入和追加写入

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
import java.io.FileOutputStream;  
  
public class FileOutputStreamDemo {  
    public static void main(String[] args) throws Exception {  
        // 创建文件输出流对象  
        FileOutputStream fos1 = new FileOutputStream("fos1.txt");  
        // 第二个参数为true表示程序每次运行都是追加字符串在原有的字符上
        FileOutputStream fos2 = new FileOutputStream("fos2.txt", true);   
  
        // 写数据  
        for (int x = 0; x < 10; x++) {  
            fos1.write(("hello" + x).getBytes());  
            fos1.write("\r\n".getBytes());// 写入一个换行  
        }  
        for (int x = 0; x < 10; x++) {  
            fos2.write(("hello" + x).getBytes());  
            fos2.write("\r\n".getBytes());// 写入一个换行  
        }

        // 释放资源  
        fos1.close();  
        fos2.close();  
    }  
}  
This post is licensed under CC BY 4.0 by the author.