Java 我的读者和作者在这种方法中是否没有正确关闭?
当我在插入新记录之前先删除记录时,我可以这样做,删除后我可以添加新记录。但是如果我先插入一条新记录,那么我的删除功能就不起作用了。根据我的研究,这主要是因为输入/输出没有正确关闭,但我已经这样做了,请看一下我的源代码谢谢 插入记录Java 我的读者和作者在这种方法中是否没有正确关闭?,java,Java,当我在插入新记录之前先删除记录时,我可以这样做,删除后我可以添加新记录。但是如果我先插入一条新记录,那么我的删除功能就不起作用了。根据我的研究,这主要是因为输入/输出没有正确关闭,但我已经这样做了,请看一下我的源代码谢谢 插入记录 public void RegCustomer() { try { File F = new File("Customer.txt"); FileWriter fw = new FileWri
public void RegCustomer()
{
try
{
File F = new File("Customer.txt");
FileWriter fw = new FileWriter(F, true);
BufferedWriter bw = new BufferedWriter(fw);
PrintWriter pw = new PrintWriter(bw);
//PrintWriter pw = new PrintWriter(new BufferedWriter(new FileWriter(F, true)));
pw.println(this.Name+","+this.CheckInDate+","+this.CheckOutDate+","+this.Floor+","+this.RoomID+","+this.ICNumber+","+this.Contact+","+this.Email);
pw.flush();
pw.close();
fw.close();
bw.close();
}
catch(Exception e)
{
}
}
删除记录
public boolean delcus(String Target)
{
boolean success = false;
File F = new File("Customer.txt");
File Ftc = new File("Temp.txt");
try
{
FileReader fr = new FileReader(F);
BufferedReader br = new BufferedReader(fr);
PrintWriter pr = new PrintWriter(Ftc);
String line = br.readLine();
while (line!=null)
{
String[] wordsinLine = line.split(",");
if (wordsinLine[0].equals(Target))
{
}
else
{
pr.println(line);
success = true;
}
line = br.readLine();
}
if (success)
{
pr.flush();
pr.close();
br.close();
fr.close();
}
}
catch (Exception e)
{
}
F.delete();
File dump = new File("Customer.txt");
Ftc.renameTo(dump);
return success;
}
我有另一个方法,在触发insert方法之前检查几个条件
public int checkroom()
{
int check = 0;
int ciDay = this.CheckInDate/10000;
int ciMonth = (this.CheckInDate/100)%100;
int coDay = this.CheckOutDate/10000;
int days = coDay - ciDay;
String name;
int Dbcid;
int Dbcod;
int DbFloor;
int DbRoomID;
try
{
File F = new File("Customer.txt");
FileReader Fr = new FileReader(F);
BufferedReader Reader = new BufferedReader(Fr);
Scanner Sc = new Scanner(Reader);
Sc.useDelimiter("[,\n]");
while(Sc.hasNext())
{
name = Sc.next();
Dbcid = Sc.nextInt();
Dbcod = Sc.nextInt();
DbFloor = Sc.nextInt();
DbRoomID = Sc.nextInt();
if (days <= 7)
{
if (DbFloor == this.Floor && DbRoomID == this.RoomID)
{
int DbcidDay = Dbcid/10000;
int DbcidMonth = (Dbcid/100)%100;
int DbcodDay = Dbcod/10000;
if(ciMonth == DbcidMonth)
{
if (ciDay >= DbcidDay && ciDay < DbcodDay)
{
check = 2;
}
else if (coDay >= DbcidDay && coDay < DbcodDay)
{
check = 3;
}
else if (ciDay <= DbcidDay && coDay >= DbcodDay)
{
check = 4;
}
else
{
check = 1;
}
}
else
{
check = 1;
}
}
else
{
check =1;
}
}
else
{
check =5;
}
}
if(check > 0)
{
Sc.close();
Reader.close();
Fr.close();
}
}
catch (Exception e)
{
}
return check;
}
public int checkroom()
{
整数检查=0;
int ciDay=this.CheckInDate/10000;
int ciMonth=(this.CheckInDate/100)%100;
int coDay=this.CheckOutDate/10000;
整数天=截止日期-截止日期;
字符串名;
int-Dbcid;
int-Dbcod;
int DbFloor;
int-DbRoomID;
尝试
{
文件F=新文件(“Customer.txt”);
FileReader Fr=新的FileReader(F);
BufferedReader读取器=新的BufferedReader(Fr);
扫描仪Sc=新扫描仪(读卡器);
Sc.useDelimiter(“[,\n]”);
while(Sc.hasNext())
{
name=Sc.next();
Dbcid=Sc.nextInt();
Dbcod=Sc.nextInt();
DbFloor=Sc.nextInt();
DbRoomID=Sc.nextInt();
如果(天=DbcidDay&&ciDay=DbcidDay&&coDay0)
{
Sc.close();
Reader.close();
Fr.close();
}
}
捕获(例外e)
{
}
退货检查;
}
我可以看到一些问题:
- 您需要在finally子句中关闭流(或者,更好的是,使用finally子句)。否则,如果抛出中断正常程序流的异常,则不会立即关闭流
- 您应该只关闭最外面的流对象(例如,您的BufferedReader,而不是FileReader)
- 你在吞咽例外。至少对捕获的异常执行printStackTrace(),以便查看是否确实抛出了异常
- 避免使用File.delete()等在发生错误时不会引发异常的方法。相反,在Files.class上使用等效的方法,该方法在发生错误时抛出异常
顺便说一句,虽然这本身不是问题,但您不需要在关闭()之前调用flush()——后者会在关闭前自动刷新。当您关闭最外层的流对象(BufferedReader)时,它会自动关闭内部流(文件读取器)。@Neil Coffey感谢您的建议,作为初学者,我会尽量避免你所说的所有错误。我的问题在阅读了你所附的文件后就解决了。我实现了finally块,现在它工作得很好。