IO综合练习

IO综合练习

1. 文件复制

package cn.tonyoliver.io;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.Reader;
import java.io.Writer;
import java.util.Scanner;

/**
 * 这个类用来测试文件复制
 * 
 * @author Administrator
 *
 */
public class Test03_Copy {
	public static void main(String[] args) throws IOException {
		// 1,接收用户输入的源文件路径
		System.out.println("请输入源文件的路径:");
		String frompath = new Scanner(System.in).nextLine();
		File from = new File(frompath);

		// 2,接收用户的目标文件路径
		System.out.println("请输入目标文件的路径:");
		String topath = new Scanner(System.in).nextLine();
		File to = new File(topath);

		// 3,调用指定方法完成复制
		// copy(from,to);//字节流
		copy2(from, to);// 字符流
	}

	// 字节流复制
	private static void copy(File from, File to) throws IOException {
		// 1,创建对象
		InputStream in = new BufferedInputStream(new FileInputStream(from));
		OutputStream out = new BufferedOutputStream(new FileOutputStream(to));

		// 2,开始读取并复制
		int b = 0;
		while ((b = in.read()) != -1) {
			out.write(b);
		}

		// 3,关闭资源
		in.close();
		out.close();
		System.out.println("文件复制成功!");
	}

	// 字符流复制
	private static void copy2(File from, File to) throws IOException {
		// 1,创建对象
		Reader in = new BufferedReader(new FileReader(from));
		Writer out = new BufferedWriter(new FileWriter(to));

		// 2,开始读取并复制
		int b = 0;
		while ((b = in.read()) != -1) {
			out.write(b);
		}

		// 3,关闭资源
		in.close();
		out.close();
		System.out.println("复制成功!");
	}
}

2. 批量读写

package cn.tonyoliver.io;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.Reader;
import java.io.Writer;
import java.util.Scanner;

/**
 * 这个类用来测试批量读写
 * @author Tony
 *
 */
public class Test03_Copy {
	public static void main(String[] args) throws IOException {
		//1,接收用户输入的源文件路径
		System.out.println("请输入源文件的路径:");
		String frompath = new Scanner(System.in).nextLine();
		File from = new File(frompath);
		
		//2,接收用户输入的目标文件路径
		System.out.println("请输入目标文件的路径:");
		String topath = new Scanner(System.in).nextLine();
		File to = new File(topath);
		
		//3,调用指定方法完成复制
		//copy(from,to);
		copy2(from,to);
	}

	// 字节流
	private static void copy(File from, File to) throws IOException {
		// 1,创建对象
		InputStream in = new BufferedInputStream(new FileInputStream(from));
		OutputStream out = new BufferedOutputStream(new FileOutputStream(to));
		
		// 2,边读边写
		int b = 0;
		byte[] buf = new byte[8*1024];
		while( ( b= in.read(buf) ) != -1 ) {//优化成一个数组一个数组的读
			//write(buf,0,b)--第一个参数是指要写出哪个数组里的数据,
			//第二参数是指从数组的哪个位置开始写出数据,第三个参数是指要写出的数据长度
			out.write(buf,0,b);//优化成一个数组一个数组的写出,必须用三个参数的,不然会多复制数据的!!
		}
		// 3,释放资源
		in.close();
		out.close();
		System.out.println("恭喜您,文件复制完成!!");
		
	}
	
	// 字符流
	private static void copy2(File from, File to) throws IOException {
		// 1,创建对象
		Reader in = new BufferedReader(new FileReader(from));
		Writer out = new BufferedWriter(new FileWriter(to));
		
		// 2,边读边写
		int b = 0;
		char[] buf = new char[8*1024];
		while((b=in.read(buf))!=-1) {
			out.write(buf);
		}
		
		// 3,释放资源
		in.close();
		out.close();
		System.out.println("恭喜您,文件复制完成!!");
	}
}

你可能感兴趣的:(IO,java)