2010-10-29 92 views
0

雖然試圖做一些文件操作的代碼是這樣的,文件處理Java

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

我在程序BufferedReader看到和InputStreamReader也包括在內,你可以用一個簡單的例子來解釋?我讀過很多關於文件處理的網站,但仍然令人困惑!

回答

5

File類本質上是一個文件描述符,它允許您獲取文件的句柄,但本身並不具有從文件讀取信息的方法。

這就是在InputStreamReader進來。一個InputStreamReader(和更容易的子類FileReader)會爲你做閱讀(還有其他的方法來做到這一點,但是這是最簡單的一個)。

BufferedReader將包裝InputStreamReader並將讀取文件到緩衝區(而不是簡單地轉換並在每次讀取調用後返回字節),讓您更容易讀取數據。

0

看看這個基本tutorial

基本上BufferedReader是讀取文件信息。有多種方法可以做到這一點,這取決於您的需求。

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,重點是在一個文件中逐行讀取。

+0

很好的例子,但在finally塊中有一個潛在的NPE(如果在新的BufferedReader ...行中發生錯誤,讀者可能爲空)。 – 2010-10-29 13:10:06

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 
} 

正如我所記得的,類BufferedReader和FileReader可以在java.io中找到;

在我看來,這是用Java讀取文件的最簡單方法。如果你需要知道如何寫文件,這個過程幾乎是一樣的,所以我也可以給你一些建議。

希望這會有所幫助。

0

基本上java.io.File類代表在文件系統中的文件,但不包含方法來操縱它們(除創建或刪除它們)

當你與他們合作,你使用java.io包中的其他類,BufferefReader和InputStreamReader就在其中,但其他類似FileInputStream。

根據操作要執行,你可以使用閱讀器或流(類與「讀者」或結束「作家」是對文本內容,以「流」結尾的類爲二進制內容,但當然,你可以隨時讀/寫文本爲二進制)。

大多數時候你必須「鏈接」幾個這些類來執行工作。同樣需要注意的是,大多數情況下,您可以使用非常類似的代碼寫入套接字。

一個簡單的例子可能是這樣的:

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

0

爲了清楚起見,從InputStreamReader.java

的InputStreamReader的javadoc的截取是從 字節的橋接流以字符流:這 讀取字節並將其解碼爲使用 字符指定的字符集。 每次調用InputStreamReader的read()方法中的一個 可能會導致從底層字節輸入流中讀取一個或多個字節 。 要啓用的 字節字符有效轉化,更字節可以 從底層流 超前讀比是必需的,以滿足當前 讀取操作。

對於頂部效率,考慮包裝 的InputStreamReader一個 的BufferedReader內。例如:

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