开发者

How to resolve Line End Reading with Scanner Class in Java

I am not an experienced Java programmer and i'm trying to write some text to a file and then read it with Scanner. I know there are lots 开发者_Python百科of ways of doing this, but i want to write records to file with delimiters, then read the pieces.

The problem is so small. When I look the output some printing isn't seen(shown in below). I mean the bold line in the Output that is only written "Scanner". I would be appreciated if anyone can answer why "String: " isn't seen there. (Please answer just what i ask)

I couldn't understand if it is a simple printing problem or a line end problem with "\r\n".

Here is the code:


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

Here is the output:


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


You're not setting your delimiter right; [|\\*] is a character class consisting of 2 characters, |, *.

    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!]      

You seemed to have found that "[|\\n]" "works", but this actually leaves a trailing \r at the end of some tokens.

Coincidentally, you should look up PrintWriter; it has println methods that uses the system property line.separator. It's basically what System.out is-a.

PrintWriter fout = new PrintWriter(new File("test.txt"));
fout.println("Testing|10|true|two|false");


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜