字节流 字符流

2021-07-30  本文已影响0人  啷里个啷里个啷个里个啷

FileInputStream
FileOutputStream

public void copyWithSteam(String srcPath,String destPath){
    System.out.println("Steam");
    FileInputStream fis = null;
    FileOutputStream fos = null;
    try {
        File srcFile = new File(srcPath);
        File destFile = new File(destPath);

        fis = new FileInputStream(srcFile);
        fos = new FileOutputStream(destFile);

        byte[] buffer = new byte[8192];
        int len;
        while((len = fis.read(buffer)) != -1){

            fos.write(buffer,0,len);
        }
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (fis != null){
            try {
                fis.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

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

上述为字节流,字符流和字节流写法一致。
字符流使用
FileReader
FileWriter 
 


上一篇下一篇

猜你喜欢

热点阅读