1。字节流
import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; public class FileOut2 { public static void main(String[] args) throws IOException { File file = new File("xx.txt"); FileInputStream in = new FileInputStream(file); BufferedInputStream bif = new BufferedInputStream(in); int a = 0; byte[] c= new byte[10]; // 字节流使用byte FileOutputStream out = new FileOutputStream("yy.txt"); BufferedOutputStream bof = new BufferedOutputStream(out); while ((a = bif.read(c,0,10)) != -1){ bof.write(c); } bof.flush(); in.close(); out.close(); } }
2。字符流
import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; public class FileOut { public static void main(String[] args) throws IOException { File file = new File("xx.txt"); FileReader in = new FileReader(file); BufferedReader bif = new BufferedReader(in); //缓冲输入流, int a = 0; char[] c= new char[10]; //字符流使用char FileWriter out = new FileWriter("yy.txt"); BufferedWriter bof = new BufferedWriter(out); while ((a = bif.read(c,0,10)) != -1){ bof.write(c); } bof.flush(); in.close(); out.close(); } }
两者都使用了缓冲,流程如下:
读取的文件 -- 输入流 -- 缓冲输入流 -- 内存 -- 缓冲输出流 -- 输出流 -- 写入的文件