2015-10-02 29 views
1

我必須編寫讀取文本文件的代碼,並告訴我文件中有多少行和字符。我有它的工作,但後來我意識到我不得不忽略空白的差距,所以我寫了一個方法來做到這一點。它適用於一行,但如果我有多行,它似乎計算任何空格。任何幫助,將不勝感激需要統計文本文件中的字符

import java.io.File; 
import java.io.FileNotFoundException; 
import java.io.FileReader; 
import java.io.IOException; 
import java.io.LineNumberReader; 
import java.util.Scanner; 

import javax.swing.JOptionPane; 

public class Inputfile { 

public static void main(String[] args) { 
    System.out.println("file name:"); 
    Scanner sc = new Scanner(System.in); 
    String fn = sc.next(); 

    int nrl = 0, nChar = 0;// nrl for number of lines 
    String line;// get line content variable 

    try { 
     File fs = new File("C:/" + fn); 
     nChar = length_ws(fs); 
     FileReader fr;// for reading file 

     fr = new FileReader(fs); 
     LineNumberReader lnr = new LineNumberReader(fr); 
     while (lnr.readLine() != null) { 
      nrl++;// count number of lines 
     } 
     JOptionPane.showMessageDialog(null, "number of lines:" + nrl + "\ntotal number of chars:" + nChar); 

     lnr.close(); 
     fr.close();// close file 
    } catch (FileNotFoundException ex) { 
     System.err.println("File not found"); 
     System.exit(0); 
    } catch (IOException ex) { 

    } 
} 

public static int length_ws(File f) throws IOException { 
    FileReader fr = null; 
    fr = new FileReader(f); 
    int i; 
    i = 0; 
    int c = 0; 
    do { 

     c = fr.read();// read character 

     if (c!= ' ') // count character except white space 
      i++; 
    } while (c != -1); 
    return i - 1;// because the is counted even the end of file 
} 
} 
+0

有你的代碼中的許多可能出現的問題。您正在使用'read()'方法來讀取字節,而不是字符,因此您正在假設在一個字節上寫入字符的文件的編碼。它可能與ASCII工作,但如果你將使用像UTF16編碼,你會得到一些問題。同樣'if(c!='')'只忽略空格,但會包含所有其他字符,如製表符'\ t',行分隔符'\ n'' \ r'或標點符號。在讀取行時,您應該簡單地處理字符的計算(使用String和Character類中的方法從行中獲取和測試這些字符)。 – Pshemo

+0

這聽起來像你可能需要學習如何使用調試器來逐步執行代碼。使用一個好的調試器,您可以逐行執行您的程序,並查看它與您期望的偏離的位置。如果你打算做任何編程,這是一個重要的工具。進一步閱讀:** [如何調試小程序](http://ericlippert.com/2014/03/05/how-to-debug-small-programs/)** – NathanOliver

回答

1

我不認爲它正在閱讀的空間,但換行(因爲這些是字符)。

我建議你只讀一次文件(現在看來你讀了兩遍)。

爲char到達

c = fr.read() 

你evalute其中炭被檢查出子囊表ASCII TABLE,你有空間,製表符和換行(注意根據格式,你可以有兩個字符LF和CR的換行)

如果你有有效的字符你提前你的字符計數器。 如果您有換行的有效字符,可以提前計算行數。

希望這有助於並提高你的編碼,運氣好的話

看到你的評論我加入這個代碼,它並不完美,但一開始

int LF = 10; // Line feed 
    int CR = 13; // Chr retrun 
    int SPACE = 32;// Space 
    int TAB = 9; // Tab 

    FileReader fr = null; 
    int numberOfChars = 0; 
    int numberOfLines = 0; 
    int c = 0; 
    try { 
     do { 

      fr = new FileReader(new File("fileName.txt")); 
      c = fr.read();// read character 
      if (c > SPACE) { // space (ignoring also other chars 
       numberOfChars++; 
      } 
      if (c == LF) { // if does not have LF try CR 
       numberOfLines++; 
      } 

     } while (c != -1); 

    } catch (Exception e) { 
     e.printStackTrace(); 
     if (fr != null) { 
      try { 
       fr.close(); 
      } catch (IOException e1) { 
      } 
     } 

    } 
+0

不,我仍然困惑如何解決it – niallo27

+0

對charset的反思,文件的編碼我相信你不必擔心知道,如果你有一個奇怪的語言可能性的空間和LF CR總是相同的。 –

+0

檢查代碼注意我如何忽略32以下的所有字符(他們都「奇怪」檢查ascii表,當我發現LF(換行符)字符計數一個換行符。如果LF不起作用嘗試與CR(Carrage return)char。這不是最完美的代碼,但你有一個開始,而且,我關閉了流;)讓我知道,如果你仍然有問題 –

相關問題