IO流(文件复制的方法)
2017-11-15 20:22
399 查看
1.使用字符流复制文件的5种方法
package com.dml.io2; /** * 使用字符流的方法复制文件 */ import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.FileReader; import java.io.FileWriter; public class Copy { public static void main(String[] args) throws Exception{ method1("Hello.java","1.java"); method2("Hello.java","2.java"); method3("Hello.java","3.java"); method4("Hello.java","4.java"); method5("Hello.java","5.java"); } /** * 使用读单个字符 * @param read * @param writer * @throws Exception */ private static void method1(String read, String writer) throws Exception { FileReader fr = new FileReader(read); FileWriter fw = new FileWriter(writer); int ch = 0; while((ch = fr.read()) != -1){ fw.write(ch); } fw.close(); fr.close(); } /** * 使用读一个数组 * @param read * @param writer * @throws Exception */ private static void method2(String read, String writer) throws Exception { FileReader fr = new FileReader(read); FileWriter fw = new FileWriter(writer); char []chs = new char[1024]; int len = 0; while((len = fr.read(chs)) != -1){ fw.write(chs,0,len); } fw.close(); fr.close(); } /** * 使用缓冲流读单个字符 * @param read * @param writer * @throws Exception */ private static void method3(String read, String writer) throws Exception { BufferedReader br = new BufferedReader(new FileReader(read)); BufferedWriter bw = new BufferedWriter(new FileWriter(writer)); int ch = 0; while((ch = br.read()) != -1){ bw.write(ch); bw.flush(); } bw.close(); br.close(); } /** * 使用缓冲流读数组 * @param read * @param writer * @throws Exception */ private static void method4(String read, String writer) throws Exception { BufferedReader br = new BufferedReader(new FileReader(read)); BufferedWriter bw = new BufferedWriter(new FileWriter(writer)); char []chs = new char[1024]; int len = 0; while((len = br.read(chs)) != -1){ bw.write(chs, 0, len); bw.flush(); } bw.close(); br.close(); } /** * 使用缓冲流读一行 * @param read * @param writer * @throws Exception */ private static void method5(String read, String writer) throws Exception { BufferedReader br = new BufferedReader(new FileReader(read)); BufferedWriter bw = new BufferedWriter(new FileWriter(writer)); String line = null; while((line = br.readLine()) != null){ bw.write(line); bw.newLine(); bw.flush(); } bw.close(); br.close(); } }
2.使用字节流复制文件的4种方法
package com.dml.io2; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.FileInputStream; import java.io.FileOutputStream; /** * 使用字节流复制文件 * @author dml * */ public class Copy_zijie { public static void main(String[] args) throws Exception { method1("a.jpg","C:\\Users\\dml\\Pictures\\Saved Pictures\\1.jpg"); method2("a.jpg","C:\\Users\\dml\\Pictures\\Saved Pictures\\2.jpg"); method3("a.jpg","C:\\Users\\dml\\Pictures\\Saved Pictures\\3.jpg"); method4("a.jpg","C:\\Users\\dml\\Pictures\\Saved Pictures\\4.jpg"); } private static void method1(String input, String output) throws Exception { FileInputStream fis = new FileInputStream(input); FileOutputStream fos = new FileOutputStream(output); int by = 0; while((by = fis.read()) != -1){ fos.write(by); } fos.close(); fis.close(); } /** * 读一个字节数组 * @param input * @param output * @throws Exception */ private static void method2(String input, String output) throws Exception { FileInputStream fis = new FileInputStream(input); FileOutputStream fos = new FileOutputStream(output); byte []bys = new byte[1024]; int len = 0; while((len = fis.read(bys)) != -1){ fos.write(bys,0,len); } fos.close(); fis.close(); } /** * 使用缓冲流读一个字节数组 * @param input * @param output * @throws Exception */ private static void method3(String input, String output) throws Exception { BufferedInputStream bis = new BufferedInputStream(new FileInputStream(input)); BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(output)); byte []bys = new byte[1024]; int len = 0; while((len = bis.read(bys)) != -1){ bos.write(bys,0,len); } bos.close(); bis.close(); } /** * 使用缓冲流读 * @param input * @param output * @throws Exception */ private static void method4(String input, String output) throws Exception { BufferedInputStream bis = new BufferedInputStream(new FileInputStream(input)); BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(output)); int by = 0; while((by = bis.read()) != -1){ bos.write(by); } bos.close(); bis.close(); } }
相关文章推荐
- 用递归方法复制目录、子目录以及文件(文件用到IO流)
- IO流复制文件11种方法
- Java-->用递归方法复制目录、子目录以及文件(文件用到IO流)
- java.io流实现文件复制的方法
- 黑马程序员——java-使用IO流复制文件主要的五种方法
- 黑马程序员——IO流的复制文件方法(含缓冲区方法)
- JAVA中IO流部分复制文件常用的四中方法
- python复制文件的方法实例详解
- php检索或者复制远程文件的方法
- 复制一个文件到另一个文件——IO流的应用
- JAVA基础,IO流。(文件复制的实现)
- python通过shutil实现快速文件复制的方法
- java中通过IO流实现文件的复制
- C++文件复制方法总结
- 如何从宿主机拖动复制文件到虚拟机VM中的解决方法
- Python比较文件夹比另一同名文件夹多出的文件并复制出来的方法
- 下载文件时报错:无法复制文件,无法读源文件或磁的解决方法
- Python比较文件夹比另一同名文件夹多出的文件并复制出来的方法
- java中IO流读取文件的方法
- 升级Win10 11082复制文件时找不到进度条窗口的解决方法