File Handling

Download as pdf or txt
Download as pdf or txt
You are on page 1of 9

File Handling

I/O Stream Classes


Que:
1. List out I/O classes in Java.
2. Explain java I/O process.
3. List different stream classes.
4. What is stream class? How are the stream classes classified?
5. Explain stream classes.
6. Describe stream classes in Java.
7. Explain basics of stream classes.
Ans:

 Java I/O is used to process the input and produce the output.
 Java program perform input and output through stream.
 A stream is linked to a physical device by the java I/O system to make input and output
operation in java.
 In general, Stream means continuous flow of data, a sequence of data.
 To bring in information, a program opens an input stream on an source and reads the
information sequentially, as shown here:

 A program can send information to destination by opening an output stream


to a destination and writing the information out sequentially.
 Java encapsulates Stream under java.io package.
 To use stream classes, we must import java.io package.
 The java.io package contains all the classes required for input and output
operations.
 Java defines two types of streams.

1. Byte Stream :

 It provides a convenient means for handling input and output of byte.

Page 1 of 9
 It is used when reading or writing binary data.

2. Character Stream :

 It provides a convenient means for handling input and output of characters.


 Character stream uses Unicode and therefore can be internationalized.

1. Byte Stream I/O Classes Hierarchy

 Byte streams are defined by using two classes hierarchies.


 At the top are two abstract classes: InputStream and OutputStream.
 The InputStream is used to read data from a source.
 The OutputStream is used for writing data to a destination.

 InputStream and OutputStream have several subclasses.


 Some important Byte stream classes are as follows:

Stream class Description

BufferedInputStream Used for Buffered Input Stream.

BufferedOutputStream Used for Buffered Output Stream.

DataInputStream Contains method for reading java standard datatype

DataOutputStream An output stream that contain method for writing java


standard data type

FileInputStream Input stream that reads from a file

FileOutputStream Output stream that write to a file.

InputStream Abstract class that describe stream input.

OutputStream Abstract class that describe stream output.

Page 2 of 9
PrintStream Output Stream that contain print() and println() method

 These classes define several key methods. Two most important are

1. read() : reads byte of data.

2. write() : Writes byte of data.

2.Character Stream I/O Classes Hierarchy

 Character stream are defined by using two classes hierarchies.


 At the top are two abstract classes: Reader and Writer.
 The Reader is used to read data from a source.
 The Writer is used for writing data to a destination.

 Reader and Writer have several subclasses.


 Some important Character stream classes are as follows:

Stream class Description

BufferedReader Handles buffered input stream.

BufferedWriter Handles buffered output stream.

FileReader Input stream that reads from file.

Page 3 of 9
FileWriter Output stream that writes to file.

InputStreamReader Input stream that translate byte to character

OutputStreamReader Output stream that translate character to byte.

PrintWriter Output Stream that contain print() and println() method.

Reader Abstract class that define character stream input

Writer Abstract class that define character stream output

 These classes define several key methods. Two most important are

1. read() : reads character of data.

2. write() : Writes character of data.

Reading and Writing Files

Que:
1. Explain how to create a text file in context to file handling.
2. Write Java syntax to create a new text file.
3. Write syntax of writing a text file in Java.
4. Write syntax of reading text file in Java.
Ans:

 In Java, all files are byte-oriented.


 Java provides methods to read and write bytes from and to a file.
 Two stream classes FileOutputStream and FileInputStream are used to read and write
file.
 Java FileOutputStream is an output stream used for writing data to a file.
 Java FileInputStream is an input stream used for reading data from a file.

Creating a File
Page 4 of 9
 To create a file use the following syntax:
 Ex:
File f = new File ("C:/java/hello.txt");

 While creating an object, it needs to specify name of file as an argument.

Reading a File

 To open a file for reading, create object of FileInputStream.


 While creating an object, it needs to specify name of file as an argument to
constructor.
 Ex:
FileInputStream f = new FileInputStream("C:/java/hello");

 If filename specifies does not exist, then FileNotFoundException is thrown.


 list of helper methods which can be used to read to stream or to do other operations on
the stream:

Sr.No. Method & Description

1
void close() throws IOException

- This method closes the file output stream.

- Releases any system resources associated with the file.

- Throws an IOException.

2
int read() throws IOException

-This method reads the single byte from file.

-Returns the byte as an integer.

-Returns the next byte of data and -1 will be returned if it's the end of the file.

 Example of FileInputStream class to read file

import java.io.*;

Page 5 of 9
class SimpleRead

public static void main(String args[])

try

FileInputStream fin=new FileInputStream("abc.txt");

int i;

while((i=fr.read())!=-1)

System.out.println((char)i);

fin.close();

catch(Exception e)

system.out.println(e);

Output: Hello world

Writing a File

 To open a file for writing, create object of FileOutputStream.


 While creating an object, it needs to specify name of file as an argument to
constructor.
 Ex:
FileOutputStream f = new FileOutputStream("C:/java/hello");

 If filename cannot be created, then FileNotFoundException is thrown.


Page 6 of 9
 list of helper methods which can be used to write to stream or to do other operations on
the stream:

Sr.No. Method & Description

1
void close() throws IOException

-This method closes the file output stream.

-Releases any system resources associated with the file.

-Throws an IOException.

2
void write(int w)throws IOException

This methods writes the specified byte to the output stream.

 Example of FileOutputStream class for writing into file:

import java.io.*;

class Test

public static void main(String args[])

try

File f1=new File(“D:/M.Java”);

FileOutputstream fout=new FileOutputStream(f1);

String s=“Hello world";

byte b[]=s.getBytes();

fout.write(b);

fout.close();

Page 7 of 9
System.out.println("success...");

catch(Exception e)

system.out.println(e);

Output:success...

 Example of Reading the data of current java file and writing it into another file

import java.io.*;

class C

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

File f1=new File(“D:/M.Java”);

FileInputStream fin=new FileInputStream("C.java");

FileOutputStream fout=new FileOutputStream("M.java");

int i=0;

while((i=fin.read())!=-1)

fout.write((byte)i);

fin.close();

System.out.println(“copying done!”);

Page 8 of 9
}

Page 9 of 9

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy