Java基础篇:文件如何读写?

244 阅读3分钟

Java为你提供了一系列的读写文件的类和方法。在Java中,所有的文件都是字节形式的。

Java提供从文件读写字节的方法。而且,Java允许在字符形式的对象中使用字节文件流。

两个最常用的流类是FileInputStream和FileOutputStream,它们生成与文件链接的字节流。为打开文件,你只需创建这些类中某一个类的一个对象,在构造函数中以参数形式指定文件的名称。这两个类都支持其他形式的重载构造函数。下面是我们将要用到的形式:

FileInputStream(String fileName) throws FileNotFoundException 
FileOutputStream(String fileName) throws FileNotFoundException

这里,fileName指定需要打开的文件名。当你创建了一个输入流而文件不存在时,引发FileNotFoundException异常。对于输出流,如果文件不能生成,则引发FileNotFound Exception异常。如果一个输出文件被打开,所有原先存在的同名的文件被破坏。

当你对文件的操作结束后,需要调用close( )来关闭文件。该方法在FileInputStream和FileOutputStream中都有定义。如下:

void close( ) throws IOException

为读文件,可以使用在FileInputStream中定义的read( )方法。我们用到的如下:

int read( ) throws IOException

该方法每次被调用,它仅从文件中读取一个字节并将该字节以整数形式返回。当读到文件尾时,read( )返回-1。该方法可以引发IOException异常。下面的程序用read()来输入和显示文本文件的内容,该文件名以命令行形式指定。

注意try/catch块处理程序运行时可能发生的两个错误——未找到指定的文件或用户忘记包括文件名了。当你用命令行时也可以用这样的方法。

/* Display a text file. 
 To use this program, specify the name 
 of the file that you want to see. 
 For example, to see a file called TEST.TXT, 
 use the following command line. 
 Java ShowFile TEST.TXT 
*/ 
import java.io.*; 
class ShowFile { 
 public static void main(String args[]) 
 throws IOException 
 { 
 int i; 
 FileInputStream fin; 
 try { 
 fin = new FileInputStream(args[0]); 
 } catch(FileNotFoundException e) { 
 System.out.println("File Not Found"); 
 return; 
 } catch(ArrayIndexOutOfBoundsException e) { 
 System.out.println("Usage: ShowFile File"); 
 return; 
 } 
 // read characters until EOF is encountered 
 do { 
 i = fin.read(); 
 if(i != -1) System.out.print((char) i); 
 } while(i != -1); 
 fin.close(); 
 } 
}

向文件中写数据,需用FileOutputStream定义的write()方法。它的最简单形式如下:

void write(int byteval) throws IOException

该方法按照byteval指定的数向文件写入字节。尽管byteval作为整数声明,但仅低8位字节可以写入文件。如果在写的过程中出现问题,一个IOException被引发。下面的例子用write()拷贝一个文本文件:

/* Copy a text file. 
 To use this program, specify the name 
 of the source file and the destination file. 
 For example, to copy a file called FIRST.TXT 
 to a file called SECOND.TXT, use the following 
 command line. 
 Java CopyFile FIRST.TXT SECOND.TXT 
*/ 
import java.io.*; 
class CopyFile { 
 public static void main(String args[]) 
 throws IOException 
 { 
 int i; 
 FileInputStream fin; 
 FileOutputStream fout; 
 try { 
 // open input file 
 try { 
 fin = new FileInputStream(args[0]); 
 } catch(FileNotFoundException e) { 
 System.out.println("Input File Not Found"); 
 return; 
 } 
 // open output file 
 try { 
 fout = new FileOutputStream(args[1]); 
 } catch(FileNotFoundException e) { 
 System.out.println("Error Opening Output File"); 
 return; 
 } 
 } catch(ArrayIndexOutOfBoundsException e) { 
 System.out.println("Usage: CopyFile From To"); 
 return; 
 } 
 // Copy File 
 try { 
 do { 
 i = fin.read(); 
 if(i != -1) fout.write(i); 
 } while(i != -1); 
 } catch(IOException e) { 
 System.out.println("File Error"); 
 } 
 fin.close(); 
 fout.close(); 
 } 
}

注意本程序中和前面ShowFile程序中处理潜在输入/输出错误的方法。不像其他的计算机语言,包括C和C++,这些语言用错误代码报告文件错误,而Java用异常处理机制。这样不仅是文件处理更为简洁,而且使Java正在执行输入时容易区分是文件出错还是EOF条件问题。

在C/C++中,很多输入函数在出错时和到达文件结尾时返回相同的值(也就是说,在C/C++中,EOF情况与输入错误情况映射相同)。

这通常意味着程序员必须还要编写特殊程序语句来判定究竟是哪种事件发生。Java中,错误通过异常引发,而不是通过read( )的返回值。这样,当read( )返回-1时,它仅表示一点:遇到了文件的结尾。