简体   繁体   English

我的读者和作者在这种方法中没有正确关闭吗?

[英]Is my readers and writers in this method not closing properly?

When I delete a record first before inserting a new record, I can do it, and after deleting I can add new record.当我在插入新记录之前先删除一条记录时,我可以这样做,删除后我可以添加新记录。 But if I insert a new record first then my delete function is not working.但是如果我先插入一条新记录,那么我的删除 function 将不起作用。 Based on my research, it's mainly because the input/output is not closed properly but I have already done that, please take a look at my source code thank you.根据我的研究,这主要是因为输入/输出没有正确关闭,但我已经这样做了,请看一下我的源代码,谢谢。

Insert record插入记录

    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)
    {
    }
}

Delete Record删除记录

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;
}

I have another method that checks for several conditions before triggering the insert method.我有另一种方法可以在触发插入方法之前检查几个条件。

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;
}

There are a few issues I can see:我可以看到一些问题:

  • You need to close your streams in a finally clause (or, better still, use a try-with-resource ).您需要在 finally 子句中关闭您的流(或者,更好的是,使用try-with-resource )。 Otherwise, if an exception is thrown that interrupts the normal program flow, your stream will not be closed immediately.否则,如果抛出异常中断正常程序流程,您的 stream 将不会立即关闭。
  • You should only close the outermost stream object (so eg your BufferedReader, but not the FileReader)您应该只关闭最外面的 stream object (例如您的 BufferedReader,但不是 FileReader)
  • You are swallowing exceptions.你正在吞咽异常。 At least do a printStackTrace() on the exceptions you catch so you can see if any are actually thrown.至少对您捕获的异常执行 printStackTrace(),以便查看是否实际抛出了任何异常。
  • Avoid methods like File.delete() that don't throw exceptions in the case of an error.避免使用像 File.delete() 这样在发生错误时不会抛出异常的方法。 Instead, use the equivalent methods on the Files.class, which throw exceptions in the event of an error.相反,使用 Files.class 上的等效方法,在发生错误时抛出异常。

Incidentally, although it's not an issue as such, you don't need to call flush() just before close()-- the latter automatically flushes before closing.顺便说一句,虽然这不是问题,但您不需要在 close() 之前调用 flush() - 后者在关闭之前自动刷新。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM