繁体   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