我支持的Java應用程序在平面文件中記錄了一些細節。我有時遇到的問題是,與前一天相比,進入量非常低。此條目最重要,因爲我們的報告是基於文件生成的。我去了寫代碼,我找不出任何問題。正在編寫的方法是同步方法。Java寫入文本文件無法正常工作
有什麼建議嗎?我也可以爲你提供代碼,你可能需要嗎?
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();
}
}
您確定日誌條目確實缺失(可能當天沒有太多活動)?如果您使用已建立的日誌記錄庫(並且您可能應該這樣做),那麼它們很可能不會得到這個基本功能的錯誤。 – Thilo
你能發表一些代碼嗎? –
我不知道你想要發生什麼。請張貼一些相關的代碼,以便我們可以在這裏開始。 – Jon