java - 计数字符、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 个字符。
我似乎无法弄清楚我做错了什么。谁能给我一些反馈?
解决方案
有几种可能的解释:
有可能每行以多个字符结尾,例如在 Windows 上,每行以 CR + LF 结尾,而您的程序总是精确计算 1 个行结束字符。
wc
可能假设与您的程序不同的字符编码,可能导致多字节字符的字符计数不同。
推荐阅读
- python - Pandas 如何根据所有行的值、应用于整个数据框的特定列值向数据框添加新列
- python - 如何根据用户对下拉列表的响应定义使用哪个文件?
- reactjs - 我应该在哪里调用 setintreval 反应
- c# - PostAsync 语句导致测试完成不确定
- java - Hibernate Criteria - 投影中的多种实体类型
- c# - Azure Web 应用程序和 Azure VM 上的 Windows 窗体应用程序之间的通信
- python - 这是 Python 中通常使用的/一种好的做法吗?
- python - 为什么站点匹配查询不存在?
- javascript - 如何在完整日历中选择全年的工作日或周末?
- jquery - Rails 设置 cookie 作为响应,但标头会丢失