通过IO流拷贝文件的几种方式如下:
需求1: 拷贝纯文本文件.
FileInputStream && FileOutputStream
- 普通的字节流一次读写一个字节.
import java.io.FileInputStream; import java.io.FileOutputStream; public class Demo1 { public static void main(String[] args) throws Exception { //创建字节输入流对象,关联数据源文件 FileInputStream fileInputStream = new FileInputStream("src\\input.txt");//不写绝对地址则默认项目目录下的地址 //创建字节输出流对象,关联目的地文件 //补充:new FileOutputStream("input.txt",true),从文本末尾开始写入数据,不会覆盖原本内容,不写true则默认为false,会从文本头开始写入,覆盖原本内容 FileOutputStream fileOutputStream = new FileOutputStream("src\\output.txt");//不写绝对地址则默认项目目录下的地址 int len;//用于接收读取的字符 //fileInputStream.read返回类型为int型,如果没有数据则返回-1 //循环读取, 只要条件满足, 就一直读, 并将读取到的数据赋值给变量. while ((len = fileInputStream.read()) != -1) { // 将读取到的数据写入到目的地文件中. fileOutputStream.write(len); } //关流,释放资源 fileInputStream.close(); fileOutputStream.close(); } }
- 普通的字节流一次读写一个字节数组. //掌握
import java.io.FileInputStream; import java.io.FileOutputStream; public class Demo2 { public static void main(String[] args) throws Exception { //创建字节输入流对象,关联数据源文件 FileInputStream fileInputStream = new FileInputStream("src\\input.txt");//不写绝对地址则默认项目目录下的地址 //创建字节输出流对象,关联目的地文件 FileOutputStream fileOutputStream = new FileOutputStream("src\\output.txt");//不写绝对地址则默认项目目录下的地址 byte[] bytes = new byte[8192]; int len; while ((len = fileInputStream.read(bytes)) != -1) { fileOutputStream.write(bytes, 0, len); } fileInputStream.close(); fileOutputStream.close(); } }
BufferedInputStream && BufferedOutputStream
- 高效的字节流一次读写一个字节. //掌握
public class Demo3 { public static void main(String[] args) throws Exception{ //创建输入流, 关联数据源文件 BufferedInputStream bufferedInputStream = new BufferedInputStream(new FileInputStream("src\\input.txt"));//BufferedInputStream(InputStream ) //创建输出流, 关联目的地文件 //补充:new FileOutputStream("input.txt",true),从文本末尾开始写入数据,不会覆盖原本内容,不写true则默认为false,会从文本头开始写入,覆盖原本内容 BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(new FileOutputStream("src\\output.txt"));//BufferedOutputStream(OutputStream) //定义变量, 用来记录读取到的数据(字节). int len; //循环读取, 只要条件满足, 就一直读, 并将读取到的数据赋值给变量. while((len = bufferedInputStream.read())!=-1){ //将读取到的数据写入到目的地文件中. bufferedOutputStream.write(len); } //关闭流,释放资源 bufferedInputStream.close(); bufferedOutputStream.close(); }
- 高效的字节流一次读写一个字节数组.
import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.FileInputStream; import java.io.FileOutputStream; public class Demo4 { public static void main(String[] args) throws Exception { //创建输入流, 关联数据源文件 BufferedInputStream bufferedInputStream = new BufferedInputStream(new FileInputStream("src\\input.txt"));//BufferedInputStream(InputStream ) //创建输出流, 关联目的地文件. BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(new FileOutputStream("src\\output.txt"));//BufferedOutputStream(OutputStream) //接收数据 byte[] bytes = new byte[8192];//建议数组长度设置为8192,与字节缓冲区长度相同. //定义变量, 用来记录读取到的数据(字节). int len; //循环读取, 只要条件满足, 就一直读, 并将读取到的数据赋值给变量. while ((len = bufferedInputStream.read(bytes)) != -1) { //将读取到的数据写入到目的地文件中. bufferedOutputStream.write(bytes, 0, len); } //关流,释放资源 bufferedInputStream.close(); bufferedOutputStream.close(); } }
InputStreamReader && OutputStreamWriter
- 转换流一次读写一个字符.
import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.InputStreamReader; import java.io.OutputStreamWriter; public class Demo5 { public static void main(String[] args) throws Exception { InputStreamReader inputStreamReader = new InputStreamReader(new FileInputStream("src\\input.txt")); OutputStreamWriter outputStreamWriter = new OutputStreamWriter(new FileOutputStream("src\\output.txt")); int len; while ((len = inputStreamReader.read())!=-1){ outputStreamWriter.write(len); } inputStreamReader.close(); outputStreamWriter.close(); } }
- 转换流一次读写一个字符数组.
import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.InputStreamReader; import java.io.OutputStreamWriter; public class Demo6 { public static void main(String[] args) throws Exception { //new InputStreamReader(new FileInputStream("day12/data/1.txt"),"utf-8");设置读取格式为utf-8,如果不设置格式,则为当前编译器默认的编码 InputStreamReader inputStreamReader = new InputStreamReader(new FileInputStream("src\\input.txt")); //new OutputStreamWriter(new FileOutputStream("day12/data/2.txt"),"utf-8");设置读取格式为utf-8,如果不设置格式,则为当前编译器默认的编码 OutputStreamWriter outputStreamWriter = new OutputStreamWriter(new FileOutputStream("src\\output.txt")); int len; char[] bytes = new char[8192]; while ((len = inputStreamReader.read(bytes)) != -1) { outputStreamWriter.write(bytes, 0, len); } inputStreamReader.close(); outputStreamWriter.close(); } }
FileReader && FileWriter
- 普通的字符流一次读写一个字符.
import java.io.FileReader; import java.io.FileWriter; public class Demo7 { public static void main(String[] args) throws Exception { FileReader fileReader = new FileReader("src\\input.txt"); FileWriter fileWriter = new FileWriter("src\\output.txt"); int len; while ((len = fileReader.read()) != -1) { fileWriter.write(len); } fileReader.close(); fileWriter.close(); } }
- 普通的字符流一次读写一个字符数组.
public class Demo8 { public static void main(String[] args) throws Exception { FileReader fileReader = new FileReader("src\\input.txt"); FileWriter fileWriter = new FileWriter("src\\output.txt"); int len; char[] chars = new char[8192]; while ((len = fileReader.read(chars)) != -1) { fileWriter.write(chars, 0, len); } fileReader.close(); fileWriter.close(); } }
BufferedReader && BufferedWriter
- 高效的字符流一次读写一个字符.
import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.FileReader; import java.io.FileWriter; public class Demo9 { public static void main(String[] args)throws Exception { BufferedReader bufferedReader = new BufferedReader(new FileReader("src\\input.txt")); BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter("src\\output.txt")); int len; while ((len = bufferedReader.read())!=-1){ bufferedWriter.write(len); } bufferedReader.close(); bufferedWriter.close(); } }
- 高效的字符流一次读写一个字符数组.
import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.FileReader; import java.io.FileWriter; public class Demo10 { public static void main(String[] args) throws Exception { BufferedReader bufferedReader = new BufferedReader(new FileReader("src\\input.txt")); BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter("src\\output.txt")); int len; char[] chars = new char[8192]; while ((len = bufferedReader.read(chars)) != -1) { bufferedWriter.write(chars, 0, len); } bufferedReader.close(); bufferedWriter.close(); } }
- 高效的字符流一次读写一行. //掌握
import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.FileReader; import java.io.FileWriter; public class Demo11 { public static void main(String[] args) throws Exception { BufferedReader bufferedReader = new BufferedReader(new FileReader("src\\input.txt")); BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter("src\\output.txt")); String len; while ((len = bufferedReader.readLine()) != null) { bufferedWriter.write(len); bufferedWriter.newLine();//换行 } bufferedReader.close(); bufferedWriter.close(); } }
需求2: 拷贝任意类型的文件.
1. 普通的字节流一次读写一个字节. 2. 普通的字节流一次读写一个字节数组. 3. 高效的字节流一次读写一个字节. 4. 高效的字节流一次读写一个字节数组.