Saturday 19, Apr 2025
We are moved to new domain
Click -> www.ehowtonow.com
Saturday, 5 October 2013

How to Write File using BufferedWriter


In this tutorials we are going to see how to write file using BufferedWriter in Java.
1.Create project called JavaMisc.
2.Create package called com.javatutorialscorner.io
Create java class called FileWriter under com.javatutorialscorner.io package.
FileWriter.java
package com.javatutorialscorner.io;

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

public class FileWriter {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
BufferedWriter bw = null;
java.io.FileWriter fw = null;
String message = "This is test String to be written in File named JavaTutorialsCorner";
File file = null;
try {
file = new File("C:\\jtc\\javatutorialscorner.txt");

if (!file.exists()) {
file.createNewFile();
}

fw = new java.io.FileWriter(file.getAbsoluteFile());
bw = new BufferedWriter(fw);
bw.write(message);
bw.close();

} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}

}

BufferedWriter is unlike FileOutputStream.It is character stream class. So,  we can directly write String, Array, character data into file.

Now run the program see the file created at specified location with text given below.

This is test String to be written in File named JavaTutorialsCorner

Shop and help us

Flipkart Offer Snapdeal offer Amazon.in offer Amazon.com offer

Related Posts:

  • How to get file size using Java Following example shows how to get file size in bytes using file.length() method in Java. lengthpublic long length() Returns the length of the file denoted by this abstract pathname. The return value is unspecified if this … Read More
  • How to rename the file using Java Following example shows how to rename the file using file.renameTo() method in Java. renameTo public boolean renameTo(File dest)  Renames the file denoted by this abstract pathname. Many aspects of the behavior of this… Read More
  • Java 8 – Overview and New Features Java 8 (Java SE 8) is a major feature release.With Java 8 release, java provided support for Functional Programming JavaScript Engine API for Date and Time Streaming API, etc.., New Features in Java 8 Some important featu… Read More
  • How to compare paths of two file using Java Following example shows how to compare paths of two file using file1.compareTo() method in Java. compareTo public int compareTo(File pathname)  Compares two abstract pathnames lexicographically. The ordering defined by… Read More
  • How to convert a String to Upper caseFollowing example shows how to convert a string to upper case. Sample Programpackage com.javatutorialcorner.javastring;public class ToUpperCase { public static void main(String[] args) { String input = "Java tutoria… Read More
  • Blogger Comments
  • Facebook Comments
  • Disqus Comments

0 comments:

Post a Comment

Item Reviewed: How to Write File using BufferedWriter Rating: 5 Reviewed By: eHowToNow