0

在尝试对文件进行一些操作时,代码如下所示,

File file=new File("aaa.txt");

我在一个程序中看到BufferedReader并且InputStreamReader也包括在内,你能用一个简单的例子解释一下吗?我在许多网站上阅读了有关文件处理的信息,但仍然令人困惑!!!!

4

6 回答 6

5

该类File本质上是一个文件描述符,它允许您获取文件的句柄,但它本身并没有从文件中读取信息的方法。

这就是InputStreamReader出现的地方。一个InputStreamReader(更容易地是它的子类FileReader)将为你做阅读(还有其他方法可以做到,但这是最简单的方法之一)。

BufferedReader包装InputStreamReader并将文件读入缓冲区(而不是在每次读取调用后简单地转换和返回字节),从而使您可以更轻松地读取数据。

于 2010-10-29T12:57:06.253 回答
1
public void printFileContentsToConsole(final String aFileName) {
 //Make a new file.
 File file = new File(aFileName);

 //Declare the reader outside the scope so we can use it
 //in the finally block.
 BufferedReader reader = null;

 try {
  reader = new BufferedReader(new FileReader(file));
  String line;

  //Read one line at a time, printing it.
  while ((line = reader.readLine()) != null) {
   System.out.println(line);
  }
 } catch (IOException e) {
  e.printStackTrace();
 } catch (Exception e) {
  e.printStackTrace();
 } finally {
  try {
   //Try to close it, this might throw an exception anyway.
   reader.close();
  } catch (Exception ex) {
   ex.printStackTrace();
  }
 }
}

显然,查看BufferedReaderFileReader API 将回答您关于这些特定类的许多问题。

为了阐明为什么要使用 BufferedReader,关键是要高效地逐行读取文件。

于 2010-10-29T12:54:52.433 回答
0

看看这个基础教程

基本上BufferedReader是读取文件信息。有多种方法可以做到这一点,实际上取决于您的需求。

于 2010-10-29T12:53:59.580 回答
0

这是我从磁盘读取文件的方式:

请注意,所有代码都必须放在 try/catch 语句中,例如在 FileNotFoundException 的情况下。

try
{
    //You first have to create a reader that will be used to access the file. 
    //You create the BufferedReader from a FileReader, and you only need to specify 
    //the address of the file on your disk.
    BufferedReader br = new BufferedReader(new FileReader("fileURL"));

    String s;

    //The BufferedReader has a method "readLine" that reads the file line by line
    //When the reader reaches the end of the file, the method readLine returns null,
    //so you can control if there is some data remaining or not.
    while( (s = br.readLine()) != null )
    {
          System.out.println(s);
    }

    //Don't forget to close the reader when the process is over.
    br.close();
}
catch(Exception e)
{
    // Do some stuff
}

我记得,可以在包 java.io 中找到 BufferedReader 和 FileReader 类;

在我看来,这是在 Java 中读取文件的最简单方法。如果您需要知道如何编写文件,过程几乎相同,所以我也可以给您一些建议。

希望这可以帮助。

于 2010-10-29T13:09:54.357 回答
0

基本上 java.io.File 类代表文件系统中的一个文件,但不包含操作它们的方法(除了创建它们或删除它们)

当您必须使用它们时,您可以使用 java.io 包中的其他类,其中包括 BufferefReader 和 InputStreamReader,但还有其他类,例如 FileInputStream。

根据您要执行的操作,您可以使用阅读器或流(以“Reader”或“Writer”结尾的类用于文本内容,以“Stream”结尾的类用于二进制内容,但当然,您总是可以以二进制形式读/写文本)。

大多数时候,您必须“链接”其中几个类来执行工作。还需要注意的是,在大多数情况下,您可以将非常相似的代码写入套接字。

一个天真的例子可能是这样的:

package demo;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.File;
import java.io.IOException;

import java.util.List;
import java.util.ArrayList;


public class ReadLines {
    public static void main( String [] args )  throws IOException { 
        File toRead = new File( args[0]);
        if( toRead.exists() ) { 
             List<String> lines = readLines( toRead );
         } else {
             System.out.println( toRead.getName() + " doesn't exists" );
         }
    }
    private static List<String> readLines( File fromFile ) throws IOException  {
          BufferedReader reader = null;

          List<String> lines = new ArrayList<String>();
          try { 
              reader = new BufferedReader( new FileReader( fromFile ) ); // chaining 
              String line  = null;
              while( ( line = reader.readLine() ) != null ) { 
                  lines.add( line );
              }
         } finally  { 
             // always close the file 
            if( reader != null )  try { 
                reader.close();
            } catch( IOException ioe ) { 
               // it's ok to ignore an exception while closing a file 
            } 
         }
         return lines;
      }
  }

我希望这段代码能让你更清楚并编译(:PI手头没有编译器

也可以看看:

http://download.oracle.com/javase/tutorial/essential/io/ http://download.oracle.com/javase/6/docs/api/java/io/package-summary.html http://download .oracle.com/javase/6/docs/api/java/io/Reader.html http://download.oracle.com/javase/6/docs/api/java/io/Writer.html http://download .oracle.com/javase/6/docs/api/java/io/InputStream.html http://download.oracle.com/javase/6/docs/api/java/io/OutputStream.html

于 2010-10-29T13:15:23.527 回答
0

为了清楚起见,取自InputStreamReader.java的 javadoc

InputStreamReader 是从字节流到字符流的桥梁:它读取字节并使用指定的字符集将它们解码为字符。[...] InputStreamReader 的 read() 方法之一的每次调用都可能导致从底层字节输入流中读取一个或多个字节。 为了实现字节到字符的有效转换,可以从底层流中预先读取 比满足当前读取操作所需的更多的字节。

为了获得最高效率,请考虑将 InputStreamReader 包装在 BufferedReader中。例如:

BufferedReader in = new BufferedReader(new InputStreamReader(System.in));

于 2010-10-29T13:33:06.180 回答