字符计数,Java程序和wc产生不一致的结果

问题描述

我编写了一个Java程序来计算文件中的字符数。要检查程序是否正常运行,我在命令行(linux)中键入以下命令以检查字符数:

wc -m fileName

wc的手册页中,我知道换行符已包含在计数中。

这是我的Java程序:

import java.io.IOException;
import java.io.File;
import java.util.Scanner;

public class NumOfChars {
  /** The main method. */
  public static void main(String[] args) throws IOException {
    // Check that command is entered correctly
    if (args.length != 1) {
      System.out.println("Usage: java NumOfChars fileName");
    }

    // Check that source file exists
    File file = new File(args[0]);
    if (!file.exists()) {
      System.out.printf("File %s does not exist\n",file);
    }

    // Create Scanner object
    Scanner input = new Scanner(file);

    int characters = 0;
    while (input.hasNext()) {
      
      String line = input.nextLine();

      // The number of characters is the length of the line plus the newline character
      characters += line.length() + 1;
    }
    input.close();

    // Print results
    System.out.printf("File %s has\n",args[0]);
    System.out.printf("%d characters\n",characters);
  }
}

我遇到的问题是,有时使用java程序报告的字符数与使用wc命令时得到的字符数不同。

这里有两个例子:

一个可行的。文件text.txt内容

This is some text
This is some text
This is some text
This is some text
This is some text
This is some text
This is some text
This is some text

命令wc -m text.txt告诉我该文件具有 144 个字符。这很好,因为当我执行Java程序java NumOfChars text.txt时,我还被告知文件具有 144 个字符。

一个无效的。文件Exercise06.java内容

import java.util.Scanner;
import java.util.regex.Pattern;
import java.util.regex.Matcher;

/** Converts a hexadecimal to a decimal. */
public class Exercise06 {
  /** Main method */
  public static void main(String[] args) {
    // Create a Scanner
    Scanner input = new Scanner(system.in);

    // Prompt the user to enter a string
    System.out.print("Enter a hex number: ");
    String hex = input.nextLine();
    
    // display result
    System.out.println("The decimal value for hex number "
      + hex + " is " + hexToDecimal(hex.toupperCase()));
  }
  

  /** Converts hexadecimal to decimal.
      @param hex The hexadecimal
      @return The deciaml value of hex
      @throws NumberFormatException if hex is not a hexadecimal
    */
  public static int hexToDecimal(String hex) throws NumberFormatException {
    // Check if hex is a hexadecimal. Throw Exception if not.
    boolean patternMatch = Pattern.matches("[0-9A-F]+",hex);
    if (!patternMatch) 
      throw new NumberFormatException();

    // Convert hex to a decimal
    int decimalValue = 0;
    for (int i = 0; i < hex.length(); i++) {
      char hexChar = hex.charat(i);
      decimalValue = decimalValue * 16 + hexCharToDecimal(hexChar);
    }
    // Return the decimal
    return decimalValue;
  }
  
  
  /** Converts a hexadecimal Char to a deciaml.
      @param ch The hexadecimal Char
      @return The decimal value of ch
    */
  public static int hexCharToDecimal(char ch) {
    if (ch >= 'A' && ch <= 'F')
      return 10 + ch - 'A';
    else // ch is '0','1',...,or '9'
      return ch - '0';
  }
}

命令wc -m Exercise06.java告诉我该文件具有 1650 个字符。但是,当我执行Java程序java NumOfChars Exercise06.java时,被告知文件具有 1596 个字符。

我似乎无法弄清楚我在做什么错。谁能给我一些反馈?

**编辑:这是我在输入head -5 Exercise06.java | od -c时得到的信息

enter image description here

解决方法

有几种可能的解释:

  • 每行可能以多个字符结尾,例如在Windows上,每行以CR + LF结尾,而您的程序始终只计入1个行结束字符。

  • wc可能采用与程序不同的字符编码,可能导致多字节字符的字符计数不同。