8

我正在从一个文件中读取,该文件在一行上读取类似 all 的内容:

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

我的扫描仪从文件中读取并将其放入字符串中:

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

现在,我希望输出为:

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

但相反,我得到与输入完全相同的东西。也就是说,每个 \n 都包含在输出中,并且所有内容都在一行而不是单独的行上。

我认为 Scanner 能够正确读取转义字符,但它会将其复制到字符串上,就像它是 \\n。

4

3 回答 3

4

如果\n写入的是您不能使用的文件,nextLine()因为没有\n(行尾)而是有\\n(两个字符)。

而是尝试使用分隔符:

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

输出 :

你好世界!

我一直在努力让它工作一段时间。

令人沮丧。

编辑:

如果您想阅读文件并将\n文本中的 替换为实际的 EOL。您可以简单地使用:

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")));
}
于 2012-05-31T09:08:57.130 回答
3

不,Scanner不会为你这样做。您必须自己进行翻译。

(请注意,如果您使用sc.useDelimiter("\\\\n")其他人建议的方法,您将破坏普通next()方法的功能,并且nextLine()可能无法按预期运行。)

这是我将如何解决它的草图:

改变

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

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

哪里JavaEscapeReader会像这样延伸FilterReader

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

给定一个包含内容的输入文件

Line1\nLine2
Line3\nLine3

该程序

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

印刷

Line1
Line2
Line3
Line4

另一种选择是使用StringEscapeUtils.unescapeJava和后处理读取的字符串。

于 2012-05-31T08:59:57.397 回答
1

您可以使用Scanner.useDelimiter来设置自己的分隔符。在您使用双引号的情况下\\n

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

例子:

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

输出:

Hello World!
I've been trying to get this to work for a while now.
Frustrating.
于 2012-05-31T09:06:47.707 回答