简体   繁体   中英

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. 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 ). Otherwise, if an exception is thrown that interrupts the normal program flow, your stream will not be closed immediately.
  • You should only close the outermost stream object (so eg your BufferedReader, but not the FileReader)
  • You are swallowing exceptions. At least do a printStackTrace() on the exceptions you catch so you can see if any are actually thrown.
  • Avoid methods like File.delete() that don't throw exceptions in the case of an error. Instead, use the equivalent methods on the Files.class, which throw exceptions in the event of an error.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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