Java基础-FileOutputStream字节输出流保存文件方式

package com.hspedu.io_;

import org.junit.Test;

import java.io.FileOutputStream;
import java.io.IOException;

public class TestFileOutputStream {
    @Test
    public void writeFile() {
        String filePath = "e:\\JavaIO\\FileOutputStream\\test.txt";
        FileOutputStream fileOutputStream = null;

        try {
            fileOutputStream = new FileOutputStream(filePath);

            // fileOutputStream.write();方法写入文件
            String string = "hello, world";
//            fileOutputStream.write(string.getBytes());
            fileOutputStream.write(string.getBytes(), 0, string.length());

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (fileOutputStream != null) {
                    fileOutputStream.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

 

posted @ 2022-04-14 15:27  柯南同学  阅读(1185)  评论(0编辑  收藏  举报