Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/350.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java写入文本文件时工作不正常_Java - Fatal编程技术网

Java写入文本文件时工作不正常

Java写入文本文件时工作不正常,java,Java,我支持的Java应用程序正在一个平面文件中记录一些细节。我有时面临的问题是,与前一天相比,入学率非常低。此条目非常重要,因为我们的报告是基于文件生成的。我写了很多代码,但我想不出任何问题。正在写入的方法是sync方法 有什么建议吗?我也可以为你提供代码是你可能需要的吗 public synchronized void log (String connID, String hotline, String callerType, String cli, String lastMen

我支持的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 ();
    }
}

您已经创建了一个
文件输出流
,但没有关闭该流。关闭该流并重试。这可能是造成问题的原因


有时会记录消息,因为垃圾收集器会每隔一段时间启动并关闭
FileOutStream
。这样就可以再次记录消息。由于在
if
else
两个块中都有
return
语句,因此出现了无法访问的错误。您必须将
PrintWriter
FileOutStreamWriter
getPrintWriter
中取出,将其放在通常调用
getPrintWriter()
的位置。然后您就可以正确地关闭流
getPrintWriter
应仅确保文件存在,因此将其重命名为
EnsureRefleExistence

如果可以使用Apache Common IO,请尝试以下操作:

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();
    File file = new File(absoluteFP);
    String message = this.getDateTime() + "," + connID + "," + hotline + "," + callerType + "," + cli + "," + lastMenu + "," + lastInput + "," + status + "," + reason;
    try {
        // note that you must explicitly add new line character if you want the line to end with newline
        FileUtils.write(file, message + "\n", "UTF-8", true);
    } catch (IOException ex) {
        ex.printStackTrace ();
    }
}
在通用IO 2.1中,您可以附加要写入的文件。您现在可以摆脱
closePW
getPrintwriter
,并且由于日志方法是
synchronized
,因此可以从同一对象一次写入一个文件。但是,如果您试图同时从不同的对象写入相同的文件,则最终会出现覆盖问题


此外,Common IO会自动为您创建缺少的父文件夹。无需显式检查和创建文件夹。

您确定日志条目确实丢失了(可能那天没有太多活动)?如果您使用一个已建立的日志库(您可能应该这样做),那么他们不太可能错误地使用这个基本功能。我不知道您希望发生什么。请发布一些相关的代码,这样我们就可以开始了。@Thio和Aditya我已经用我的代码更新了我的文章。@ArunKumar哪里有代码?@ArunKumar你应该编辑这篇文章来添加代码,而不是开始新的代码。只是最后的澄清,假设我将这两个不同的方法分开,我是否必须使我的文件夹存在方法也成为同步方法?不需要
file.mkDirs()
将失败,但不会给出异常。这将是安全的。