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

用Java编写了一个wc程序,但它不会计算行尾字符,java,eol,wc,Java,Eol,Wc,所以我已经用Java完成了(我想)我的wc程序,它从用户输入中获取一个文件名(甚至是多个),并从文件中计算行数、字数、字节数(字符数)。提供了两个用于测试的文件,它们是.dat格式的,可以从dos/linux命令行读取。除行末有\n或\r\n个字符时的计数外,所有操作都正常工作。这些都不算在内。请帮忙 import java.io.*; import java.util.regex.Pattern; public class Prog03 { private static int tota

所以我已经用Java完成了(我想)我的wc程序,它从用户输入中获取一个文件名(甚至是多个),并从文件中计算行数、字数、字节数(字符数)。提供了两个用于测试的文件,它们是.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

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


您可以改用read()方法。但在这种情况下,你必须逐个阅读每个字符。

仅仅是一条注释,要将一行拆分为单词,仅基于单个空格进行拆分是不够的:
行。拆分(“”)如果单词之间有多个空格或制表符,您将错过。最好对任何空白字符
行进行拆分。拆分(\\s+)

谢谢你提供的信息,我不知道我试过使用它,但它仍然只能读取一个字节。我在里面放了一个标签,计数只增加了1,我做错了什么?它还是读什么作为一个字节?你把账单放在哪里了?什么不起作用?请举例说明…尝试使用line.split(\\s+)而不是line.split(“”)。我为测试目的创建了一个SAMPL.TXT文件的开头和中间部分。实际上,您对分割输出的
字符串[]
数组不做任何操作。您可以使用自己编写的
countWords()
方法来执行完全不同的操作我可以执行类似
chars+=in.read()的操作吗;如果(in.read()==''chars-=1
?您不能执行
chars+=in.read()
,因为in.read()返回读取字符的ascii码。所以您可以做的是,检查换行符(正如您在注释中所说的那样):
如果(in.read()==10)chars-=1
;其中10是换行符的ascii码。
  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();

    }

}