Home Java Examples Io Java Write To File End Of Line
Questions:Ask|Latest

 
 

Share on Google+Share on Google+

Java Write To File End Of Line

Advertisement
In this tutorial you will learn how to write to file at the end of the line. Write to a file in which some contents are available already and you need to write the text at the end of line you may to use the FileWriter("String fileName", boolean append) this constructor creates a FileWriter object with the given file name with retaining the old text.

Java Write To File End Of Line

In this tutorial you will learn how to write to file at the end of the line.

Write to a file in which some contents are available already and you need to write the text at the end of line you may to use the FileWriter("String fileName", boolean append) this constructor creates a FileWriter object with the given file name with retaining the old text. You may wrap the FileWriter object to the higher level output stream instance for the efficient writing in the file.

For this example you should already have a text file.

In the example given below at first I have created a text file into which I have written some text. Now to write new text in a file at the end of line using java program I have constructed the FileWriter object using above mentioned FileWriter constructor into which passed the file name that I required to rewrite. Then passed the FileWriter object to the instance of BufferedWriter class. And to write the text in new line I have used the newLine() method of BufferedWriter class that breaks the line into new line.

Example :

WriteToFileEndOfLine.java

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

class WriteToFileEndOfLine { public static void main(String args[]) { try { FileWriter fw = new FileWriter("writeToFileEndOfLine.txt", true); BufferedWriter bw = new BufferedWriter(fw); bw.newLine(); bw.write("These text will be written at the end line of file"); bw.close(); } catch (Exception e) { System.out.println(e); } System.out.println("Text written successfully into the file."); } }

How to Execute this example :

After doing the basic process to execute a java program write simply on command prompt as :

javac WriteToFileEndOfLine.java to compile the program

And after successfully compilation to run simply type as :

java WriteToFileEndOfLine

Output :

When you will execute this example, new texts that you are trying to write in the existing file will be written after the end of line i.e. new text will be written in a new line as the output is given below :

Download Source Code

Advertisement

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: January 19, 2012

Related Tutorials

Ask Questions?    Discuss: Java Write To File End Of Line  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments
DMCA.com