0
点赞
收藏
分享

微信扫一扫

FileInputStream与FileOutputStream

敬亭阁主 2022-02-07 阅读 52

 

FileReader与FileWriter 只能用于文本文件,如.txt,.java,.c,.c++等

对于图片,视频等字节流文件需要用字节流即:FileInputStream与FileOutputStream 

package IOStream;

import org.junit.Test;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

public class InputOutputStream {
    @Test
    public void test() throws IOException {

        FileInputStream fis = null;
        FileOutputStream fos = null;
        try {
            File srcFile = new File("hanyang.png");
            File destFile = new File("hanyang1.png");

            fis = new FileInputStream(srcFile);
            fos = new FileOutputStream("hanyang1.png");

            byte[] buffer = new byte[5];
            int len ;
            while((len = fis.read(buffer))!= -1){
                fos.write(buffer,0,len);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                fos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                fis.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }




    }
}
举报

相关推荐

0 条评论