• August 30, 2014
  • Core Java
  • Comments Off on FileInputStream and FileOutputStream in java

FileInputStream and FileOutputStream in java

FileInputStream:

FileInputStream stream is used for reading data from the files.

Commonly used constructors of FileInputStream:

1. FileInputStream(File file)

Creates a FileInputStream by opening a connection to an actual file, the file named by the File object file in the file system.

2. FileInputStream(String name)

Creates a FileInputStream by opening a connection to an actual file, the file named by the path name name in the file system.

Example:

FileInputStreamExample.java

import java.io.FileInputStream;
 
/**
 * This program is used to read a file using FileInputStream.
 * @author javawithease
 */
class IOTest{
	public void readFile(){
	       try {
		  //creating FileInputStream object.
		  FileInputStream fis = 
                    new FileInputStream("F:\\New folder\\data1.txt");
		  int i;
		  //read file.
		  while((i=fis.read())!=-1){
			System.out.print((char)i);
		  }
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
 
public class FileInputStreamExample {
	public static void main(String args[]){
		//creating IOTest object.
		IOTest obj = new IOTest();
 
		//method call.
		obj.readFile();
	}
}

Output:

Hello World.

Download this example.

FileOutputStream:

FileOutputStream is used to create a file and write data into it. It will create a file, if it does not exist.

Commonly used constructors of FileOutputStream:

1. FileOutputStream(File file)

Creates a file output stream to write to the file represented by the specified File object.

2. FileOutputStream(String name)

Creates a file output stream to write to the file with the specified name.

Example:

FileOutPutStreamExample.java

 
import java.io.FileOutputStream;
 
/**
 * This program is used to write data into 
 * a file using FileOutputStream.
 * @author javawithease
 */
class IOTest{
	String str = "Hello www.javawithease.com";
 
	public void writeFile(){
	     try {
		  //Creating FileOutputStream object.
		  //It will create a new file before writing if not exist.
		  FileOutputStream fos = 
                        new FileOutputStream("F:\\New folder\\data2.txt");
		  byte b[]=str.getBytes();  
		  fos.write(b); 
		  fos.flush();
		  //Close file after write operation.
		  fos.close();  
 
		  System.out.println("Contents written successfully.");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
 
public class FileOutPutStreamExample {
	public static void main(String args[]){
		//Creating IOTest object.
		IOTest obj = new IOTest();
 
		//method call
		obj.writeFile();
	}
}

Output:

Hello.
This is a text file.

Download this example.

Example: Reading the data from one file and writing it into another file.

ReadWriteExample.java

import java.io.FileInputStream;
import java.io.FileOutputStream;
 
/**
 * This program is used to read a file and write into another file.
 * @author javawithease
 */
class IOTest{
	public void readFile(){
	    try {
		  //Creating FileInputStream object.
		  FileInputStream fis = 
			new FileInputStream("F:\\New folder\\data1.txt");
 
		  //Creating FileOutputStream object.
		  FileOutputStream fos = 
			new FileOutputStream("F:\\New folder\\data7.txt");
 
		  int i;
		  //read file.
		  while((i=fis.read())!=-1){
			fos.write(i);
		  }
 
		  System.out.println("Content writen successfully.");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
 
public class ReadWriteExample {
	public static void main(String args[]){
		//creating IOTest object.
		IOTest obj = new IOTest();
 
		//method call.
		obj.readFile();
	}
}

Output:

Contents written successfully.

Download this example.
 
Next Topic: Byte Streams in java with example.
Previous Topic: Input output (I/O) in java.

Related Topics:

Input output (I/O) in java.
FileInputStream and FileOutputStream in java with example.
Byte Streams in java with example.
DataInputStream and DataOutputStream in java with example.
BufferedInputStream and BufferedOutputStream in java with example.
FileReader and FileWriter in java with example.
How To Check If A File Exists In Java.
Serialization in java with example.
Transient in java with example.

 


Comments are closed.