2015-09-24 38 views
0

因此,我幾乎完成了(我認爲)我的wc程序在Java中,它從用戶輸入(甚至多個)獲取文件名,並計數文件中的行,字,字節(字符數)。有2個文件用於測試目的,它們是.dat格式,可以從dos/linux命令行讀取。除了在行尾有\ n或\ r \ n個字符時的計數以外,一切工作都正常。它不會計數這些。請幫忙?用Java編寫了一個wc程序,但它不會計算行尾字符

import java.io.*; 
import java.util.regex.Pattern; 

public class Prog03 { 


private static int totalWords = 0, currentWords = 0; 
private static int totalLines =0, currentLines = 0; 
private static int totalBytes = 0, currentBytes = 0; 

public static void main(String[] args) { 


    System.out.println("This program determines the quantity of lines,         words, and bytes\n" + 
"in a file or files that you specify.\n" + 
"\nPlease enter one or more file names, comma-separated: "); 

     getFileName(); 

     System.out.println(); 

} // End of main method. 


public static void countSingle (String fileName, BufferedReader in) { 

    try { 

     String line; 

     String[] words; 

     //int totalWords = 0; 

     int totalWords1 = 0; 

     int lines = 0; 

     int chars = 0; 

     while ((line = in.readLine()) != null) { 

      lines++; 
      currentLines = lines; 

      chars += line.length(); 
      currentBytes = chars; 

      words = line.split(" "); 

      totalWords1 += countWords(line); 
      currentWords = totalWords1; 

     } // End of while loop. 

     System.out.println(currentLines + "\t\t" + currentWords + "\t\t" +  currentBytes + "\t\t" 
       + fileName); 

    } catch (Exception ex) { 

     ex.printStackTrace(); 

    } 
} 

public static void countMultiple(String fileName, BufferedReader in) { 

    try { 

     String line; 

     String[] words; 

     int totalWords1 = 0; 

     int lines = 0; 

     int chars = 0; 

     while ((line = in.readLine()) != null) { 

      lines++; 
      currentLines = lines; 

      chars += line.length(); 
      currentBytes = chars; 

      words = line.split(" "); 

      totalWords1 += countWords(line); 
      currentWords = totalWords1; 


     } // End of while loop. 
     totalLines += currentLines;    
     totalBytes += currentBytes; 
     totalWords += totalWords1; 

    } catch (Exception ex) { 

     ex.printStackTrace(); 

    } 

} // End of method count(). 

private static long countWords(String line) { 

    long numWords = 0; 

    int index = 0; 

    boolean prevWhitespace = true; 

    while (index < line.length()) { 

     char c = line.charAt(index++);     

     boolean currWhitespace = Character.isWhitespace(c); 

     if (prevWhitespace && !currWhitespace) { 

      numWords++; 

     } 

     prevWhitespace = currWhitespace; 

    } 

    return numWords; 

} // End of method countWords(). 

private static void getFileName() {   

    BufferedReader in ;   

     try { 

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

     String fileName = in.readLine(); 

     String [] files = fileName.split(", "); 

      System.out.println("Lines\t\tWords\t\tBytes" + 
       "\n--------\t--------\t--------"); 

     for (int i = 0; i < files.length; i++) { 
     FileReader fileReader = new FileReader(files[i]); 

     in = new BufferedReader(fileReader); 

     if (files.length == 1) { 
      countSingle(files[0], in); 
      in.close(); 
     } 
     else { 
     countMultiple(files[i], in); 
      System.out.println(currentLines + "\t\t" + 
        currentWords + "\t\t" + currentBytes + "\t\t" 
       + files[i]); 
     in.close(); 
     } 
     }  
     if (files.length > 1) { 
      System.out.println("----------------------------------------" + 
       "\n" + totalLines + "\t\t" + totalWords + "\t\t" +  totalBytes + "\t\tTotals"); 
     } 
     } 

     catch (FileNotFoundException ioe) { 
     System.out.println("The specified file was not found. Please recheck " 
       + "the spelling and try again."); 

     ioe.printStackTrace(); 

    } 

     catch (IOException ioe) { 

       ioe.printStackTrace(); 

      } 


} 
} // End of class 

是整個程序,如果有人幫助應該需要什麼都看不到,但是這是我算每串在一條線上的長度(我假定EOL字符可這個計數的一部分,但它們不是。)

public static void countMultiple(String fileName, BufferedReader in) { 

    try { 

     String line; 

     String[] words; 

     int totalWords1 = 0; 

     int lines = 0; 

     int chars = 0; 

     while ((line = in.readLine()) != null) { 

      lines++; 
      currentLines = lines; 

      **chars += line.length();** 
      currentBytes = chars; 

      words = line.split(" "); 

      totalWords1 += countWords(line); 
      currentWords = totalWords1; 


     } // End of while loop. 
     totalLines += currentLines;    
     totalBytes += currentBytes; 
     totalWords += totalWords1; 

    } catch (Exception ex) { 

     ex.printStackTrace(); 

    } 

} 

回答

0

BufferedReader總是忽略新的換行或換行符。使用readLine()無法做到這一點。

您可以改爲使用read()方法。但在這種情況下,你必須單獨閱讀每個字符。

+0

我可以做'chars + = in.read(); if(in.read()==''chars- = 1'? – Camdeazy

+0

你不能做'chars + = in.read()',因爲in.read()返回讀取字符的ascii代碼。可以做的是,檢查換行符(如您在評論中所說的): 'if(in.read()== 10)chars- = 1'; 其中10是ascii代碼行 – Rehman

0

只是一條評論,要將一行分割爲單詞,僅基於單個空格拆分是不夠的:line.split(" ");如果單詞之間存在多個空格或製表符,則會錯過。更好地做任何空白字符分割line.split("\\s+");

+0

感謝那一點信息,不知道 – Camdeazy

+0

我試過使用這個,但它仍然只讀取它作爲一個字節。我把一個選項卡在那裏,計數只增加了1,我做錯了什麼? – Camdeazy

+0

它仍然讀取什麼作爲一個字節?你把一個標籤在哪裏?什麼是不工作?請詳細說明例子... –

相關問題