Java:为什么我不能阅读和比较文本文件?

时间:2016-02-28 05:24:32

标签: java netbeans

我尝试做的是在用户从组合框中选择项目并单击按钮后,将运行案例2并且txtcp将文本框设置为RK1314并将其保存到文本文件中

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!");
        }

按下按钮后,没有任何反复发生。

3 个答案:

答案 0 :(得分:1)

您的代码似乎正在寻找一条完全等于" RK1234&#34 ;;即

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是"板",那么它的线将是" Car plate:RK1234" ......不等于" RK1234"。

所以当你按下按钮时:

  • 打开文件。
  • 它读取的每一行都没有匹配(因此没有对话框)
  • 它不会抛出I / O异常(因此没有" Hello")对话框。
  • 它到达文件的末尾,关闭它并完成。

简而言之,您不会看到任何对话框。

也许您应该测试一行是否包含该字符串;例如

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

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

答案 1 :(得分:0)

根据你的问题。该文件包含这样的权利。 客户姓名:宝贝 客户编号:1234 订单号:RK123。 所以,当你在阅读文件时  它是逐行阅读的 。所以请不要使用equals方法。使用包含方法。它会给你结果。

答案 2 :(得分:-1)

可能的原因是您的文件没有被写入。

根据PrintWriter PrintWriter(Writer)构造函数创建一个新的PrintWriter,没有自动行刷新。因此,要么您需要在out.flush()之前致电out.close(),要么创建PrintWriter使用this控制器,您可以使用该控件指定' true' to autoFlush参数。

目前正在发生的是你的文件没有被写入。