目录

方法1:BufferedReader直接包装字符流、PrintWriter直接包装字符流

方法2:不包装直接使用字节流

方法3:BufferedReader直接包装字节流、PrintWriter(先转换)包装字节流

方法4:BufferedReader(先转换)包装字节流、PrintWriter直接包装字符流

方法5:桥梁流。BufferedReader和BufferedWriter都通过桥梁流转换包装字节流

方法6:随机流

方法7:File、FileReader、FileWriter


方法1:BufferedReader直接包装字符流、PrintWriter直接包装字符流


package ioexerices;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.PrintWriter;

/**BufferedReader直接包装字符流
 * PrintWriter直接包装字符流
 * 将Bridge.txt--复制到->bb.txt中
 * @author HYZX
 *
 */
public class IOCopyTest {
public static void main(String[] args) {
	
	try {
		FileReader fr = new FileReader("Bridge.txt");
		BufferedReader br = new BufferedReader(fr);
		
		FileWriter fw = new FileWriter("bb.txt");
		PrintWriter pw = new PrintWriter(fw);
		
		String temp = "";
		while((temp = br.readLine())!=null){
			pw.println(temp);
			pw.flush();
		}
		
	} catch (Exception e) {
		e.printStackTrace();
	}
}
}


方法2:不包装直接使用字节流


package ioexerices;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

/**不包装直接使用字节流
 * 将Bridge.txt--复制到->cc.txt中    ; 
 * @author HYZX
 *
 */
public class IOCopyTest2 {
public static void main(String[] args) throws IOException {
	FileInputStream fis = new FileInputStream(new File("Bridge.txt"));
	FileOutputStream fos = new FileOutputStream(new File("cc.txt"));
	byte b[] = new byte[1024];
	int len=0;
	while((len=fis.read(b))!=-1){
		fos.write(b);
	}
}
}


方法3:BufferedReader直接包装字节流、PrintWriter(先转换)包装字节流


package ioexerices;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;

/** BufferedReader直接包装字节流
 *  PrintWriter(先转换)包装字节流
 * 
 * 将Bridge.txt--复制到->cc.txt中    ; 提高效率
 * @author HYZX
 *
 */
public class IOCopyTest3 {
public static void main(String[] args) throws IOException {
	File f = new File("Bridge.txt");
	BufferedReader br = new BufferedReader(
			new InputStreamReader(new FileInputStream(f)));
	
	PrintWriter pw = new PrintWriter(new OutputStreamWriter(
			new FileOutputStream(new File("cc.txt"))));
			
	byte b[] = new byte[(int)f.length()];
	String len="";
	while((len = br.readLine())!=null){
		pw.println(len);
		pw.flush(); //此句至关重要
	}
//	System.out.println(new String(b,0,b.length));
}
}


方法4:BufferedReader(先转换)包装字节流、PrintWriter直接包装字符流


package ioexerices;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
/**
 * BufferedReader(先转换)包装字节流
 * PrintWriter直接包装字符流
 * @author HYZX
 *
 */
public class IOCopyTest4 {

	public static void main(String[] args) throws IOException {
		File f = new File("Bridge.txt");
		BufferedReader br = new BufferedReader(
				new InputStreamReader(new FileInputStream(f)));
		PrintWriter pw = new PrintWriter(new FileWriter(new File("cc.txt")));
		
		BufferedWriter bw = new BufferedWriter(
				new OutputStreamWriter(new FileOutputStream(new File("cc.txt"))));
		byte b[] = new byte[(int)f.length()];
		String len="";
		while((len = br.readLine())!=null){
			pw.println(len);
			pw.flush();
		}
		
	}
	}


方法5:桥梁流。BufferedReader和BufferedWriter都通过桥梁流转换包装字节流


package ioexerices;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
/**BufferedReader和BufferedWriter都通过桥梁流转换包装字节流
 * @author HYZX
 *
 */
public class IOCopyTest5 {
	public static void main(String[] args) throws IOException {
		File f = new File("Bridge.txt");
		BufferedReader br = new BufferedReader(
				new InputStreamReader(new FileInputStream(f)));
		
		BufferedWriter bw = new BufferedWriter(
				new OutputStreamWriter(new FileOutputStream(new File("cc.txt"))));
		byte b[] = new byte[(int)f.length()];
		String len="";
		while((len = br.readLine())!=null){
			bw.write(len);
			bw.flush();
			bw.newLine();
		}
	}
}


方法6:随机流


package ioexerices;

import java.io.RandomAccessFile;
/**
 * 使用随机流
 * @author HYZX
 *
 */
public class IOCopyTest6 {
public static void main(String[] args) throws Exception {
	RandomAccessFile raf1 = new RandomAccessFile("Bridge.txt", "r");
	RandomAccessFile raf2 = new RandomAccessFile("cc.txt", "rw");
	//方式1
	int count=0;
	byte b[] = new byte[1024];
	while((count = raf1.read(b))!=-1){
		raf2.write(b);
	}
	
/*  方式2
 * 	int len=0;
	while((len = raf1.read())!=-1){
		raf2.write(len);
	}*/
}
}


方法7:File、FileReader、FileWriter


package ioexerices;

import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;

/**
 * 使用File和 FileReader  FileWriter
 * @author HYZX
 *
 */
public class IOCopyTest7 {
public static void main(String[] args) throws Exception {
	File f1=  new File("Bridge.txt");
	FileReader fr = new FileReader(f1);
	File f2 = new File("cc.txt");
	FileWriter fw =  new FileWriter(f2);
	
	//方式1
/*	int count = 0;
	while((count = fr.read())!=-1){
		System.out.print((char)count);
		fw.write(count);
//		fw.write((char)count);  //两个都可以
	}*/
	//方式2
	char c[] = new char[(int) f1.length()];
	int temp=0;
	while((temp = fr.read(c))!=-1){
		fw.write(c);
		fw.flush();
	}
	
	fw.close();
	fr.close();//关流很重要
}
}