gpt4 book ai didi

Java 写入文本文件无法正常工作

转载 作者:行者123 更新时间:2023-12-02 08:03:49 24 4
gpt4 key购买 nike

我支持的 Java 应用程序正在将一些详细信息记录在平面文件中。我有时面临的问题是,与前一天相比,入场人数非常低。此条目至关重要,因为我们的报告是根据该文件生成的。我编写代码时找不到任何问题。正在写入的方法是sync方法。

有什么建议吗?我还可以为您提供代码,您可能需要吗?

 public synchronized void log (String connID, String hotline, String callerType,
String cli, String lastMenu, String lastInput,
String status, String reason)
{
//String absoluteFP = LOG_LOC + ls + this.getFilename();

//PrintWriter pw = this.getPrintWriter(absoluteFP, true, true);

try
{
pw.print (this.getDateTime ()+ ","+connID +","+hotline+","+callerType+","+ cli+"," + lastMenu + "," + lastInput + "," + status + "," + reason);


//end 1006
pw.print (ls);
pw.flush ();
//pw.close();
}
catch (Exception e)
{
e.printStackTrace ();
return;
}
}

private synchronized PrintWriter getPrintWriter (String absoluteFileName,
boolean append, boolean autoFlush)
{
try
{
//set absolute filepath
File folder = new File (absoluteFileName).getParentFile ();//2009-01-23

File f = new File (absoluteFileName);

if (!folder.exists ())//2009-01-23
{
//System.out.println ("Call Detailed Record folder NOT FOUND! Creating a new);
folder.mkdirs ();

//System.out.println ("Configure log folder");
this.setHiddenFile (LOG_LOC);//set tmp directory to hidden folder

if (!f.exists ())
{
//System.out.println ("Creating a new Call Detailed Record...");//2009-01-23

f.createNewFile ();//2009-01-23

}
}
else
{
if (!f.exists ())
{
//System.out.println ("Creating a new Call Detailed Record...");//2009-01-23

f.createNewFile ();//2009-01-23


}
}

FileOutputStream tempFOS = new FileOutputStream (absoluteFileName, append);
if (tempFOS != null)
{
return new PrintWriter (tempFOS, autoFlush);
}
else
{
return null;
}
}
catch (Exception ex)
{
ex.printStackTrace ();
return null;
}
}

/**
* Set the given absolute file path as a hidden file.
* @param absoluteFile String
*/
private void setHiddenFile (String absoluteFile)
{
//set hidden file
//2009-01-22, KC
Runtime rt = Runtime.getRuntime ();
absoluteFile = absoluteFile.substring (0, absoluteFile.length () - 1);//2009-01-23
try
{
System.out.println (rt.exec ("attrib +H " + "\"" + absoluteFile + "\"").getInputStream ().toString ());
}
catch (IOException e)
{
e.printStackTrace ();
}
}

private String getDateTime ()
{
//2011-076-09, KC-format up to milliseconds to prevent duplicate PK in CDR table.
//return DateUtils.now ("yyyy/MM/dd HH:mm:ss");
return DateUtils.now ("yyyy/MM/dd HH:mm:ss:SSS");
//end 0609
}

private String getFilename ()
{
///return "CDR_" + port + ".dat";//2010-10-01
return port + ".dat";//2010-10-01
}

public void closePW ()
{
if (pw != null)
{
pw.close ();
}
}

最佳答案

您已经创建了 FileOutputStream ,但不会关闭该流。关闭该流并重试。这可能是导致问题的原因。

有时会记录消息,因为垃圾收集器会在某些时间间隔启动并关闭 FileOutStream 。这样就可以再次记录消息。由于您有 return,因此您收到了无法访问的错误。 if 中的声明& else block 。您必须调用 PrintWriterFileOutStreamWritergetPrintWriter将其放在您通常称为 getPrintWriter() 的位置。然后您将能够正确关闭流。 getPrintWriter应该只确保文件存在,因此将其重命名为 ensureFileExistance

关于Java 写入文本文件无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8483631/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com