A: 

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

Tim Perry
thanks man, when i replaced with that:Scanner src = new Scanner(fin).useDelimiter("[|\\n]");it is solved, your answer helped me a lot, thanks again..
No problem...I'd never heard of the Scanner class and now I can write more concise code :)
Tim Perry
+1  A: 

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