Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java:为什么在读取文本文件的情况下\n被视为2个字符?_Java_File_Count_Character_Reader - Fatal编程技术网

Java:为什么在读取文本文件的情况下\n被视为2个字符?

Java:为什么在读取文本文件的情况下\n被视为2个字符?,java,file,count,character,reader,Java,File,Count,Character,Reader,} 考虑到xxx.txt包含: import java.io.*; public class xxx { public static void main(String[] args) throws IOException { FileReader fs = new FileReader("xxx.txt"); int t = fs.read(); int count = 0; while (t!=-1) { count++;

}

考虑到xxx.txt包含:

import java.io.*;

public class xxx {

public static void main(String[] args) throws IOException {
    FileReader fs = new FileReader("xxx.txt");
    int t = fs.read();
    int count = 0;

    while (t!=-1) {
        count++;
        t = fs.read();
    }
    System.out.println(count);

}
我只是不明白为什么“下一行”被认为是两个字符?我手动计算了10个字符(包括空格),但结果是12个字符

谢谢。

  • 这是因为windows使用2个字符
    \r\n
    转到新行,即
    \r
    (回车)和
    \n
    (换行符)
  • *基于nix(类Unix)的系统,如BSD、Linux,只对换行符使用
    \n
  • Mac仅使用
    \r
回车将光标移动到行首,而
\n
将光标移动到下一行

引用维基百科():

  • LF:Multics、Unix和类Unix系统(Linux、OS X、FreeBSD、AIX、Xenix等)、BeOS、Amiga、RISC OS和其他
  • CR:Commodore 8位机器、Acorn BBC、ZX Spectrum、TRS-80、Apple II系列、Oberon、Mac操作系统至版本9和操作系统9
  • RS:QNX预POSIX实现
  • 0x9B:Atari 8位机器使用ATASCII ASCII变体(十进制155)
  • CR+LF:Microsoft Windows、DOS(MS-DOS、PC-DOS等)、12月TOPS-10、RT-11、CP/M、MP/M、Atari TOS、OS/2、Symbian OS、Palm OS、Amstrad CPC、, 以及大多数其他早期的非Unix和非IBM操作系统
  • LF+CR:Acorn BBC和RISC OS假脱机文本输出

因此,要得出结论,行编码因操作系统系列而异。

我测试了您的方法。新行不被视为一个字符,而实际上被视为两个字符。您可以在我的代码中测试这一点,尝试注释该行,说“逐行打印每个字符” 顺便说一句,如果你想删减空白,并实际获得字数,我已经参照我的例子做了。在while循环中,您已经编写了它的迭代计数bt,但它没有给出确切的输出替换
count++
++count

a
b b
cccd
FileReader fs=newfilereader(“src/hackerrank/xxx.txt”);
int t=fs.read();
整数计数=0;
StringBuffer字=新的StringBuffer();
列表字符=新的ArrayList();
而(t!=-1){
字符。添加((字符)t);
++计数;
t=fs.read();
}
系统输出打印项次(计数);
用于(字符aChar:chars){
//System.out.println(aChar);逐行打印每个字符
if(字符.isWhitespace(aChar)){
//忽略空白
}否则{
append(aChar);//添加不带任何空格的输入
}
}
System.out.println(word.length());
FileReader fs = new FileReader("src/hackerrank/xxx.txt");
    int t = fs.read();
    int count = 0;
    StringBuffer word = new StringBuffer();
    List<Character> chars = new ArrayList<Character>();

    while (t!=-1) {
        chars.add((char)t);
        ++count;
        t = fs.read();
    }

    System.out.println(count);

    for (Character aChar : chars) {
        //System.out.println(aChar); printing each character line by line 
        if (Character.isWhitespace(aChar)) {
        //ignoring the white spaces   
        }else{
        word.append(aChar);//adding the input without any whitespaces
        }

    }

    System.out.println(word.length());