2016-02-28 116 views
0

我想要做的是用戶選擇後從組合框項目,點擊一個按鈕,案例2將運行,並且將txtcp文本框設置爲RK1314,並將其保存到文本文件Java:爲什麼我無法讀取和比較文本文件?

case 2: if (sportcb.getSelectedItem().equals("Ferrari F430 Scuderia")) 
      { 
       ... 
       txtcp.setText("RK1314"); 

所以在按下按鈕之後,我想要讀取和比較文本,如果它出現在文本文件中,則會出現消息。

 String line; 
     String fileName = "test.txt"; 
     String link = txtcp.getText(); 
     BufferedReader br = null; 
     try { 
      br = new BufferedReader(new FileReader(fileName)); 

      while((line=br.readLine()) != null) { 
      if(line.equals(link)) 
       JOptionPane.showMessageDialog(this," identical match found in the text file!"); 
      } 
      br.close(); 
     } 
     catch(IOException e) {  
      JOptionPane.showMessageDialog(this,"hello"); 
     } 

這裏是我寫的文本文件代碼:

 File data = new File("test.txt"); 

     try { 
      String cname = name.getText(); 
      String sdate = startdate.getDate().toString(); 
      String edate = enddate.getDate().toString(); 

      if (data.exists() == false) { 
      System.out.println("We had to make a new file."); 
      data.createNewFile(); 
      } 
      PrintWriter out = new PrintWriter(new FileWriter(data, true)); 
      out.append("Customer name: "+cname); 
      out.append(System.lineSeparator()); 
      out.append("Contact Number: "+cn); 
      out.append("Car plate: "+plate); 
      out.append(System.lineSeparator()); 
      out.append("---------------------------------------------------------------------------------------------------------------------"); 
      out.append(System.lineSeparator()); 
      JOptionPane.showMessageDialog(this, "Order is Recorded!"); 
      out.close(); 
     } catch(IOException e) { 
      JOptionPane.showMessageDialog(this, "Order is not Recorded!"); 
     } 

按下按鈕後,什麼都沒有發生。

+0

我試過使用FileReader fr = new FileReader(fileName); BufferedReader br = new BufferedReader(f r); 但它仍然不會工作 – user63566

+0

pl編輯您的問題,並添加評論中添加的信息到您的問題文本並刪除評論。 –

+0

@RajenRaiyarela基本上沒什麼,我只是嘗試了一種不同的方式,但最後結果仍然是一樣的 – user63566

回答

1

您的代碼似乎在尋找一條等於「RK1234」的線條;即

if (line.equals(link)) { ... 

然而,用於寫入文件的代碼輸出該數據是這樣的:

 out.append("Customer name: "+cname); 
     out.append(System.lineSeparator()); 
     out.append("Contact Number: "+cn); 
     out.append(System.lineSeparator()); 
     out.append("Car plate: "+plate); // Updated ... 
     out.append(System.lineSeparator()); 
     out.append("---------------------------------------" + 
       "---------------------------------------" + 
       "---------------------------------------"); 
     out.append(System.lineSeparator()); 

的4行中沒有通過上述生產可可能等於「RK1234」。假設RK1234是「板」,那麼該線將是「車牌:RK1234」......這不等於「RK1234」。

所以,當你按下按鈕:

  • 它打開文件。
  • 它讀取每一行都沒有匹配(所以沒有對話框)
  • 它不會拋出I/O異常(所以沒有「你好」)對話框。
  • 它到達文件末尾,關閉它並完成。

總之,你不要看到任何對話框。

也許你應該測試如果一行包含那個字符串;例如

if (line.indexOf(link) >= 0) { ... 

if (line.contains(link)) { ... 
+0

非常感謝您指出我的問題,我重新編輯了這些問題。 – user63566

+0

即使在對問題進行編輯之後,我的答案仍然存在。 –

-1

可能的原因是您的文件沒有寫入。

根據PrintWriterPrintWriter(Writer)構造函數創建一個新的PrintWriter,不會自動行刷新。因此,無論您需要在out.close()之前致電out.flush()還是創建PrintWriter使用this控制器,您可以使用該控制器指定autoFlush參數的「true」。

目前發生的事情是您的文件沒有被寫入。

+0

這是不正確的。 'out.close()'會在關閉之前刷新所有數據。如果它不能,它會拋出一個異常。 http://docs.oracle.com/javase/8/docs/api/java/io/OutputStreamWriter.html#close-- –

0

根據你的問題。該文件包含這樣的權利。 客戶名稱:寶貝 客戶編號:1234 訂單編號:RK123。 所以,當你正在閱讀文件 它正在逐行閱讀 。所以請不要使用equals方法。使用包含方法。它會給你結果。