0

我不是经验丰富的 Java 程序员,我正在尝试将一些文本写入文件,然后使用 Scanner 读取它。我知道有很多方法可以做到这一点,但我想用分隔符将记录写入文件,然后阅读这些片段。

问题是如此之小。当我查看输出时,看不到一些打印(如下所示)。我的意思是输出中的粗线仅写为“扫描仪”。如果有人能回答为什么那里没有看到“字符串: ”,我将不胜感激。(请回答我的问题)

我不明白这是一个简单的打印问题还是“\r\n”的行尾问题。

这是代码:


    import java.io.FileReader;
    import java.io.FileWriter;
    import java.io.IOException;
    import java.util.Scanner;

public class Tmp {
    public static void main(String args[]) throws IOException {
        int i;
        boolean b;
        String str;

        FileWriter fout = new FileWriter("test.txt");
        fout.write("Testing|10|true|two|false\r\n");
        fout.write("Scanner|12|one|true|");
        fout.close();

        FileReader fin = new FileReader("Test.txt");

        Scanner src = new Scanner(fin).useDelimiter("[|\\*]");

        while (src.hasNext()) {
            if (src.hasNextInt()) {
                i = src.nextInt();
                System.out.println("int: " + i);
            } else if (src.hasNextBoolean()) {
                b = src.nextBoolean();
                System.out.println("boolean: " + b);
            } else {
                str = src.next();
                System.out.println("String: " + str);
            }
        }

        fin.close();
    }
}   

这是输出:


String: Testing
int: 10
boolean: true
String: two
String: false
Scanner
int: 12
String: one
boolean: true

4

2 回答 2

1

您没有正确设置分隔符;[|\\*]是由 2 个字符组成的字符类,|, *.

    String s = "hello|world*foo*bar\r\nEUREKA!";
    System.out.println(s);
    // prints:
    // hello|world*foo*bar
    // EUREKA!

    Scanner sc;

    sc = new Scanner(s).useDelimiter("[|\\*]");
    while (sc.hasNext()) {
        System.out.print("[" + sc.next() + "]");
    }
    // prints:
    // [hello][world][foo][bar
    // EUREKA!]

    System.out.println();

    sc = new Scanner(s).useDelimiter("\r?\n|\r|\\|");
    while (sc.hasNext()) {
        System.out.print("[" + sc.next() + "]");
    }
    // prints:
    // [hello][world*foo*bar][EUREKA!]      

您似乎已经发现"[|\\n]"“有效”,但这实际上\r在某些标记的末尾留下了尾随。

巧合的是,你应该抬头看PrintWriter;它有println使用系统属性的方法line.separator。它基本上是什么System.out

PrintWriter fout = new PrintWriter(new File("test.txt"));
fout.println("Testing|10|true|two|false");
于 2010-04-15T23:52:14.313 回答
0

The problem is that you are writing out the String "String: " and then writing out control character \r, or carriage return, and then writing out the contents.

The following version should work a bit better for you:

FileWriter fout = new FileWriter("test.txt");
fout.write("Testing|10|true|two|false\n");
fout.write("Scanner|12|one|true|");
fout.close();
FileReader fin = new FileReader("test.txt");
Scanner src = new Scanner(fin).useDelimiter("[|\n]");

To really see what I am talking about with the \r, you should change your original program so the print code looks like this:

  } else {
    str = src.next().trim();
    str = str.replace('\n', '_');
    str = str.replace('\r', '_');
    System.out.println("String: " + str);
  }

You should see the output:

String: false__Scanner

于 2010-04-15T22:07:47.067 回答