2015-10-25 70 views
3

我想讀取一個ascii文件並識別換行符「\ n」的位置,以知道每行有哪些字符和多少個字符。文件大小爲538MB。當我運行下面的代碼時,它從來不打印任何東西。 我搜索了很多,但我沒有找到任何ascii文件。我使用NetBeans和Java 8.任何想法??逐行讀取ascii文件 - Java

以下是我的代碼。

String inputFile = "C:\myfile.txt"; 
FileInputStream in = new FileInputStream(inputFile); 
FileChannel ch = in.getChannel(); 
int BUFSIZE = 512; 
ByteBuffer buf = ByteBuffer.allocateDirect(BUFSIZE); 
Charset cs = Charset.forName("ASCII"); 

while ((rd = ch.read(buf)) != -1) { 
     buf.rewind(); 
     CharBuffer chbuf = cs.decode(buf); 

     for (int i = 0; i < chbuf.length(); i++) { 
      if (chbuf.get() == '\n'){ 
       System.out.println("PRINT SOMETHING"); 
      } 
     } 
} 
+0

你看過http://stackoverflow.com/questions/4716503/best-way-to-read-a-text-file-in-java? –

+0

我已經看到這篇文章,但與BufferReader它拋出我Java內存不足錯誤,所以我無法使用readline()函數。 – lostromos

+0

對於大文件,使用'RandomAccessFile'而不是'FileReaders'。 – ccc

回答

0

行中的字符的數量是由一個readLine呼叫讀取的字符串的長度:

try (BufferedReader br = new BufferedReader(new FileReader(file))) { 
    int iLine = 0; 
    String line; 
    while ((line = br.readLine()) != null) { 
     System.out.println("Line " + iLine + " has " + 
          line.length() + " characters."); 
     iLine++; 
    } 
} catch(IOException ioe){ 
    // ... 
} 

注意,(系統相關)線結束標記已經從串由readLine被剝離。

如果一個非常大的文件不包含換行符,確實有可能導致內存不足。逐字閱讀將避免這種情況。

 
    File file = new File("Z.java"); 
    Reader reader = new FileReader(file); 
    int len = 0; 
    int c; 
    int iLine = 0; 
    while((c = reader.read()) != -1) { 
     if(c == '\n'){ 
      iLine++; 
      System.out.println("line " + iLine + " contains " + 
           len + " characters"); 
      len = 0; 
     } else { 
      len++; 
     } 
    } 
    reader.close(); 
+0

使用BufferedReader時,它拋出java.lang.OutOfMemoryError:Java堆空間。這就是爲什麼我使用ByteBuffer。 – lostromos

+0

@Iostromos整個文件是否可能不包含行結束符?這是一個「常規」文本文件或一些奇怪的字節串? – laune

+0

@Iostromos添加了一個不存儲任何文件數據的版本 - 這應該是確定的。 (如果速度太慢:可以改進。) – laune

-1

您應該使用FileReader這是便於閱讀字符文件的類。

FileInputStream javs docs clearly states

FileInputStream is meant for reading streams of raw bytes such as image data. For reading streams of characters, consider using FileReader.

嘗試以下

try (BufferedReader br = new BufferedReader(new FileReader(file))) { 
    String line; 
    while ((line = br.readLine()) != null) { 
     for (int pos = line.indexOf("\n"); pos != -1; pos = line.indexOf("\n", pos + 1)) { 
     System.out.println("\\n at " + pos); 
     } 
    } 
} 
+0

由於編譯錯誤,它停止運行。如何忽略它? – lostromos

+0

如果這段代碼會打印任何內容,這將是令人驚訝的。 – laune

+0

@lostromos我糾正了一個小錯誤。 –

1

法文件的內容存儲到一個字符串:

static String readFile(String path, Charset encoding) throws IOException 
{ 
    byte[] encoded = Files.readAllBytes(Paths.get(path)); 
    return new String(encoded, encoding); 
} 

這裏是找到一個人物的出現方式在整個字符串中:

public static void main(String [] args) throws IOException 
{ 
    List<Integer> indexes = new ArrayList<Integer>(); 
    String content = readFile("filetest", StandardCharsets.UTF_8); 
    int index = content.indexOf('\n'); 
    while (index >= 0) 
    { 
     indexes.add(index); 
     index = content.indexOf('\n', index + 1); 
    } 
} 

Found here and here

+0

此方法也會拋出內存不足錯誤。我提到了一個高達538MB的大文件。 – lostromos