如何在我附加到的文本文件中搜索特定条目?
我制作了一个 GUI 来输入数据,但是我可以读取整个文件,但这对我没有用,因为我从用户的角度思考,如果想要调用更早的预订,我对了解如何做到这一点。当我读回数据时,我正在尝试实例化一个对象。
这是我的一些代码:
search.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
String booking;
String where;
booking = JOptionPane.showInputDialog(" Booking Reference: ");
System.out.println("Booking Reference " + e.getActionCommand());
if (booking.equals("Wales"))
try{
FileInputStream fstream = new FileInputStream("Wales.txt");
// Use DataInputStream to read binary NOT text.
BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
String strLine;
while ((strLine = br.readLine()) != null) {
System.out.println (strLine);
}
in.close();
}
catch (Exception ex)
{
System.err.println("Error: " + ex.getMessage());
}
else if (booking.equals("Scotland"))
try{
FileInputStream fstream = new FileInputStream("Scotland.txt");
DataInputStream in = new DataInputStream(fstream);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String strLine;
while ((strLine = br.readLine()) != null) {
System.out.println (strLine);
}
where = JOptionPane.showInputDialog("Which booking reference do you wish to search?: ");
}
catch (Exception ex)
{
System.err.println("Error: " + ex.getMessage());
}
}
});