Java 如何计算DOS文本文件中包括行尾的字符数?
我正在尝试计算文本文件中的字符数。但是,我只能计算字母和空格,不能计算\r\n行末尾的数字。我怎样才能把它包括进去? 下面的函数计算文件中的行数、字数和字符数Java 如何计算DOS文本文件中包括行尾的字符数?,java,Java,我正在尝试计算文本文件中的字符数。但是,我只能计算字母和空格,不能计算\r\n行末尾的数字。我怎样才能把它包括进去? 下面的函数计算文件中的行数、字数和字符数 public static void Count(String FILENAME, int n) throws IOException { inFile = new BufferedReader(new FileReader(FILENAME)); String currentLine; //= inFile.re
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]--;
}
}
要计数,\r\n请使用read()方法 readLine()方法读取一行文本。一行被认为是由换行符('\n')、回车符('\r')或紧接着换行符的回车符中的任意一个终止的 返回:
包含行内容的字符串,不包括任何行终止字符,如果已到达流的结尾,则为null。一种简单的方法是利用字符流而不是面向行的流,因为每次调用readLine()时,字符流都会给您一个字符
publicstaticvoidcount(字符串文件名,intn)引发IOException{
infle=新文件读取器(文件名);
字符流特征;
int numCharacters=0;
字符串currentLine=“”;
而((currentCharacter=infle.readLine())!=null){
如果(currentCharacter=='\n')
{
行[n]++;
字节[n]+=currentLine.length();
字节[n]++;
String[]WORDS=currentLine.split(“”);
单词[n]=单词[n]+单词长度;
如果(currentLine.length()==0)
单词[n]——;
}
currentCharacter=infle.readLine();
currentLine+=currentCharacter;
numCharacters++;
}
但是read()返回一个整数,我确实需要字符串将行拆分为单词