簡體   English   中英

我的讀者和作者在這種方法中沒有正確關閉嗎?

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

當我在插入新記錄之前先刪除一條記錄時,我可以這樣做,刪除后我可以添加新記錄。 但是如果我先插入一條新記錄,那么我的刪除 function 將不起作用。 根據我的研究,這主要是因為輸入/輸出沒有正確關閉,但我已經這樣做了,請看一下我的源代碼,謝謝。

插入記錄

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

我有另一種方法可以在觸發插入方法之前檢查幾個條件。

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

我可以看到一些問題:

  • 您需要在 finally 子句中關閉您的流(或者,更好的是,使用try-with-resource )。 否則,如果拋出異常中斷正常程序流程,您的 stream 將不會立即關閉。
  • 您應該只關閉最外面的 stream object (例如您的 BufferedReader,但不是 FileReader)
  • 你正在吞咽異常。 至少對您捕獲的異常執行 printStackTrace(),以便查看是否實際拋出了任何異常。
  • 避免使用像 File.delete() 這樣在發生錯誤時不會拋出異常的方法。 相反,使用 Files.class 上的等效方法,在發生錯誤時拋出異常。

順便說一句,雖然這不是問題,但您不需要在 close() 之前調用 flush() - 后者在關閉之前自動刷新。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM