上一次,我们在from.txt中写的是123456789,但是实际上做文件IO操作的时候,内容不可能只有123456789,因此,这一次,

我在from.txt里放了一篇英文文章。如下

BEIJING - China's Health Ministry on Monday vowed to cooperate with police investigations into medical institutions and workers involved in instances of illegal human organ transplants.

Police will announce the list of hospitals and doctors involved in some recently-uncovered human organ trafficking rings, Deng Haihua, the ministry's spokesman, said at a press conference here.

The ministry will work with judicial departments to punish those who have violated professional codes, Deng said.

Chinese police said early this month that 137 suspects had been arrested in the latest crackdown on human organ trafficking jointly conducted by 18 provincial police authorities in late July. Police rescued 127 organ suppliers in the operation.

Police said the detained suspects illegally recruited suppliers over the Internet, facilitated the deals and made huge profits from the transactions.

Also, a court in central China's Hunan Province on Friday concluded the trial of nine people involved in the illegal trade of a teenager's kidney. A surgeon, two nurses, a surgical assistant and an anesthesiologist were charged.

Test.java

import java.io.*;
//加入IO流的包
class Test{
	public static void main(String args []){
		//声明输入流引用
		FileInputStream fis = null;
		//声明输出流引用
		FileOutputStream fos = null;
		//用try catch捕捉异常
		try{
			//生成代表输入流的对象
			fis = new FileInputStream("d:/work/src/from.txt");
			//生成代表输出流的对象
			fos = new FileOutputStream("d:/work/src/to.txt");
			//生成一个字节数组,这里设为1024个字节,即1K
			byte [] buffer = new byte[1024];
			//循环语句
			while(true){
				//调用输入流对象的read方法,读取数据,用temp保存其返回值。
				int temp = fis.read(buffer, 0, buffer.length);
				//读取完后,也就是没读到数据,fis.read会返回-1
				if(temp == -1){
					//跳出循环语句
					break;
				}
				//调用输出流对象的write方法,写入数据。
				fos.write(buffer,0,temp);
			}
		}
		catch(Exception e){
			System.out.println(e);
		}
	}
}

多加了一个循环语句,每次读完数据都判断read方法的返回值是否正常,如果为-1,则已经没有数据可读,则应跳出循环语句。

编译执行后,from.txt和to.txt的内容一致。

下面介绍关闭IO流的方法

Test.java

import java.io.*;
//加入IO流的包
class Test{
	public static void main(String args []){
		//声明输入流引用
		FileInputStream fis = null;
		//声明输出流引用
		FileOutputStream fos = null;
		//用try catch捕捉异常
		try{
			//生成代表输入流的对象
			fis = new FileInputStream("d:/work/src/from.txt");
			//生成代表输出流的对象
			fos = new FileOutputStream("d:/work/src/to.txt");
			//生成一个字节数组,这里设为1024个字节,即1K
			byte [] buffer = new byte[1024];
			//循环语句
			while(true){
				//调用输入流对象的read方法,读取数据,用temp保存其返回值。
				int temp = fis.read(buffer, 0, buffer.length);
				//读取完后,也就是没读到数据,fis.read会返回-1
				if(temp == -1){
					//跳出循环语句
					break;
				}
				//调用输出流对象的write方法,写入数据。
				fos.write(buffer,0,temp);
			}
		}
		catch(Exception e){
			System.out.println(e);
		}
		//不管try里的代码是否产生异常,都会执行finally的代码,
		//所以关闭文件的代码放在finally里比较合适。
		finally{
			//这里用try catch的原因,是因为,fis.close()和fos.close()会产生异常
			//所以这里同样对异常进行捕捉。
			try{
				fis.close();
				fos.close();
			}
			catch(Exception e){
				System.out.println(e);
			}
		}
	}
}

在之前的代码上加了finally{}

上面是字节流

----------------------------------------------------------

下面是字符流

from.txt内容为abcde

to.txt内容为空

TestChar.java

//字符流:读写文件时,以字符为基础
//字节输入流:Reader <-- FileReader         ||  FileInputStream
// int read(char [] c, int off, int len)    ||  read
//字节输出流:Writer <-- FileWriter         ||  FileOutputStream
// void write(char [] c, int off, int len)  ||  write
import java.io.*;

public class TestChar{
	public static void main(String args []){
		FileReader fr = null;
		FileWriter fw = null;
		try{
			fr = new FileReader("d:/work/src/from.txt");
			fw = new FileWriter("d:/work/src/to.txt");
			char [] buffer = new char[100];
			int temp = fr.read(buffer,0,buffer.length);
			fw.write(buffer,0,temp);
		}
		catch(Exception e){
			System.out.println(e);
		}
		finally{
			try{
				fr.close();
				fw.close();
			}
			catch(Exception e){
				System.out.println(e);
			}
		}
	}
}

可以看出来,字符流跟字节流是非常相似的,所以学会了字节流,字符流也是一样的。

执行完后,to.txt和from.txt内容一致。

同样的,如果是from.txt里面装了一篇文章呢,修改TestChar.java文件如下

//字符流:读写文件时,以字符为基础
//字节输入流:Reader <-- FileReader         ||  FileInputStream
// int read(char [] c, int off, int len)    ||  read
//字节输出流:Writer <-- FileWriter         ||  FileOutputStream
// void write(char [] c, int off, int len)  ||  write
import java.io.*;

public class TestChar{
	public static void main(String args []){
		FileReader fr = null;
		FileWriter fw = null;
		try{
			fr = new FileReader("d:/work/src/from.txt");
			fw = new FileWriter("d:/work/src/to.txt");
			char [] buffer = new char[100];
			while(true){
				int temp = fr.read(buffer,0,buffer.length);
				if(temp == -1){
					break;
				}
				fw.write(buffer,0,temp);
			}
		}
		catch(Exception e){
			System.out.println(e);
		}
		finally{
			try{
				fr.close();
				fw.close();
			}
			catch(Exception e){
				System.out.println(e);
			}
		}
	}
}

这样就实现了对大容量文件的IO操作。