JavaBeat

  • Home
  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
    • Join Us (JBC)
  • Privacy
  • Contact Us

Java FileWriter Example

April 18, 2014 by Krishna Srinivasan Leave a Comment

FileWriter class useful for writing the stream of characters to a file. FileWriter extends from the OutputStreamWriter. For writing streams of bytes, implement the FileOutputStream class. This class has the different constructors to create the instance for the file object.

  • FileWriter(File file)– Constructs a FileWriter object given a File object.
  • FileWriter(File file, boolean append) – Constructs a FileWriter object given a File object. Appends to the existing file if the append parameter is true. Otherwise overwrites the content.
  • FileWriter(FileDescriptor fd) – Constructs a FileWriter object associated with a file descriptor.
  • FileWriter(String fileName) – Constructs a FileWriter object given a file name.
  • FileWriter(String fileName, boolean append) – Constructs a FileWriter object given a file name with a boolean indicating whether or not to append the data written.

Lets look at the example below to understand how FileWriter class can be implemented.
FileWriterExample.java

[code lang="java"]<br />package javabeat.net.core;<br /><br />import java.io.File;<br />import java.io.FileWriter;<br />import java.io.IOException;<br /><br />/**<br /><%%KEEPWHITESPACE%%> * Java FileWriter Example<br /><%%KEEPWHITESPACE%%> *<br /><%%KEEPWHITESPACE%%> * @author Krishna<br /><%%KEEPWHITESPACE%%> *<br /><%%KEEPWHITESPACE%%> */<br />public class FileWriterExample {<br /><br /><%%KEEPWHITESPACE%%>	/**<br /><%%KEEPWHITESPACE%%>	 * @param args<br /><%%KEEPWHITESPACE%%>	 */<br /><%%KEEPWHITESPACE%%>	public static void main(String[] args) throws IOException {<br /><br /><%%KEEPWHITESPACE%%>		//Create file instance<br /><%%KEEPWHITESPACE%%>		File file = new File("NewTextFile.txt");<br /><br /><%%KEEPWHITESPACE%%>		//Create FileWriter instance with append flag as false<br /><%%KEEPWHITESPACE%%>		FileWriter fileWriter = new FileWriter(file,false);<br /><br /><%%KEEPWHITESPACE%%>		//Write string to a file<br /><%%KEEPWHITESPACE%%>		fileWriter.write("This is FileWriter Example!!");<br /><br /><%%KEEPWHITESPACE%%>		//Write newline character<br /><%%KEEPWHITESPACE%%>		fileWriter.write("\n");<br /><br /><%%KEEPWHITESPACE%%>		//Write using Offset and length in the string<br /><%%KEEPWHITESPACE%%>		fileWriter.write("Whole Text Only Part is Written using Offset and Length",5,20);<br /><br /><%%KEEPWHITESPACE%%>		//Close the file writer object<br /><%%KEEPWHITESPACE%%>		fileWriter.close();<br /><%%KEEPWHITESPACE%%>	}<br /><br />}<br /><br />[/code]

The new file would have created with the below content if you run the above example program.
NewTextFile.txt

[code]<br />This is FileWriter Example!!<br /><%%KEEPWHITESPACE%%> Text Only Part is W<br />[/code]

Filed Under: Java Tagged With: Java File IO

About Krishna Srinivasan

He is Founder and Chief Editor of JavaBeat. He has more than 8+ years of experience on developing Web applications. He writes about Spring, DOJO, JSF, Hibernate and many other emerging technologies in this blog.

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Follow Us

  • Facebook
  • Pinterest

As a participant in the Amazon Services LLC Associates Program, this site may earn from qualifying purchases. We may also earn commissions on purchases from other retail websites.

JavaBeat

FEATURED TUTORIALS

Answered: Using Java to Convert Int to String

What is new in Java 6.0 Collections API?

The Java 6.0 Compiler API

Copyright © by JavaBeat · All rights reserved