Java基于字符流和字节流的文件I/O方法

在Java中,所有文件都是由字节组成,Java毫无疑问提供了从文件读字节和向文件中写字节的方法。然而,Java也可以使用字符流进行文件I/O操作,甚至有时候使用字符流可能会比使用字节流更有优势,基于这些,这里笔者将介绍基于两种流的文件I/O方法。

1.使用字节流读写文件

1.1 基于字节流的文件读出

首先,创建FileInputStream对象用于打开输入的文件,FileInputStream最常用的构造函数如下(当然也可以使用其他构造函数):

//fileName为要打开的文件名,若该文件不存在就会抛出FileNotFoundException
FileInputStream (String fileName) throws FileNotFoundException

其次,使用read()方法类读取文件,read方法有三个版本:
a. int read()
b. int read(byte buffer[])
c. int read(byre buffer,int offset,int numBytes)
这里我们使用版本a来做详细介绍,当到达文件末尾时,read()会返回-1,出错时则会抛出IOException.
最后,当不需要使用文件后,要调用close()方法来关闭文件,它的基本形式如下:

void close() throws IOException

从文件读入的代码示例:

FileInputStream fin=null;
		int i;
		try{
				fin=new FileInputStream("D:\\JT\\test1.txt");
				do {
					i=fin.read();
					if(i!=-1)
						System.out.print((char) i);
				}while(i!= -1);

			}
			catch(IOException exc){
				System.out.println("I/O Error: "+exc);
			}
			finally{
				try{
					if(fin!=null)
						fin.close();
				}
				catch(IOException exc){
					System.out.println("Closing Error");
				}
			}

1.2 基于字节流的文件写入

首先,创建一个FileOutputStream对象,用于打开一个文件用于写入,以下是它最常用的构造函数:

//打开一个文件后会覆盖之间的同名文件
FileOutputStream(String fineName) throws FileNotFoundException

//当append的值为true时,写入会追加到文件的末尾,为false时,与上面的方法类似
FileOutputStream(String fileName,boolean append) throws FileNotFoundException

其次,我们需要使用write方法来写入文件,同样write也有几种版本,这里就不一一列举,笔者使用最简单的形式:
void write(int byteval) throws IOException //该种方法写入byteval的低8位到文件中去
最后,使用close()关闭它。
写入文件代码示例:

FileOutputStream fout=null;
		int i;
		try{
				fout=new FileOutputStream("D:\\JT\\test1.txt",true);
				do {
					i=(char)System.in.read();
					if(i!='#')
						fout.write(i);
				}while(i!='#');

			}
			catch(IOException exc){
				System.out.println("I/O Error: "+exc);
			}
			finally{
				try{
					if(fout!=null)
						fout.close();
				}
				catch(IOException exc){
					System.out.println("Closing Error");
				}
			}

2.使用字符流读写文件

2.1 基于字符流的文件读出

首先,利用FileReader类创建一个用于读取文件内容的Reader,以下是它最常用的构造函数:

FileReader(String fileName) throws FileNotFoundException

其次,创建一个BufferedReader对象,调用如下代码:

BufferedReader br=new BufferedReader(new FileReader("D:\\JT\\test2"));

然后,再调用readLine()方法即可从文件以字符流的形式读出文件内容。
最后,读取完毕后,调用close()方法关闭文件即可。
基于字符流的文件写入示例:

FileReader fr=null;
		try{
			
				fr=new FileReader("C:\\JT\\test2");
				BufferedReader br=new BufferedReader(fr);
				while((br.readLine())!=null)
				{
					System.out.println(br.readLine());
				}

			}
			catch(IOException exc){
				System.out.println("I/O Error: "+exc);
			}
			finally{
				try{
					if(fr!=null)
						fr.close();
				}
				catch(IOException exc){
					System.out.println("Closing Error");
				}
			}

2.2 基于字符流的文件写入

首先,利用FileWriter创建一个用于写入文件的Writer,它最常用的构造函数如下:

//打开一个文件后会覆盖同名文件
FileWriter(String fileName) throws IOExpection

//append为true追加到文件末尾,否则文件被重写
FileWriter(String fileName,boolean append) throws IOExpection

其次,由于FileWriter是从OutputStream Writer和Writer派生而来的,FileWriter可以使用这些类定义write()方法,write()方法同样有几个版本,这里笔者使用的是如下版本:
void write(String str);//写入字符串str
最后,当文件处理完后同样使用close()关闭它。
基于字符流的文件写入示例:

//在输入"no"之前,文件会把键盘输入读取到文件中去
		BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
		FileWriter fw=null;
		try{
				fw=new FileWriter("D:\\JT\\test2",true);
				String str=br.readLine();
				while(str.compareTo("no")!=0)
				{
					fw.write(str);
					str=br.readLine();
				}
			}
			catch(IOException exc){
				System.out.println("I/O Error: "+exc);
			}
			finally{
				try{
					if(fw!=null)
						fw.close();
				}
				catch(IOException exc){
					System.out.println("Closing Error");
				}
			}
发布了16 篇原创文章 · 获赞 18 · 访问量 4137

猜你喜欢

转载自blog.csdn.net/qq_42103091/article/details/98725802