[英]How to count characters in DOS text file including line ending?
I'm trying count the number of characters in a text file.我正在尝试计算文本文件中的字符数。 However, I can only count the letters and whitespace, not \\r\\n at the end of the line.
但是,我只能计算行尾的字母和空格,而不是 \\r\\n。 How can I include it?
我怎样才能包括它? The function below counts the number of lines, words, and characters in a file.
下面的函数计算文件中的行数、单词数和字符数。
public static void Count(String FILENAME, int n) throws IOException {
inFile = new BufferedReader(new FileReader(FILENAME));
String currentLine; //= inFile.readLine();
while ((currentLine=inFile.readLine()) != null) {
lines[n]++;
bytes[n]+=currentLine.length();
bytes[n]++;
String[] WORDS = currentLine.split(" "); // split the string into sub-string by whitespace
// to separate each words and store them into an array
words[n] = words[n] + WORDS.length;
if (currentLine.length()==0)
words[n]--;
}
}
One simple way would be to utilize Character Streams instead of Line-oriented Streams since character streams give you one character each time you call readLine().一种简单的方法是使用字符流而不是面向行的流,因为每次调用 readLine() 时,字符流都会为您提供一个字符。
public static void Count(String FILENAME, int n) throws IOException { inFile = new FileReader(FILENAME); char currentCharacter; int numCharacters = 0; String currentLine = ""; while ((currentCharacter=inFile.readLine()) != null){ if(currentCharacter == '\\n') { lines[n]++; bytes[n]+=currentLine.length(); bytes[n]++; String[] WORDS = currentLine.split(" "); words[n] = words[n] + WORDS.length; if (currentLine.length()==0) words[n]--; } currentCharacter=inFile.readLine(); currentLine += currentCharacter; numCharacters ++; }
And then the sum would be stored in numCharacters.然后总和将存储在 numCharacters 中。 To retain the ability to count lines and bytes and such, you could have a String line declared before the loop and concatenate each character to the end of it in the loop.
为了保留对行数和字节数等进行计数的能力,您可以在循环之前声明一个字符串行,并将每个字符连接到循环中的末尾。 Once you hit a \\n, you know that you have one line in the line variable.
一旦你点击了 \\n,你就知道 line 变量中有一行。 Then u could increment line[n] by one, increase bytes[n] by line.length(), etc.
然后你可以将 line[n] 增加 1,通过 line.length() 增加 bytes[n],等等。
source of info: https://docs.oracle.com/javase/tutorial/essential/io/charstreams.html信息来源: https : //docs.oracle.com/javase/tutorial/essential/io/charstreams.html
for counting \\r\\n use read() method.计数 \\r\\n 使用 read() 方法。
readLine() method Reads a line of text. readLine() 方法 读取一行文本。 A line is considered to be terminated by any one of a line feed ('\\n'), a carriage return ('\\r'), or a carriage return followed immediately by a linefeed.
一行被视为以换行符 ('\\n')、回车符 ('\\r') 或回车符后紧跟换行符中的任何一个结束。
Returns: A String containing the contents of the line, not including any line-termination characters, or null if the end of the stream has been reached.返回: 包含行内容的字符串,不包括任何行终止字符,如果已到达流的末尾,则返回 null。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.