【问题标题】:How to append text to an existing file in Java?如何将文本附加到Java中的现有文件?
【发布时间】:2016-10-23 06:25:30
【问题描述】:

我需要将文本重复附加到 Java 中的现有文件中。我该怎么做?

【问题讨论】:

    标签: java file-io io text-files


    【解决方案1】:

    您这样做是为了记录吗?如果是,则有several libraries for this。最受欢迎的两个是Log4jLogback

    Java 7+

    对于一次性任务,Files class 让这一切变得简单:

    try {
        Files.write(Paths.get("myfile.txt"), "the text".getBytes(), StandardOpenOption.APPEND);
    }catch (IOException e) {
        //exception handling left as an exercise for the reader
    }
    

    小心:如果文件不存在,上述方法将抛出NoSuchFileException。它也不会自动附加换行符(在附加到文本文件时通常需要)。另一种方法是同时传递CREATEAPPEND 选项,如果文件尚不存在,它将首先创建文件:

    private void write(final String s) throws IOException {
        Files.writeString(
            Path.of(System.getProperty("java.io.tmpdir"), "filename.txt"),
            s + System.lineSeparator(),
            CREATE, APPEND
        );
    }
    

    但是,如果你要多次写入同一个文件,上面的 sn-ps 必须多次打开和关闭磁盘上的文件,这是一个缓慢的操作。在这种情况下,BufferedWriter 更快:

    try(FileWriter fw = new FileWriter("myfile.txt", true);
        BufferedWriter bw = new BufferedWriter(fw);
        PrintWriter out = new PrintWriter(bw))
    {
        out.println("the text");
        //more code
        out.println("more text");
        //more code
    } catch (IOException e) {
        //exception handling left as an exercise for the reader
    }
    

    注意事项:

    • FileWriter 构造函数的第二个参数将告诉它追加到文件,而不是写入新文件。 (如果文件不存在,则会创建它。)
    • 建议对昂贵的作家使用BufferedWriter(例如FileWriter)。
    • 使用PrintWriter 可以让您访问println 语法,您可能已经习惯了System.out
    • BufferedWriterPrintWriter 包装器并不是绝对必要的。

    旧版 Java

    try {
        PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("myfile.txt", true)));
        out.println("the text");
        out.close();
    } catch (IOException e) {
        //exception handling left as an exercise for the reader
    }
    

    异常处理

    如果您需要对旧 Java 进行稳健的异常处理,它会变得非常冗长:

    FileWriter fw = null;
    BufferedWriter bw = null;
    PrintWriter out = null;
    try {
        fw = new FileWriter("myfile.txt", true);
        bw = new BufferedWriter(fw);
        out = new PrintWriter(bw);
        out.println("the text");
        out.close();
    } catch (IOException e) {
        //exception handling left as an exercise for the reader
    }
    finally {
        try {
            if(out != null)
                out.close();
        } catch (IOException e) {
            //exception handling left as an exercise for the reader
        }
        try {
            if(bw != null)
                bw.close();
        } catch (IOException e) {
            //exception handling left as an exercise for the reader
        }
        try {
            if(fw != null)
                fw.close();
        } catch (IOException e) {
            //exception handling left as an exercise for the reader
        }
    }
    

    【讨论】:

    • 您应该使用 java7 try-with-resources 或将 close() 放在 finally 块中,以确保在出现异常时关闭文件
    • 假设new BufferedWriter(...) 抛出异常; FileWriter 会关闭吗?我猜它不会被关闭,因为close() 方法(在正常情况下)将在out 对象上调用,在这种情况下不会初始化 - 所以实际上close() 方法不会被调用-> 文件将被打开,但不会被关闭。所以恕我直言,try 语句应该看起来像这样try(FileWriter fw = new FileWriter("myFile.txt")){ Print writer = new ....//code goes here } 他应该在退出try 块之前flush() 作家!!!
    • 注意,如果 try 块内抛出异常,“Older java”示例将无法正确关闭流。
    • Java 7 方法的几个可能的“陷阱”: (1) 如果文件不存在,StandardOpenOption.APPEND 将不会创建它 - 有点像无声的失败也不会抛出异常。 (2) 使用.getBytes() 表示附加文本前后没有返回字符。已添加alternative answer 来解决这些问题。
    • @SteveChambers 感谢您的意见。如果文件不存在,我无法相信附加模式不会创建文件,所以我不得不尝试确认。不知道他们在想什么...我发现它确实会引发异常,但是如果您复制/粘贴我的代码并将catch 块留空,那么您将看不到它。我已更新我的答案以反映这些问题,并添加了指向您的答案的链接。
    【解决方案2】:

    您可以使用 fileWriter 并将标志设置为 true 进行追加。

    try
    {
        String filename= "MyFile.txt";
        FileWriter fw = new FileWriter(filename,true); //the true will append the new data
        fw.write("add a line\n");//appends the string to the file
        fw.close();
    }
    catch(IOException ioe)
    {
        System.err.println("IOException: " + ioe.getMessage());
    }
    

    【讨论】:

    • 很好的答案,尽管使用 System.getProperty("line.separator" ) 代替 "\n" 更好。
    • @Decoded 我已经回滚了您对此答案的编辑,因为它无法编译。
    • @Kip,出了什么问题?我一定是输入了一个“错字”。
    • 试试资源怎么样? try(FileWriter fw = new FileWriter(filename,true)){ // Whatever }catch(IOException ex){ ex.printStackTrace(); }
    【解决方案3】:

    使用 Apache Commons 2.1:

    FileUtils.writeStringToFile(file, "String to append", true);
    

    【讨论】:

    • 哦,谢谢。我被所有其他答案的复杂性逗乐了。我真的不明白为什么人们喜欢让他们的(开发者)生活复杂化。
    • 这种方法的问题是它每次都会打开和关闭输出流。根据您写入文件的内容和频率,这可能会导致可笑的开销。
    • @Buffalo 是对的。但是您总是可以在将它们写入文件之前使用 StringBuilder 构建大块(值得编写)。
    • @KonstantinK 但是你需要写的所有内容都被加载到内存中。
    【解决方案4】:

    我只是添加一些小细节:

        new FileWriter("outfilename", true)
    

    2.nd 参数 (true) 是一个称为 appendable (http://docs.oracle.com/javase/7/docs/api/java/lang/Appendable.html) 的功能(或接口)。它负责能够将一些内容添加到特定文件/流的末尾。这个接口是从 Java 1.5 开始实现的。每个具有该接口的对象(即BufferedWriter、CharArrayWriter、CharBuffer、FileWriter、FilterWriter、LogStream、OutputStreamWriter、PipedWriter、PrintStream、PrintWriter、StringBuffer、StringBuilder、StringWriter、Writer)都可以用于添加内容 p>

    换句话说,您可以将一些内容添加到您的 gzip 文件中,或者一些 http 进程

    【讨论】:

      【解决方案5】:
          String str;
          String path = "C:/Users/...the path..../iin.txt"; // you can input also..i created this way :P
      
          BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
          PrintWriter pw = new PrintWriter(new FileWriter(path, true));
      
          try 
          {
             while(true)
              {
                  System.out.println("Enter the text : ");
                  str = br.readLine();
                  if(str.equalsIgnoreCase("exit"))
                      break;
                  else
                      pw.println(str);
              }
          } 
          catch (Exception e) 
          {
              //oh noes!
          }
          finally
          {
              pw.close();         
          }
      

      这会做你想做的事..

      【讨论】:

        【解决方案6】:

        这里的所有 try/catch 块的答案不应该都包含在 finally 块中的 .close() 片段吗?

        标记答案示例:

        PrintWriter out = null;
        try {
            out = new PrintWriter(new BufferedWriter(new FileWriter("writePath", true)));
            out.println("the text");
        } catch (IOException e) {
            System.err.println(e);
        } finally {
            if (out != null) {
                out.close();
            }
        } 
        

        此外,从 Java 7 开始,您可以使用 try-with-resources statement。关闭声明的资源不需要 finally 块,因为它是自动处理的,而且也不那么冗长:

        try(PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("writePath", true)))) {
            out.println("the text");
        } catch (IOException e) {
            System.err.println(e);
        }
        

        【讨论】:

        • out 超出范围时,它会在被垃圾回收时自动关闭,对吧?在您使用 finally 块的示例中,如果我没记错的话,我认为您实际上需要在 out.close() 周围进行另一个嵌套的 try/catch。 Java 7 解决方案非常巧妙! (自 Java 6 以来我没有做过任何 Java 开发,所以我对这种变化并不熟悉。)
        • @Kip 不,超出范围在 Java 中没有任何作用。该文件将在未来的某个随机时间关闭。 (可能在程序关闭时)
        • @etech 第二种方法是否需要flush 方法?
        【解决方案7】:

        示例,使用番石榴:

        File to = new File("C:/test/test.csv");
        
        for (int i = 0; i < 42; i++) {
            CharSequence from = "some string" + i + "\n";
            Files.append(from, to, Charsets.UTF_8);
        }
        

        【讨论】:

        • 这是一个可怕的建议。您打开文件的流 42 次而不是一次。
        • @xehpuk 好吧,这取决于。 42 仍然可以,如果它使代码更具可读性。 42k 是不可接受的。
        【解决方案8】:
        FileOutputStream stream = new FileOutputStream(path, true);
        try {
        
            stream.write(
        
                string.getBytes("UTF-8") // Choose your encoding.
        
            );
        
        } finally {
            stream.close();
        }
        

        然后在上游某处捕获 IOException。

        【讨论】:

          【解决方案9】:

          使用 java.nio.Files 和 java.nio.file.StandardOpenOption

              PrintWriter out = null;
              BufferedWriter bufWriter;
          
              try{
                  bufWriter =
                      Files.newBufferedWriter(
                          Paths.get("log.txt"),
                          Charset.forName("UTF8"),
                          StandardOpenOption.WRITE, 
                          StandardOpenOption.APPEND,
                          StandardOpenOption.CREATE);
                  out = new PrintWriter(bufWriter, true);
              }catch(IOException e){
                  //Oh, no! Failed to create PrintWriter
              }
          
              //After successful creation of PrintWriter
              out.println("Text to be appended");
          
              //After done writing, remember to close!
              out.close();
          

          这将创建一个使用文件的BufferedWriter,它接受StandardOpenOption 参数,并从结果BufferedWriter 自动刷新PrintWriterPrintWriterprintln()方法,然后可以调用写入文件。

          此代码中使用的StandardOpenOption参数:打开文件写入,只追加到文件,如果文件不存在则创建文件。

          Paths.get("path here") 可以替换为new File("path here").toPath()。 并且可以修改Charset.forName("charset name") 以适应所需的Charset

          【讨论】:

            【解决方案10】:

            我可能会建议apache commons project。这个项目已经提供了一个框架来做你需要的(即灵活的集合过滤)。

            【讨论】:

              【解决方案11】:

              确保流在所有情况下都正确关闭。

              这些答案中有多少会在出现错误时使文件句柄保持打开状态,这有点令人担忧。答案https://stackoverflow.com/a/15053443/2498188 是在金钱上,但这只是因为BufferedWriter() 不能扔。如果可以,则异常将使FileWriter 对象保持打开状态。

              不关心BufferedWriter() 是否可以抛出的更通用的方法:

                PrintWriter out = null;
                BufferedWriter bw = null;
                FileWriter fw = null;
                try{
                   fw = new FileWriter("outfilename", true);
                   bw = new BufferedWriter(fw);
                   out = new PrintWriter(bw);
                   out.println("the text");
                }
                catch( IOException e ){
                   // File writing/opening failed at some stage.
                }
                finally{
                   try{
                      if( out != null ){
                         out.close(); // Will close bw and fw too
                      }
                      else if( bw != null ){
                         bw.close(); // Will close fw too
                      }
                      else if( fw != null ){
                         fw.close();
                      }
                      else{
                         // Oh boy did it fail hard! :3
                      }
                   }
                   catch( IOException e ){
                      // Closing the file writers failed for some obscure reason
                   }
                }
              

              编辑:

              从 Java 7 开始,推荐的方式是使用“try with resources”并让 JVM 处理它:

                try(    FileWriter fw = new FileWriter("outfilename", true);
                        BufferedWriter bw = new BufferedWriter(fw);
                        PrintWriter out = new PrintWriter(bw)){
                   out.println("the text");
                }  
                catch( IOException e ){
                    // File writing/opening failed at some stage.
                }
              

              【讨论】:

              • +1 用于正确使用 Java 7 的 ARM。这是关于这个棘手主题的好问题:stackoverflow.com/questions/12552863/…
              • 嗯,由于某种原因,PrintWriter.close() 未在 the docs 中声明为 throws IOException。看看它的sourceclose() 方法确实不能抛出IOException,因为它从底层流中捕获它,并设置一个标志。因此,如果您正在编写下一代航天飞机或 X 射线剂量计量系统的代码,您应该在尝试 out.close() 之后使用 PrintWriter.checkError()。这确实应该记录在案。
              • 如果我们对关闭过于偏执,那么每个XX.close() 都应该在自己的try/catch 中,对吗?例如,out.close() 可能会引发异常,在这种情况下,bw.close()fw.close() 将永远不会被调用,而fw 是最关键的关闭。
              【解决方案12】:

              在项目中的任何位置创建一个函数,然后在需要时调用该函数。

              大家要记住,你们正在调用的活动线程不是异步调用的,因为要正确完成它可能需要 5 到 10 页。 为什么不花更多的时间在你的项目上,忘记写任何已经写好的东西。 妥妥的

                  //Adding a static modifier would make this accessible anywhere in your app
              
                  public Logger getLogger()
                  {
                     return java.util.logging.Logger.getLogger("MyLogFileName");
                  }
                  //call the method anywhere and append what you want to log 
                  //Logger class will take care of putting timestamps for you
                  //plus the are ansychronously done so more of the 
                  //processing power will go into your application
              
                  //from inside a function body in the same class ...{...
              
                  getLogger().log(Level.INFO,"the text you want to append");
              
                  ...}...
                  /*********log file resides in server root log files********/
              

              三行代码第二行,因为第三行实际上附加了文本。 :P

              【讨论】:

                【解决方案13】:

                图书馆

                import java.io.BufferedWriter;
                import java.io.File;
                import java.io.FileWriter;
                import java.io.IOException;
                

                代码

                public void append()
                {
                    try
                    {
                        String path = "D:/sample.txt";
                
                        File file = new File(path);
                
                        FileWriter fileWriter = new FileWriter(file,true);
                
                        BufferedWriter bufferFileWriter  = new BufferedWriter(fileWriter);
                
                        fileWriter.append("Sample text in the file to append");
                
                        bufferFileWriter.close();
                
                        System.out.println("User Registration Completed");
                
                    }catch(Exception ex)
                    {
                        System.out.println(ex);
                    }
                }
                

                【讨论】:

                  【解决方案14】:

                  你也可以试试这个:

                  JFileChooser c= new JFileChooser();
                  c.showOpenDialog(c);
                  File write_file = c.getSelectedFile();
                  String Content = "Writing into file"; //what u would like to append to the file
                  
                  
                  
                  try 
                  {
                      RandomAccessFile raf = new RandomAccessFile(write_file, "rw");
                      long length = raf.length();
                      //System.out.println(length);
                      raf.setLength(length + 1); //+ (integer value) for spacing
                      raf.seek(raf.length());
                      raf.writeBytes(Content);
                      raf.close();
                  } 
                  catch (Exception e) {
                      //any exception handling method of ur choice
                  }
                  

                  【讨论】:

                    【解决方案15】:

                    最好使用 try-with-resources 然后所有 pre-java 7 最终业务

                    static void appendStringToFile(Path file, String s) throws IOException  {
                        try (BufferedWriter out = Files.newBufferedWriter(file, StandardCharsets.UTF_8, StandardOpenOption.APPEND)) {
                            out.append(s);
                            out.newLine();
                        }
                    }
                    

                    【讨论】:

                      【解决方案16】:

                      在 Java-7 中也可以这样做:

                      import java.nio.file.Files;
                      import java.nio.file.Path;
                      import java.nio.file.Paths;
                      import java.nio.file.StandardOpenOption;
                      

                      //------------------------

                      Path filePath = Paths.get("someFile.txt");
                      if (!Files.exists(filePath)) {
                          Files.createFile(filePath);
                      }
                      Files.write(filePath, "Text to be added".getBytes(), StandardOpenOption.APPEND);
                      

                      【讨论】:

                      • 需要什么进口?这些东西使用哪个库?
                      【解决方案17】:

                      以下方法可以让你将文本附加到某个文件:

                      private void appendToFile(String filePath, String text)
                      {
                          PrintWriter fileWriter = null;
                      
                          try
                          {
                              fileWriter = new PrintWriter(new BufferedWriter(new FileWriter(
                                      filePath, true)));
                      
                              fileWriter.println(text);
                          } catch (IOException ioException)
                          {
                              ioException.printStackTrace();
                          } finally
                          {
                              if (fileWriter != null)
                              {
                                  fileWriter.close();
                              }
                          }
                      }
                      

                      或者使用FileUtils:

                      public static void appendToFile(String filePath, String text) throws IOException
                      {
                          File file = new File(filePath);
                      
                          if(!file.exists())
                          {
                              file.createNewFile();
                          }
                      
                          String fileContents = FileUtils.readFileToString(file);
                      
                          if(file.length() != 0)
                          {
                              fileContents = fileContents.concat(System.lineSeparator());
                          }
                      
                          fileContents = fileContents.concat(text);
                      
                          FileUtils.writeStringToFile(file, fileContents);
                      }
                      

                      它效率不高,但工作正常。换行符得到正确处理,如果一个新文件尚不存在,则会创建一个新文件。

                      【讨论】:

                        【解决方案18】:

                        尝试使用 bufferFileWriter.append,它适用于我。

                        FileWriter fileWriter;
                        try {
                            fileWriter = new FileWriter(file,true);
                            BufferedWriter bufferFileWriter = new BufferedWriter(fileWriter);
                            bufferFileWriter.append(obj.toJSONString());
                            bufferFileWriter.newLine();
                            bufferFileWriter.close();
                        } catch (IOException ex) {
                            Logger.getLogger(JsonTest.class.getName()).log(Level.SEVERE, null, ex);
                        }
                        

                        【讨论】:

                        • 这里的 obj.toJSONString() 是什么?
                        • @BhaskaraArani 这只是一个字符串,他举了一个 JSON 对象转换为字符串的示例,但想法是它可以是任何字符串。
                        【解决方案19】:

                        如果我们使用 Java 7 及以上版本并且知道要添加(附加)到文件中的内容,我们可以使用 NIO 包中的newBufferedWriter 方法。

                        public static void main(String[] args) {
                            Path FILE_PATH = Paths.get("C:/temp", "temp.txt");
                            String text = "\n Welcome to Java 8";
                        
                            //Writing to the file temp.txt
                            try (BufferedWriter writer = Files.newBufferedWriter(FILE_PATH, StandardCharsets.UTF_8, StandardOpenOption.APPEND)) {
                                writer.write(text);
                            } catch (IOException e) {
                                e.printStackTrace();
                            }
                        }
                        

                        有几点需要注意:

                        1. 指定字符集编码始终是一个好习惯,为此我们在StandardCharsets 类中有常量。
                        2. 代码使用try-with-resource语句,资源在尝试后自动关闭。

                        虽然 OP 没有询问,但以防万一我们想搜索具有某些特定关键字的行,例如confidential 我们可以在 Java 中使用流 API:

                        //Reading from the file the first line which contains word "confidential"
                        try {
                            Stream<String> lines = Files.lines(FILE_PATH);
                            Optional<String> containsJava = lines.filter(l->l.contains("confidential")).findFirst();
                            if(containsJava.isPresent()){
                                System.out.println(containsJava.get());
                            }
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                        

                        【讨论】:

                        • 警告:使用 BufferedWriter write(String string) 时,如果希望在每个字符串写入后换行,则应调用 newLine()
                        【解决方案20】:

                        我的回答:

                        JFileChooser chooser= new JFileChooser();
                        chooser.showOpenDialog(chooser);
                        File file = chooser.getSelectedFile();
                        String Content = "What you want to append to file";
                        
                        try 
                        {
                            RandomAccessFile random = new RandomAccessFile(file, "rw");
                            long length = random.length();
                            random.setLength(length + 1);
                            random.seek(random.length());
                            random.writeBytes(Content);
                            random.close();
                        } 
                        catch (Exception exception) {
                            //exception handling
                        }
                        

                        【讨论】:

                          【解决方案21】:

                          这可以在一行代码中完成。希望这会有所帮助:)

                          Files.write(Paths.get(fileName), msg.getBytes(), StandardOpenOption.APPEND);
                          

                          【讨论】:

                          • 它可能还不够:) 更好的版本是 Files.write(Paths.get(fileName), msg.getBytes(), StandardOpenOption.APPEND, StandardOpenOption.CREATE);
                          【解决方案22】:
                          FileOutputStream fos = new FileOutputStream("File_Name", true);
                          fos.write(data);
                          

                          true 允许在现有文件中追加数据。如果我们会写

                          FileOutputStream fos = new FileOutputStream("File_Name");
                          

                          它将覆盖现有文件。所以选择第一种方法。

                          【讨论】:

                            【解决方案23】:

                            此代码将满足您的需求:

                               FileWriter fw=new FileWriter("C:\\file.json",true);
                               fw.write("ssssss");
                               fw.close();
                            

                            【讨论】:

                            【解决方案24】:
                            /**********************************************************************
                             * it will write content to a specified  file
                             * 
                             * @param keyString
                             * @throws IOException
                             *********************************************************************/
                            public static void writeToFile(String keyString,String textFilePAth) throws IOException {
                                // For output to file
                                File a = new File(textFilePAth);
                            
                                if (!a.exists()) {
                                    a.createNewFile();
                                }
                                FileWriter fw = new FileWriter(a.getAbsoluteFile(), true);
                                BufferedWriter bw = new BufferedWriter(fw);
                                bw.append(keyString);
                                bw.newLine();
                                bw.close();
                            }// end of writeToFile()
                            

                            【讨论】:

                              【解决方案25】:

                              如果您想在特定行中添加一些文本,您可以先读取整个文件,将文本附加到任何您想要的位置,然后覆盖下面代码中的所有内容:

                              public static void addDatatoFile(String data1, String data2){
                              
                              
                                  String fullPath = "/home/user/dir/file.csv";
                              
                                  File dir = new File(fullPath);
                                  List<String> l = new LinkedList<String>();
                              
                                  try (BufferedReader br = new BufferedReader(new FileReader(dir))) {
                                      String line;
                                      int count = 0;
                              
                                      while ((line = br.readLine()) != null) {
                                          if(count == 1){
                                              //add data at the end of second line                    
                                              line += data1;
                                          }else if(count == 2){
                                              //add other data at the end of third line
                                              line += data2;
                                          }
                                          l.add(line);
                                          count++;
                                      }
                                      br.close();
                                  } catch (FileNotFoundException e) {
                                      e.printStackTrace();
                                  } catch (IOException e) {
                                      e.printStackTrace();
                                  }       
                                  createFileFromList(l, dir);
                              }
                              
                              public static void createFileFromList(List<String> list, File f){
                              
                                  PrintWriter writer;
                                  try {
                                      writer = new PrintWriter(f, "UTF-8");
                                      for (String d : list) {
                                          writer.println(d.toString());
                                      }
                                      writer.close();             
                                  } catch (FileNotFoundException | UnsupportedEncodingException e) {
                                      e.printStackTrace();
                                  }
                              }
                              

                              【讨论】:

                                【解决方案26】:
                                import java.io.BufferedWriter;
                                import java.io.FileWriter;
                                import java.io.IOException;
                                import java.io.PrintWriter;
                                
                                public class Writer {
                                
                                
                                    public static void main(String args[]){
                                        doWrite("output.txt","Content to be appended to file");
                                    }
                                
                                    public static void doWrite(String filePath,String contentToBeAppended){
                                
                                       try(
                                            FileWriter fw = new FileWriter(filePath, true);
                                            BufferedWriter bw = new BufferedWriter(fw);
                                            PrintWriter out = new PrintWriter(bw)
                                          )
                                          {
                                            out.println(contentToBeAppended);
                                          }  
                                        catch( IOException e ){
                                        // File writing/opening failed at some stage.
                                        }
                                
                                    }
                                
                                }
                                

                                【讨论】:

                                • 以上只是At this link 提出的解决方案的一个快速示例实现。因此,您可以复制并运行代码并立即查看它是如何工作的,请确保 output.out 文件与 Writer.java 文件位于同一目录中
                                【解决方案27】:

                                Kip's answer 上略微扩展, 这是一个简单的 Java 7+ 方法,用于将 新行 附加到文件,如果它不存在则创建它

                                try {
                                    final Path path = Paths.get("path/to/filename.txt");
                                    Files.write(path, Arrays.asList("New line to append"), StandardCharsets.UTF_8,
                                        Files.exists(path) ? StandardOpenOption.APPEND : StandardOpenOption.CREATE);
                                } catch (final IOException ioe) {
                                    // Add your own exception handling...
                                }
                                

                                补充说明:

                                1. 上面使用Files.write 重载将 的文本写入文件(即类似于println 命令)。要仅将文本写入末尾(即类似于print 命令),可以使用替代Files.write 重载,传入一个字节数组(例如"mytext".getBytes(StandardCharsets.UTF_8))。

                                2. CREATE 选项仅在指定目录已存在时才有效 - 如果不存在,则抛出 NoSuchFileException。如果需要,可以在设置path后添加以下代码来创建目录结构:

                                  Path pathParent = path.getParent();
                                  if (!Files.exists(pathParent)) {
                                      Files.createDirectories(pathParent);
                                  }
                                  

                                【讨论】:

                                • 是否需要检查文件是否存在?我认为.CREATE 为您完成了这项工作。
                                • 如果在文件已经存在时使用.CREATE,它会默默地无法追加任何内容 - 不会引发异常,但现有文件内容保持不变。
                                • 使用APPEND + CREATE 完美运行,无需检查:Files.write(Paths.get("test.log"), (Instant.now().toString() + "\r\n").getBytes(), StandardOpenOption.CREATE, StandardOpenOption.APPEND);
                                【解决方案28】:

                                您可以使用以下代码将内容附加到文件中:

                                 String fileName="/home/shriram/Desktop/Images/"+"test.txt";
                                  FileWriter fw=new FileWriter(fileName,true);    
                                  fw.write("here will be you content to insert or append in file");    
                                  fw.close(); 
                                  FileWriter fw1=new FileWriter(fileName,true);    
                                 fw1.write("another content will be here to be append in the same file");    
                                 fw1.close(); 
                                

                                【讨论】:

                                  【解决方案29】:

                                  java 7+

                                  以我的拙见,因为我是普通 java 的粉丝,我建议它是上述答案的组合。也许我参加聚会迟到了。代码如下:

                                   String sampleText = "test" +  System.getProperty("line.separator");
                                   Files.write(Paths.get(filePath), sampleText.getBytes(StandardCharsets.UTF_8), 
                                   StandardOpenOption.CREATE, StandardOpenOption.APPEND);
                                  

                                  如果文件不存在,它会创建它,如果已经存在,它会附加 sampleText 到现有文件。使用它,可以避免将不必要的库添加到类路径中。

                                  【讨论】:

                                    【解决方案30】:

                                    1.7 方法:

                                    void appendToFile(String filePath, String content) throws IOException{
                                    
                                        Path path = Paths.get(filePath);
                                    
                                        try (BufferedWriter writer = 
                                                Files.newBufferedWriter(path, 
                                                        StandardOpenOption.APPEND)) {
                                            writer.newLine();
                                            writer.append(content);
                                        }
                                    
                                        /*
                                        //Alternative:
                                        try (BufferedWriter bWriter = 
                                                Files.newBufferedWriter(path, 
                                                        StandardOpenOption.WRITE, StandardOpenOption.APPEND);
                                                PrintWriter pWriter = new PrintWriter(bWriter)
                                                ) {
                                            pWriter.println();//to have println() style instead of newLine();   
                                            pWriter.append(content);//Also, bWriter.append(content);
                                        }*/
                                    }
                                    

                                    【讨论】:

                                      猜你喜欢
                                      • 2010-12-10
                                      • 2019-07-24
                                      • 1970-01-01
                                      相关资源
                                      最近更新 更多