简体   繁体   English

使用scanner.Delimiter错误地读取输入文件的第一个字符

[英]First character of input file read incorrectly using scanner.useDelimiter

Im trying to read from an input file using useDelimiter function. 我试图使用useDelimiter函数从输入文件中读取。 My files include "/ and \\r\\n" as separator. 我的文件包含“/和\\ r \\ n”作为分隔符。 Naturally, I went for scanner.useDelimiter("/|\\r\\n") but the output returned is funny, specifically the first character of the file 当然,我去了scanner.Delimiter(“/ | \\ r \\ n”),但返回的输出很有趣,特别是文件的第一个字符

The code is as below: 代码如下:

Scanner readfile = new Scanner(new File("text.txt")).useDelimiter("/|\r\");          
while(readfile.hasNext()) {                              
    System.out.println(readfile.next());
}

The input file is as below: 输入文件如下:

Dr A/P0001/N28-201/012-3465789/1
Dr B/P0002/D03-356/013-3334445/3
Dr C/SP0001/K12-311/014-9988655/4
Dr D/SP0002/T09-101/018-8888333/2
Dr E/P0003/L34-213/014-6655241/0

The output returned: 输出返回:

锘緿r A
P0001
N28-201
012-3465789
1
Dr B
P0002
D03-356
013-3334445
3
Dr C
SP0001
K12-311
014-9988655
4
Dr D
SP0002
T09-101
018-8888333
2
Dr E
P0003
L34-213
014-6655241
0

Can somebody help? 有人可以帮忙吗? I've been searching the internet for one whole week. 我一直在互联网上搜索一整周。 Thanks in advance. 提前致谢。

Try changing your code and use BufferedReader. 尝试更改代码并使用BufferedReader。

   public static void main(String[] args)  {
      try {
         BufferedReader in = new BufferedReader(new FileReader("c:\\filename"));
         String str;

         while ((str = in.readLine()) != null) {
            System.out.println(str);
         }
         System.out.println(str);
      } catch (IOException e) {
      }
   }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM