我正在创建一个存储书籍,书籍ID和书籍类型的程序。
我能够根据输入从文件中删除行,但是因为这可能导致用户删除我想要的整个类型的书籍,因此用户需要输入书名和书ID。在同一行中由#分隔,例如JamesandtheGiantPeach#01#Family。有没有办法让我使用多输入JOptionPane.showInputDialog
,这样它就要求用户同时输入书名和ID,然后删除两行中的一行?
我环顾四周,我找到了使用扫描仪类的答案,但不知道如何使用JOptionPane
来完成。但是,我知道如何创建一个多输入窗格。
static void removeMovie() throws IOException {
try {
File inFile = new File("books.txt");
if (!inFile.isFile()) {
System.out.println("Parameter is not an existing file");
return;
}
File tempFile = new File("books.txt" + "1");
BufferedReader br = new BufferedReader(new FileReader(inFile));
PrintWriter pw = new PrintWriter(new FileWriter(tempFile));
String line = null;
String removeLine = JOptionPane.showInputDialog("Enter line to remove");
while ((line = br.readLine()) != null) {
if (!line.trim().contains(removeLine)) {
pw.println(line);
pw.flush();
}
}
pw.close();
br.close();
//Delete the original file
if (!inFile.delete()) {
System.out.println("Could not delete file");
return;
}
if (!tempFile.renameTo(inFile)) {
System.out.println("Could not rename file");
}
} catch (FileNotFoundException ex) {
ex.printStackTrace();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
答案 0 :(得分:2)
我建议使用JDialog
。创建扩展JDialog
的类。在本课程中,您可以创建任何想要的内容。 JDialog具有JFrame和JDialog的构造函数,因此您可以将您的框架设置为父级,并在确认删除父级的fire方法后从文件中删除图书