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 w3spoint
 */
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 w3spoint
 */
class IOTest{
	String str = "Hello www.w3spoint.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:

Contents written successfully.

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 w3spoint
 */
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.

Content Protection by DMCA.com
Please Share