我想保存我在文本字段中写的任何内容,并删除程序在单击搜索按钮后找到的行。为什么不动呢?下面是我的两个按钮:
private class dDelete implements ActionListener {
public void actionPerformed (ActionEvent e) {
HM.remove((String)result.getText());
}
}
private class sSave implements ActionListener {
public void actionPerformed (ActionEvent e) {
Set <String> ISet = HM.keySet();
Iterator itr = ISet.iterator();
String tuple = "";
java.io.File iwrite = new java.io.File("c:\\temp\\savetest.txt");
if (iwrite.exists()){
System.out.println("The file exists");
System.exit(0);
}
java.io.PrintWriter output = null;
try {
output = new java.io.PrintWriter(iwrite);
} catch(Exception ex) {
ex.printStackTrace();
}
while (itr.hasNext()) {
String Keys = (String)itr.next();
String val = HM.get(Keys);
tuple = Keys + " " + val;
output.print(tuple);
}
}
}发布于 2012-05-02 06:42:31
你应该关闭你的输出写入器output.close(),这可能就是原因。
您没有显示要添加到集合HM中的内容,因此很难判断删除是否有效。检查对HM.remove的调用的返回值,您将看到它是否成功,否则您使用了错误的键(在添加为移除时不使用相同/相等的键)。
除此之外,当您同时需要键和值(HM.entrySet())时,我建议在entrySet上迭代。
还要根据Java命名约定重命名类和变量(类以大写字母开头,实例变量不应以大写字母开头等)。了解更多信息。请参阅Java naming conventions。
https://stackoverflow.com/questions/10405325
复制相似问题