Java: Why is \n considered 2 characters in a read text file situation?

大城市里の小女人 提交于 2021-01-28 19:19:19

问题


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);

}

}

Considering that xxx.txt contains:

a
b b
cccd

I'm just confused at why "next line" is considered 2 characters? I manually counted 10 characters (including white space) but the result is a 12.

Thank you.


回答1:


  • It is because windows uses 2 characters \r\n to go to a new line i.e \r(carriage return) and \n(newline feed)
  • *nix (Unix like) based systems such as BSD, Linux use only \n for newline
  • Mac uses only \r

Carriage return moves the cursor to the beginning of the line whereas \n moves the cursor to the next line.

Quoting from Wikipedia(https://en.wikipedia.org/wiki/Newline):

  • LF: Multics, Unix and Unix-like systems (Linux, OS X, FreeBSD, AIX, Xenix, etc.), BeOS, Amiga, RISC OS, and others
  • CR: Commodore 8-bit machines, Acorn BBC, ZX Spectrum, TRS-80, Apple II family, Oberon, Mac OS up to version 9, and OS-9
  • RS: QNX pre-POSIX implementation
  • 0x9B: Atari 8-bit machines using ATASCII variant of ASCII (155 in decimal)
  • CR+LF: Microsoft Windows, DOS (MS-DOS, PC DOS, etc.), DEC TOPS-10, RT-11, CP/M, MP/M, Atari TOS, OS/2, Symbian OS, Palm OS, Amstrad CPC, and most other early non-Unix and non-IBM OSes
  • LF+CR: Acorn BBC and RISC OS spooled text output.

Hence to conclude line encoding differ as per the OS family.




回答2:


i tested your method a new line is considered not as a one single char its actually considered as 2 characters you can test this in my code try commenting out the line saying "printing each character line by line" by the way if you want to trim the whitespaces and actually get the word count i've done that reffer to my example . inside the while loop you have written its iterating the count bt doesnt give the exact output replace count++ with ++count.

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());


来源:https://stackoverflow.com/questions/36760209/java-why-is-n-considered-2-characters-in-a-read-text-file-situation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!