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

How to write file using FileOutputStream in Java


In this tutorials we are going to see how to write file using FileOutputStream 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.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class FileWriter {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
FileOutputStream fos = 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");
fos = new FileOutputStream(file);

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

byte[] bytes = message.getBytes();
fos.write(bytes);
fos.flush();
fos.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}

}

The FileOutputStream class  is a bytes stream class that used to handle raw binary data.

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:

  • Convert XML to PropertiesIn this tutorial we are going to see how to convert XML file into properties using Java 1. Create project called JavaXML. 2. Create package called com.javatutorialscorner.xml 3. Create java class called XMLToProperty under co… Read More
  • How to Create Pretty Print JSON using JacksonIn this Tutorials we are going to see how to create Pretty Print JSON using Jackson. 1. Create project called JSONExample. 2. Create package called com.javatutorialscorner.jackson  3. Create java class called… Read More
  • Convert Property File to XMLIn this tutorial we are going to see how to convert Property file into XML using Java 1. Create project called JavaXML. 2. Create package called com.javatutorialscorner.xml3. Create java class called PropertyToXML under com.… Read More
  • Read XML File using DOM parserIn this tutorial we are going to see how to read XML file using DOM Parser. In this example I used to read XML document by Element Name.DOM parser parse the entire XML document and load it into memory.DOM parse the nodes as t… Read More
  • Java Best Practice – Reuse Objects instead of creating new ones if possibleReuse Objects instead of creating new ones if possibleObject creation is an expensive operation in Java, with impact on both performance and memory utilization.The cost varies depending on the amount of initialization that ne… Read More
  • Blogger Comments
  • Facebook Comments
  • Disqus Comments

0 comments:

Post a Comment

Item Reviewed: How to write file using FileOutputStream in Java Rating: 5 Reviewed By: eHowToNow