用Java编写了一个wc程序,但它不会计算行尾字符

时间:2015-09-24 07:52:30

标签: java eol wc

所以我已经完成了(我认为)Java中的wc程序,它从用户输入(甚至是多个)中获取文件名,并对文件中的行,单词,字节(字符数)进行计数。提供了2个用于测试目的的文件,它们采用.dat格式,可从dos / linux命令行读取。除了在行尾有\ n或\ r \ n字符的计数外,一切都正常工作。它不会算这些。请帮帮忙?

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字符将是这个计数的一部分,但它们不是& #39; t)的

  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();

    }

}

2 个答案:

答案 0 :(得分:0)

BufferedReader始终忽略换行或换行符。使用readLine()无法做到这一点。

您可以改用read()方法。但在这种情况下,你必须单独阅读每个角色。

答案 1 :(得分:0)

只是评论,将一行分为单词,仅基于单个空格进行拆分是不够的:line.split(" ");如果单词之间有多个空格或制表符,您将会错过。最好在任何空白字符line.split("\\s+");

上进行拆分