C++ File Handling

C++ has a standard library fstream which is used for file handling. <fstream> header file must be included in your program to process files. It has three data types ofstream, ifstream and fstream.


ofstream is the output file stream which is used to create files and to write information to files.


ifstream is the input file stream which is used to read information from files.


fstream is the general file stream which can be used to create files, write information to files, and read information from files.

Opening a file

If you want to write or read from a file first you must open the file. ifstream object is used to open a file to just read from it, ofstream and fstream objects can be used to open a file for writing. The open() function is a member of fstream, ifstream, and ofstream objects. The first argument specifies the name and location of the file and second argument specifies the mode in which the file should be opened.

fstream myFile;
myFile.open(“myFile.txt”, ios::out | ios::in );

Mode Description
ios::app The output to that file will be appended to the end.
ios::ate Move the read/write control to the end of the file.
ios::in Open the file for reading.
ios::out Open the file for writing.
ios::trunc The contents will be truncated before opening the file if it already exists.

Closing a file

The close() function is is a member of fstream, ifstream, and ofstream objects and used to close opened files. C++ automatically flush all the streams, opened files and release allocated memory when a program terminates. But it is good practice to close all the opened files before program termination. Following is the syntax to close a file.

void close();

Writing & reading from a file

The stream insertion operator (<<) is used with the ofstream or fstream object to write information to a file. The stream extraction operator (>>) is used with the ifstream or fstream object to read information from a file.

File Handling Example

#include <fstream>
#include <iostream>

using namespace std;

int main () {

char data[50];

ofstream myFile;

cin.getline(data, 50);

//writing information into the file
myFile << data << endl;

//closing file

//opening the file in read mode
ifstream oldFile;

oldFile >> data;

//printing the data on screen.
cout << data << endl;

//closing file

return 0;


Java Files & I/O

To perform input and output(I/O) operations in java we use java.io package which contains classes for these operations. Most application needs to read input from a source, process it and generate output depending on input.

InputStream: InPutStream is used when you want to read data from a source.

OutPutStream: OutPutStream is used when you want to write data at some destination.

In following example we are reading a text file and displaying its contents on screen using java.io package classes.

import java.io.*;

public class JavaReadTextFile {

public static void main(String args[]) throws IOException


FileInputStream ins = null;

try {

ins = new FileInputStream(“textfile.txt”);

BufferedReader br = new BufferedReader(new InputStreamReader(ins));

String c = null;

while ((c = br.readLine()) != null) {



if (br != null) {



} finally {

if (ins != null) {







The contents of textfile.txt will be displayed on screen line by line.

Java Byte Stream

Byte stream in java is used to perform input and output operations of 8 bit bytes. The following example will copy the contents of a text file to another text file using FileInputStream and FileOutputStream.

import java.io.*;

public class JavaCopyFileContents {

public static void main(String args[]) throws IOException


FileInputStream fin = null;

FileOutputStream fout = null;

int ch;

try {

fin = new FileInputStream(“inputtextfile.txt”);

fout = new FileOutputStream(“outputtextfile.txt”);

while ((ch = fin.read()) != -1) {





} catch (IOException e){

System.out.println(“Something went wrong”);





A new file “outputtextfile.txt” will be created with “inputtextfile.txt” contents.

Standard Streams

All programming languages are required to have standard I/O streams so a program can take input form user’s keyboard and display output on user’s screen after processing the input. Java has following standard I/O streams.

System.in: standard input stream

System.out: standard output stream

System.err: standard error stream

Java Directories

A directory is a type of file which can contain other directories and files. In java File object is used to create new directories and list the files and sub-directories. The mkdir() and mkdirs() methods are used to create directories. If parent directories don’t exists the mkdir() method will return false. mkdirs() methods create both the directory and all parent directories.

Creating Directory

import java.io.File;

public class JavaCreateDirectory {

public static void main(String args[]) {

String dirPath = “C:/javaexamples/files”;

File f = new File(dirPath);