io

【Java】【IO流】FileOutputStream

2017-04-08  本文已影响72人  JerichoPH

IO流(FileOutputStream)

  1. 写入文件
public class Demo1_IO {

    public static void main(String[] args) throws Exception {
        FileOutputStream fos = new FileOutputStream("aaa.txt"); // 如果aaa.txt不存在则创建一个
        fos.write(97); // 写出的是int类型,文件中保存的是通过字码表中对应的字符
        fos.write(98);
        fos.write(99);
        fos.close();
        // 每次打开文件则会将文件重写
    }
}
  1. 追加写入
public class Demo1_File {

    public static void main(String[] args) throws IOException {
        FileOutputStream fos = new FileOutputStream("aaa.txt", true); // 将第二个参数填写为true则在原有文件中追加内容
        fos.write(97); // 写出的是int类型,文件中保存的是通过字码表中对应的字符
        fos.write(98);
        fos.write(99);
        fos.close();
    }
}
  1. 复制文件
public class Demo1_File {

    public static void main(String[] args) throws IOException {
        FileInputStream fis = new FileInputStream("aaa.txt");
        FileOutputStream fos = new FileOutputStream("bbb.txt");
        int b;
        while ((b = fis.read()) != -1) {
            fos.write(b);
        }
        fis.close();
        fos.close();
    }
}
  1. 一次性拷贝文件(消耗内存大)==当文件过大时不宜使用==
public class Demo1_File {

    public static void main(String[] args) throws IOException {
        FileInputStream fis = new FileInputStream("aaa.txt");
        FileOutputStream fos = new FileOutputStream("bbb.txt");
        byte[] arr = new byte[fis.available()];
        fis.read(arr);
        fos.write(arr);
    
        fis.close();
        fos.close();
    }
}
  1. 分步拷贝文件(适合拷贝大文件)
public class Demo1_File {

    public static void main(String[] args) throws IOException {
        FileInputStream fis = new FileInputStream("aaa.txt");
        FileOutputStream fos = new FileOutputStream("bbb.txt");
    
        // 读取步长
        int size = (int) new File("aaa.txt").length();
        byte[] arr = new byte[size];
    
        int len;
        while ((len = fis.read(arr)) != -1) {
            fos.write(arr);
        }
    
        fis.close();
        fos.close();
    }
}
  1. BufferedInputStream和BufferedOutputStream
public class Demo1_File {

    public static void main(String[] args) throws IOException {
        File sourceFile = new File("aaa.txt");
        if (!sourceFile.isFile()) {
            throw new IOException("拷贝源必须是文件");
        }
    
        BufferedInputStream bis = new BufferedInputStream(new FileInputStream(sourceFile.getAbsoluteFile()));
        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("bbb.txt"));
    
        int b;
        while ((b = bis.read()) != -1) {
            bos.write(b);
            bos.flush(); // 实时刷新
        }
    
        bis.close();
        bos.close();
    }
}
  1. try finally处理流 流程
File sourceFile = new File("aaa.txt");
    
// Java 1.7版本
try (BufferedInputStream bis = new BufferedInputStream(new FileInputStream(sourceFile.getAbsoluteFile()));
        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("bbb.t"));) {
    int b;
    while ((b = bis.read()) != -1) {
        bos.write(b);
        bos.flush(); // 实时刷新
    }
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(source.getAbsoluteFile()));
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(target));

// Java 1.6版本写法
try {
    int b;
    while ((b = bis.read()) != -1) {
        bos.write(b);
        bos.flush(); // 实时刷新
    }
} finally {
    try {
        if (bis != null) {
            bis.close();
        }
    } finally {
        if (bos != null) {
            bos.close();
        }
    }
}
上一篇 下一篇

猜你喜欢

热点阅读