Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,907 questions

51,839 answers

573 users

How to append text to an existing text file in Java

3 Answers

0 votes
package javaapplication1;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;

public class Example {
    
    public static void main(String[] args) {
       
        try (BufferedWriter bw = new BufferedWriter(new FileWriter("d:/test.txt", true))) {
            bw.newLine();
            bw.write("append line 1");
            bw.write(" continue - same append line 1");
            bw.newLine();
            bw.write("append line 2");
            bw.flush();
        } catch (IOException ex) {
            Logger.getLogger(Example.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}
   
   
/*
run:

check d:\test.txt file
    
*/

 



answered Jan 23, 2016 by avibootz
edited Jan 24, 2016 by avibootz
0 votes
package javaapplication1;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;

public class Example {
    
    public static void main(String[] args) {
       
        try {
            Files.write(Paths.get("d:\\test.txt"), 
                                  "\r\nlast line".getBytes(), 
                                  StandardOpenOption.APPEND);
        } catch (IOException ex) {
            System.err.println("IOException: " + ex.getMessage());
        }
    }
}
   
   
/*
run:

check d:\test.txt file
    
*/
   
   
/*
run:

check d:\test.txt file
    
*/

 



answered Jan 23, 2016 by avibootz
edited Jan 24, 2016 by avibootz
0 votes
package javaapplication1;

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

public class Example {
    
    public static void main(String[] args) {
       
        try (PrintWriter pw = new PrintWriter(new BufferedWriter(
                                              new FileWriter("d:\\test.txt", true)))) {
            pw.println();
            pw.print("last line");
        } catch (IOException ex) {
            System.err.println("IOException: " + ex.getMessage());
        }
 
    }
}
   
   
/*
run:

check d:\test.txt file
    
*/

 



answered Jan 23, 2016 by avibootz
edited Jan 24, 2016 by avibootz

Related questions

1 answer 164 views
1 answer 159 views
1 answer 143 views
1 answer 172 views
1 answer 171 views
1 answer 169 views
...