How to make Scanner Properly Read Escape Characters?

不打扰是莪最后的温柔 提交于 2019-11-28 04:04:33

问题


I'm reading from a file that reads something like all on one line:

Hello World!\nI've been trying to get this to work for a while now.\nFrustrating.\n

And my Scanner reads that from the file and puts it in a String:

Scanner input = new Scanner(new File(fileName));
String str = input.nextLine();
System.out.print(str);

Now, I want the output then to be:

Hello World!
I've been trying to get this work for a while now.
Frustrating.

But instead I'm getting the exact same thing as the input. That is, each \n is included in the output and everything is on one line instead of separate lines.

I thought that Scanner would be able to read the escape character properly but it's instead copying it onto the String like it's \\n.


回答1:


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



回答2:


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.




回答3:


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.


来源:https://stackoverflow.com/questions/10830366/how-to-make-scanner-properly-read-escape-characters

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