DEV Community

loading...

FILE HANDALING

pranavi333 profile image Pranavi nerella ・2 min read

A file is the name of memory location where we can store data permanently under computer’s hard disk. Java language supports a set of classes in java.io package to create and maintain files. The following are some of the classes we can use to create and maintain files :

FileInputStream
FileOutputStream
FileWriter etc.,

FileOutputStream class :

This class represent data as a stream of bytes. The objects of this class can be used to open and access a file in write mode. The object for this class can be created as below :

FileOutputStream fis = new FileOutputStream(“file-name”,boolean-value); Here, if the boolean value is true, the file can be opened in append mode. If it is false,the data is overwritten from the beginning of the file every time when we execute the program.The following program demonstrates the use of this class.

/* Program to store data into a file using FileOutputStream class */

import java.io.*;

class FileWrite{

public static void main(String args[]) throws Exception {

InputStreamReader isr = new InputStreamReader(System.in);

BufferedReader br = new BufferedReader(isr);

System.out.println(“Enter some text “);

String data = br.readLine();

FileOutputStream fos = new FileOutputStream(“file1.txt”,true);

/* Because data is in the form of String object, convert it into byte array */

byte b[] = data.getBytes();

fos.write(b); /* writes byte array into file.txt */

fos.close();

}

}

FileInputStream class :

This clas represents data as a stream of bytes. The objects of this class can be used to open and access a file in read mode. The object for this class can be created as below :

FileInputStream fis = new FileInputStream(“file-name”);

The following program demonstrates the use of this class.

/* Program to read data from a file using FileInputStream class */

import java.io.*;

class FileRead {

public static void main(String args[]) throws Exception {

FileInputStream fis = new FileInputStream(“file1.txt”);

int size = fis.available(); /* available() returns no. of bytes in file1.txt */

byte b [] = new byte[size];

/* create a byte array of file size */

fis.read(b);

String data = new String(b);

/* convert the byte array to string */

System.out.println(“The contents of file1.txt are : “ + data);

}// end of main

}// end of class

FileWriter class :

This class represent data as a stream of characters. The objects of this class can be used to open and access a file in write mode. The object for this class can be created as below :

FileWriter fw = new FileWriter(filename,boolean-value);

The following program demonstrates the use of FileWriter class.

import java.io.*;

class FileWrite {

public static void main(String args[]) throws Exception {

FileWriter fw = new FileWriter(“file2.txt”,true);

BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

System.out.println(“Enter some text “);

String data = br.readLine();

// convert data into equalent character array

char ch[] = data.toCharArray();

fw.write(ch);

fw.close();

} // end main

} // end class

Discussion (0)

Forem Open with the Forem app