0% found this document useful (0 votes)
17 views

File Handling

Uploaded by

moradiyadhruv34
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views

File Handling

Uploaded by

moradiyadhruv34
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

UNIT V File Handling

➢ Java has features that support functions for input and output with the help of
stream bytes.
➢ java.io is the package that has support to all input and output related classes.

Basics of Streams
➢ Sequence of data is called as stream.
➢ To read data from the source, the input stream is used and to write data to a
source the output stream is used.
➢ In java the inputstream and outputstream are the basis stream classes.
➢ To perform input and output of 8 bit data, in java it uses java byte streams.

Java provides following three standard streams:

1. Standard Input:
We normally use a keyboard to feed data to the program as standard input
stream which is represented in System.in
2. Standard Output:
Normally a computer screen is used as a standard output stream and is
represented as System.out.
3. Standard Error:
During the execution of a program, the error data at the output which is
produced by the user program for which the computer screen is used as
standard error stream which is represented by System.err.

➢ Java has features to Supports two type of streams:


1. Byte Stream
2. Character stream

Stream classes
➢ In java the package called java.io which is categorized in two parts for which one
is for handling writing and reading of bytes i.e. ByteStream and other is for
reading and writing of characters i.e. CharacterStream.

Byte Stream:
➢ For handling input and output of bytes, Byte Stream is used.
➢ There are two abstract classes defined at the top for Byte Stream which are
InputStream and OutputStream.
➢ Below here the figure shows the hierarchy of the Byte Stream.

Byte Stream

InputStream OutputStream

ObjectInputStream FilterInputStream FileInputStream ObjectOutputStream FileoutputStream


FilterOutputStream

DataInputStream BufferedInputStream DataOutputstream BufferedOutputStream

Character Stream
➢ To handle input and output of characters, the Character Stream is used.
➢ It uses the Unicode that can be internationalized.
➢ Below is the figure that uses the class hierarchy of character Stream.
The object of InputStream has a list of methods that works as helper and are
used to read to stream or to perform any other operations on the stream.

Table 6.1 Methods


Sr. No Method Explanation
1 close() This method is used to close the file output stream.

2 read() To read data of specified byte the InputStream is used.

3 finalize() The close all the connections to the file, this method is used.

4 Available() To read and give number of bytes from the file, this is used.

➢ To create file and write data to the file, we can use the outputstream.
➢ If the file does exist then it is created before opening the file.
➢ A list of helper methods is available with the objects of OutputStream which can
be used to read the stream or perform some other operations.
Table 6.2 Methods
Sr. No Method Explanation
1 close() To shutdown the file, we use this.
To write specified number of bytes to the output stream, we
2 write()
use this.
3 finalize() To close the file and clean all the connections, we use this.

Creation of the Text File


➢ We use the class file to store the path and name of a directory or file.
➢ Method called as File.createNewFile() is used to create a new file in java which
returns a Boolean values either true or false.
➢ True if the file is created or false.
➢ This class has constructors like:
➢ File(String pathname); // pathname could be file or a directory name
File(String dirPathname, String filename);
File(File directory, String filename);

Example
import java.io.*;
public class FileDemo
{
public static void main( String[] args )
{
try {

File file = new File("D:\\f1.txt");

if (file.createNewFile()){
System.out.println("File is successfully created!");
}else{
System.out.println("File already exists.");
}
file.delete();
System.out.println("file is deleted");
} catch (IOException e) {
e.printStackTrace();
}
}
}
Output:
File is successfully created!
File is deleted

Reading and Writing Text files


FileInputStream Class

➢ To read data from the files we have to use FileInputStream.


➢ We have to create object with the help of new keyword for which several
constructors are available.
➢ Following constructor takes a file name as a string to create an input stream
object to read the file:

InputStream f = new FileInputStream("C:/f1.txt");

File f = new File("C:/f1.txt");


InputStream f = new FileInputStream(f);

Example:
import java.io.*;

class DemoRead
{
public static void main(String args[]){
try{
FileInputStream fin=new FileInputStream("f1.txt");
int i=0;
while((i=fin.read())!=-1){
System.out.println((char)i);
}
fin.close();
}catch(Exception e){system.out.println(e);}
}
}

Output:
Java Programming

FileOutputStream class

➢ FileOutputStream is used to create a file and write data into it.


➢ Here are two constructors which can be used to create a FileOutputStream
object.

OutputStream f = new FileOutputStream("C:/java/hello")

File f = new File("C:/java/hello");


OutputStream f = new FileOutputStream(f);

Example:
import java.io.*;

class Test
{
public static void main(String args[])
{
try{
FileOutputstream fout=new FileOutputStream("f1.txt");
String s="Java Programming";
byte b[]=s.getBytes();//converting string into byte array
fout.write(b);
fout.close();
System.out.println("Successfully Completed….");
}catch(Exception e){system.out.println(e);}
}
}
Output:
Successfully Completed

Video Links:

https://www.youtube.com/watch?v=ufdChySSFc0

https://www.youtube.com/watch?v=BxCbxfpwC7Q

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