Java 附加到文件不起作用

Java 附加到文件不起作用,java,io,Java,Io,我无法连续写入该文件。我想要一个必须附加到文件中的函数。但是,我无法得到我需要的。如果写的代码有什么错误,有人能帮我吗 void writeLog(String s) { try { String filename= "D:\\Gardening\\Logs.txt"; FileWriter fw = new FileWriter(filename,true); //the true will append the new data fw.write(s+"\n"

我无法连续写入该文件。我想要一个必须附加到文件中的函数。但是,我无法得到我需要的。如果写的代码有什么错误,有人能帮我吗

void writeLog(String s)
{
  try
  {
    String filename= "D:\\Gardening\\Logs.txt";
    FileWriter fw = new FileWriter(filename,true); //the true will append the new data
    fw.write(s+"\n");//appends the string to the file
    fw.close();
  }
  catch(IOException ioe)
  {
    System.err.println("IOException: " + ioe.getMessage());
  }
}
伙计们,我在类的构造函数中发现了问题,完整的代码在这里

Class Log
{
   Log()
   {
      try{
      FileWriter fw=new FileWriter("path of file");
      }
      catch(Exception a)
      {
         System.err.println(a);
      }
   }
   void writeLog(String s)
   {
     try
     {
String filename= "D:\\Gardening\\Logs.txt";
FileWriter fw = new FileWriter(filename,true); //the true will append the new data
fw.write(s+"\n");//appends the string to the file
fw.close();
 }
 catch(IOException ioe)
 {
   System.err.println("IOException: " + ioe.getMessage());
 }
}

当它在构造函数中被一次又一次地调用时,它就这样发生了

刚刚测试了您的代码,它运行良好。你能发布你的完整代码吗?另外,如何调用writeLog方法

public class Test {
    void writeLog(String s) {
        try {
            String filename = "C:\\Temp\\Logs.txt";
            FileWriter fw = new FileWriter(filename, true);
            fw.write(s + "\n");
            fw.close();
        } catch (IOException ioe) {
            System.err.println("IOException: " + ioe.getMessage());
        }
    }

    public static void main(String[] args) {
        Test t1 = new Test();

        for (int i = 0; i < 5; i++) {
            t1.writeLog("Hello");
        }

    }
}

尝试在调用FileWriter的方法之外创建FileWriter,并将其作为第二个参数提供给方法。如果要在整个项目中将字符串附加到文件中,则可以将FileWriter创建为单例,无需反复重新创建FileWriter

顺便说一下,您应该在finally块中关闭FileWriter。因为在您的代码中,如果在关闭操作之前发生异常,则无法关闭FileWriter。我的代码建议如下:

  public static void main(String[] args) {

    FileWriter fw = null;
            try {

             fw = new FileWriter(filename,true);
             writeLog("Your String", fw);

            } catch (IOException ioe) {
                System.err.println("IOException: " + ioe.getMessage());
            }
             finally
          {
               try {
                if(fw != null)
                fw.close();
            }  catch (IOException e) {
                e.printStackTrace();
            }
          }


    }

    static void writeLog(String s , FileWriter fw)
        {
          try
          {
            fw.write(s+"\n"); 
          }
          catch(IOException ioe)
          {
            System.err.println("IOException: " + ioe.getMessage());
          }

        }

这应该将s+\n附加到Logs.txt,您会遇到什么样的错误?我尝试了您的代码,它工作得很好,请解释更多?但是,我无法得到我需要的。请提供您遇到的问题/错误的真实描述。它不是附加到文件中。我不明白为什么
  public static void main(String[] args) {

    FileWriter fw = null;
            try {

             fw = new FileWriter(filename,true);
             writeLog("Your String", fw);

            } catch (IOException ioe) {
                System.err.println("IOException: " + ioe.getMessage());
            }
             finally
          {
               try {
                if(fw != null)
                fw.close();
            }  catch (IOException e) {
                e.printStackTrace();
            }
          }


    }

    static void writeLog(String s , FileWriter fw)
        {
          try
          {
            fw.write(s+"\n"); 
          }
          catch(IOException ioe)
          {
            System.err.println("IOException: " + ioe.getMessage());
          }

        }