簡體   English   中英

使用scanner.Delimiter錯誤地讀取輸入文件的第一個字符

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

我試圖使用useDelimiter函數從輸入文件中讀取。 我的文件包含“/和\\ r \\ n”作為分隔符。 當然,我去了scanner.Delimiter(“/ | \\ r \\ n”),但返回的輸出很有趣,特別是文件的第一個字符

代碼如下:

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

輸入文件如下:

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

輸出返回:

鍩緿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

有人可以幫忙嗎? 我一直在互聯網上搜索一整周。 提前致謝。

嘗試更改代碼並使用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