HowToDoInJava

  • Python
  • Java
  • Spring Boot
  • Dark Mode
Home / Java / I/O / Java Append to File

Java Append to File

Learn to append content to file in Java using BufferedWritter, PrintWriter, FileOutputStream and Files class. In all the examples, while opening the file to write, you have pass a second argument as true which denotes that file is opened in append mode.

Table of Contents

Append to File using Files class
Append to File using BufferedWritter
Append to File using PrintWriter
Append to File using FileOutputStream

Append to File using Files class

With Files class, we can write a file using it’s write() function. Internally write() function uses OutputStream to write byte array into the file.

To append content to an existing file, Use StandardOpenOption.APPEND while writing the content.

public static void usingPath() throws IOException 
{
    String textToAppend = "\r\n Happy Learning !!";	//new line in content
    Path path = Paths.get("c:/temp/samplefile.txt");
    Files.write(path, textToAppend.getBytes(), StandardOpenOption.APPEND);	//Append mode
}

Append to File using BufferedWritter

BufferedWritter buffers before writing, so it result in less IO operations, so it improve the performance.

To append string to an existing file, open file writer in append mode with passing the second argument as true.

public static void usingBufferedWritter() throws IOException 
{
	String textToAppend = "Happy Learning !!";
	
        //Set true for append mode
	BufferedWriter writer = new BufferedWriter(
		new FileWriter("c:/temp/samplefile.txt", true));	

	writer.write(textToAppend);
	writer.close();
}

Append to File using PrintWriter

Use PrintWriter to write formatted text to a file. This class implements all of the print methods found in PrintStream, so you can use all formats which you use with System.out.println() statements.

To append content to an existing file, open file writer in append mode by passing the second argument as true.

public static void usingPrintWriter() throws IOException 
{
	String textToAppend = "Happy Learning !!";
	
	FileWriter fileWriter = new FileWriter("c:/temp/samplefile.txt", true);	//Set true for append mode
    PrintWriter printWriter = new PrintWriter(fileWriter);
    printWriter.println(textToAppend);	//New line
    printWriter.close();
}

Append to File using FileOutputStream

Use FileOutputStream to write binary data to a file. FileOutputStream is meant for writing streams of raw bytes such as image data. For writing streams of characters, consider using FileWriter.

To append content to an existing file, open FileOutputStream in append mode by passing second argument as true.

public static void usingFileOutputStream() throws IOException 
{
	String textToAppend = "\r\n Happy Learning !!";	//new line in content
	
	FileOutputStream outputStream = new FileOutputStream("c:/temp/samplefile.txt", true);
    byte[] strToBytes = textToAppend.getBytes();
    outputStream.write(strToBytes);
 
    outputStream.close();
}

Happy Learning !!

Was this post helpful?

Let us know if you liked the post. That’s the only way we can improve.
TwitterFacebookLinkedInRedditPocket

About Lokesh Gupta

A family guy with fun loving nature. Love computers, programming and solving everyday problems. Find me on Facebook and Twitter.

Feedback, Discussion and Comments

  1. blue

    July 9, 2019

    Thank you

Comments are closed on this article!

Search Tutorials

Java IO

  • Java IO Introduction
  • Java How IO works?
  • Java IO vs NIO
  • Java Create File
  • Java Write to File
  • Java Append to File
  • Java Read File
  • Java Read File to String
  • Java Read File to Byte[]
  • Java Make File Read Only
  • Java Copy File
  • Java Copy Directory
  • Java Delete Directory
  • Java Current Working Directory
  • Java Read/Write Properties File
  • Java Read File from Resources
  • Java Read File from Classpath
  • Java Read/Write UTF-8 Data
  • Java Check if File Exist
  • Java Create Temporary File
  • Java Write to Temporary File
  • Java Delete Temporary File
  • Java Read from Console
  • Java Typesafe input using Scanner
  • Java Password Protected Zip
  • Java Unzip with Subdirectories
  • Java Generate SHA/MD5
  • Java Read CSV File
  • Java InputStream to String
  • Java String to InputStream
  • Java OutputStream to InputStream
  • Java InputStreamReader
  • Java BufferedReader
  • Java FileReader
  • Java LineNumberReader
  • Java StringReader
  • Java FileWriter
  • Java BufferedWriter
  • Java FilenameFilter
  • Java FileFilter

Java Tutorial

  • Java Introduction
  • Java Keywords
  • Java Flow Control
  • Java OOP
  • Java Inner Class
  • Java String
  • Java Enum
  • Java Collections
  • Java ArrayList
  • Java HashMap
  • Java Array
  • Java Sort
  • Java Clone
  • Java Date Time
  • Java Concurrency
  • Java Generics
  • Java Serialization
  • Java Input Output
  • Java New I/O
  • Java Exceptions
  • Java Annotations
  • Java Reflection
  • Java Garbage collection
  • Java JDBC
  • Java Security
  • Java Regex
  • Java Servlets
  • Java XML
  • Java Puzzles
  • Java Examples
  • Java Libraries
  • Java Resources
  • Java 14
  • Java 12
  • Java 11
  • Java 10
  • Java 9
  • Java 8
  • Java 7

Meta Links

  • About Me
  • Contact Us
  • Privacy policy
  • Advertise
  • Guest and Sponsored Posts

Recommended Reading

  • 10 Life Lessons
  • Secure Hash Algorithms
  • How Web Servers work?
  • How Java I/O Works Internally?
  • Best Way to Learn Java
  • Java Best Practices Guide
  • Microservices Tutorial
  • REST API Tutorial
  • How to Start New Blog

Copyright © 2020 · HowToDoInjava.com · All Rights Reserved. | Sitemap

  • Sealed Classes and Interfaces