How to make Scanner Properly Read Escape Characters?

北战南征 提交于 2019-11-29 10:34:36

If \n is written is the file you can't use nextLine() because there is not \n (end of line) but instead there is \\n (two characters).

Instead try with a delimiter :

    Scanner sc = new Scanner(new File("/home/alain/Bureau/ttt.txt"));
    sc.useDelimiter("\\\\n");
    while(sc.hasNext()){
        System.out.println(sc.next());
    }

Output :

Hello World!

I've been trying to get this to work for a while now.

Frustrating.

EDIT:

If you want to read the file and replace the \n in the text with actual EOL. You can simply use :

Scanner sc = new Scanner(new File("/home/alain/Bureau/ttt.txt"));

//loop over real EOL
while(sc.hasNextLine()){

     //Replace the `\n` in the line with real EOL.
     System.out.println(sc.nextLine().replace("\\n", System.getProperty("line.separator")));
}
aioobe

No, Scanner won't do that for you. You'll have to do the translation yourself.

(Note that if you use something like sc.useDelimiter("\\\\n") as others have suggested you're breaking the functionality of the ordinary next() method and nextLine() may not function as expected.)

Here's a sketch of how I would solve it:

Change

Scanner input = new Scanner(new FileReader(fileName));

to

Scanner input = new Scanner(new JavaEscapeReader(new FileReader(fileName)));
                            ^^^^^^^^^^^^^^^^^^^^^                        ^

where JavaEscapeReader would extend FilterReader like this:

class JavaEscapeReader extends FilterReader {

    JavaEscapeReader(Reader in) {
        super(in);
    }

    @Override
    public int read() throws IOException {
        int ch = super.read();
        switch (ch) {
        case '\\':
            switch (super.read()) {
            case '\\': return '\\';
            case 'n': return '\n';
            case 't': return '\t';
            case 'f': return '\f';
            // ...
            default:
                throw new IOException("Invalid char sequence.");
            }
        default:
            return ch;
        }
    }

    @Override
    public int read(char[] cbuf, int off, int len) throws IOException {
        int i = 0, ch;
        while (i < len && -1 != (ch = read()))
            cbuf[i++] = (char) ch;
        return i == 0 ? -1 : i;
    }
}

Given an input file with the content

Line1\nLine2
Line3\nLine3

the program

Scanner sc = new Scanner(new JavaEscapeReader(new FileReader("filename.txt")));
while (sc.hasNextLine())
    System.out.println(sc.nextLine());

prints

Line1
Line2
Line3
Line4

Another option is to use StringEscapeUtils.unescapeJava and post process the read strings.

You can use Scanner.useDelimiter to set your own delimiter. In your case using double quoted \\n:

s.useDelimiter("\\\\n");

Example:

Scanner s = new Scanner("Hello World!\\nI've been trying to get this to " +
                        "work for a while now.\\nFrustrating.\\n");
s.useDelimiter("\\\\n");

System.out.println(s.next());
System.out.println(s.next());
System.out.println(s.next());

Outputs:

Hello World!
I've been trying to get this to work for a while now.
Frustrating.
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!