2015-11-30 94 views
0

我試圖讀出一個文件。Java - 掃描器分隔符 - '懸掛元字符'+'接近索引0'

該文件具有特定對象的多個屬性,這與現在不相關。

但是這些屬性是用「+」號分開的。

現在,當我嘗試讀取出來,並使用了+分隔符,我得到的錯誤:Dangling meta character '+' near index 0

注:請用一個「1」(在數據庫對象的ID)開始,而不是用 '+'

這是代碼我一直使用:

public void doImport() throws FileNotFoundException, IOException{ 

    file = new File(document); 
    Scanner fileIn = new Scanner(file); 

    while(fileIn.hasNextLine()){ 

     //reading a single line of the file 
     String line = fileIn.nextLine(); 
     Scanner scan = new Scanner(line); 

     //setting the delimiter 
     scan.useDelimiter("+"); 

     while(scan.hasNext()){ 
      //printing contents, split by a + 
      System.out.println(scan.next()); 
     } 

     String string = fileIn.nextLine(); 
     System.out.println(string); 
    } 
    fileIn.close(); 
} 

該文件的內容:fileContent

回答

2

分隔符EXPEC因此在這種情況下,您應該使用: scan.useDelimiter("\\+");

正則表達式中的+號表示您想要1個或更多匹配,例如[a-z]+表示從a到z的一個或多個字母。