【发布时间】:2020-07-15 17:33:15
【问题描述】:
public static void Replace_Record(String editTerm, String newItem, String newAmount, String newPrice){
String filepath="temp_Food_Item.txt";
String tempfile= "temp_Food_Item_temp.txt";
File oldFile= new File(filepath);
File newFile=new File(tempfile);
String item=""; String quantity=""; String price="";
System.out.println("working ");
try{
//System.out.println("working pt1");
FileWriter fw= new FileWriter(tempfile,true);
BufferedWriter bw= new BufferedWriter(fw);
PrintWriter pw= new PrintWriter(bw);
x = new Scanner(new File(filepath));
x.useDelimiter("[,/n]");
//System.out.println("working pt2");
while(x.hasNext()){
//System.out.println("working pt3");
item=x.next();
quantity=x.next();
price=x.next();
if(item.equalsIgnoreCase(editTerm)){
pw.println(newItem+","+newAmount+","+newPrice);
}
else{
//System.out.println("working pt4 ");
pw.println(item+","+quantity+","+price);
}
}
x.close();
pw.flush();
pw.close();
oldFile.delete();
File dump=new File(filepath);
newFile.renameTo(dump);
}
catch(Exception e){
System.out.println("Error declared");
}
}
我不明白我哪里出错了,但它正在打印“错误声明”,所以我在工作 pt1 后调试并发现它停止并去捕捉请帮助? 附加信息包括: 我正在为一家餐馆创建一个数据库,并且我在 txt 文件中以 item_name,item_amount,item_price 的顺序输入信息,所以理论上我从 main 中获取我的新值并将它们传递给方法,它首先复制一个文件,直到它涉及我要删除的字符串,然后替换它们并返回以从真实文件中复制字符串。但每次我运行这个我都会被抓住。
TIA
【问题讨论】:
-
您应该打印
Exception的stackTrace而不仅仅是"Error declared",它永远不会告诉您到底发生了什么,但异常堆栈跟踪会做。 -
您可以使用 e.printStacktrace() 打印堆栈跟踪,这将打印到标准错误