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

Java:为什么我不能读取和比较文本文件?,java,netbeans,Java,Netbeans,我试图做的是,在用户从combobox中选择项目并单击按钮后,案例2将运行,txtcp将文本框设置为RK1314,并将其保存到文本文件中 case 2: if (sportcb.getSelectedItem().equals("Ferrari F430 Scuderia")) { ... txtcp.setText("RK1314"); 因此,按下按钮后,我想读取并比较文本文件中的文本,如果文本文件中存在,

我试图做的是,在用户从combobox中选择项目并单击按钮后,案例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!");
        }

按下按钮后,什么也没有发生。

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

根据构造函数创建一个新的PrintWriter,无需自动换行。因此,您需要在out.close之前调用out.flush,或者在创建PrintWriter时使用contrustor,您可以使用contrustor为autoFlush参数指定“true”


目前,您的文件没有被写入。

您的代码似乎在寻找一行正好等于RK1234的代码;i、 e

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异常,因此没有Hello对话框。 它到达文件末尾,关闭文件并完成。 简而言之,您看不到任何对话框

也许您应该测试一行是否包含该字符串;e、 g

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


根据你的问题。该文件包含这样的权限。 客户名称:宝贝 客户号码:1234 订单号:RK123。 因此,当您读取文件时 它正在逐行阅读
. 所以请不要使用equals方法。使用contains方法。它将给出结果。

我尝试使用FileReader fr=new FileReaderfileName;BufferedReader br=新的BufferedReader r;但它仍然不起作用。编辑你的问题,将评论中添加的信息添加到你的问题文本中,然后删除评论。@RajenRaiyarela基本上没什么,我只是尝试了另一种方法,但最终结果仍然是一样的。问题可能存在于你在这里介绍的代码中,也可能不存在。您提到了GUI元素,但没有代码实际引用它们。请创建一个。这远远不是完整的或最小的。这是不正确的。out.close将在关闭前尝试刷新所有数据。如果不能,它将抛出异常。非常感谢您指出我的问题,我重新编辑了问题。即使您对问题进行了编辑,我的答案仍然有效。
if (line.contains(link)) { ...