You are on page 1of 6

File Handling

=== *** ====

To perform IO operations we need classes & interfaces these are present in java.io
using java.io package classes & interfaces it is possible to work with only text
files.

abc.txt java xyz.txt

Channel : it is a communication medium to transfer the data.

java --Channel--- text

Every Channel can do two operations


read
write

There are two types of channels,


1. Byte oriented channel : The data is transfer in the form of bytes
: 8-bit : image data
FileInputStream : read the data
FileOutputStream : write the data

2. Character oriented Channel : The data is transfer in the form of characters:


16-bit : text data
FileReader : to Read the data
FileWriter : to write the data

steps to design the applcation:


step 1: create the channel.
step 2: read or write operations.
step 3: close the resources.

ex-1: abc.txt java xyz.txt


package com.tcs;

import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

public class CharacterStreamEx {

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

//step-1 : create the channel


FileReader reader = new FileReader("abc.txt");
FileWriter writer = new FileWriter("xyz.txt");

//step 2:
int c;
while((c = reader.read())!=-1)
{ System.out.println((char)c);
writer.write(c);
}

//step 3: close the resources


reader.close();
writer.close();
System.out.println("The resources are released.....");
}
}

To read the data file is mandatory But to write the data file is optional.
To write the data, If the file is not available then it will create and write the
data.
If the file is already exists it will override the data.

ex-2:
package com.tcs;

import java.io.File;
import java.io.IOException;

public class Test {

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

File file = new File("rani.txt");


System.out.println(file.exists());
boolean status = file.createNewFile();
if(status)
{ System.out.println("File is created
successfully..."+file.getPath());
}
else
{ System.out.println("File is already exists.....");
}
System.out.println(file.exists());

File f = new File("usman");


boolean b = f.mkdir();
if(b)
{ System.out.println("Directory Created successfully....");
}
else
{ System.out.println("Directory already exists....");
}

File ff = new File("usman","ratan.txt");


ff.createNewFile();
System.out.println("The file is created under the usman
directory.....");
}
}

ex-3: Assume abc.txt file contain some data


at last append some characters to the file "ratan"

ex-4:
create the directory : ratan
under the directory create the file : ram.txt
write some data to file : Good morning sir.

ex-5:
ratan (directory)
|--> ram.txt
remove the directory. (to remove the directory the dir should be empty...)

Day-2
====

ex-1: Handling the exceptions using try-catch-finally.


try
{ create the channel
read or write operations
}
catch ()
{ handle the exception
}
finally
{ close the resources
}

package com.tcs;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

public class ByteStreamExample {

public static void main(String[] args) {


//Resource declaration
FileInputStream inputStream = null;
FileOutputStream outputStream = null;

try
{ //step 1: create the channel
inputStream = new FileInputStream("ram.jpg");
outputStream = new FileOutputStream("usman.jpg");

//step 2: read or write operations.


int c;
while((c=inputStream.read())!=-1)
{ outputStream.write(c);
}
System.out.println("operations are completed...");
}
catch(IOException e)
{
e.printStackTrace();
}
finally
{
//Step-3 : release the resources
try{
if(inputStream!=null)inputStream.close();
if(outputStream!=null)outputStream.close();
}
catch(IOException e)
{ e.printStackTrace();
}
}
}
}

a. Close the resources using finally block, it will close the resources both normal
cases & abnormal cases.
b. Before releasing the resources check the resources are existed or not.
c. Declare the resource outside the try block so that we can access the resources
in try-catch-finally.

ex: Assignment
Assume abc.txt file contains some data
hi sir Good morning
how are you

write first 2-chars to : a.txt : hi


write first 6-chars to : b.txt : hi sir
write first 11-chars to : c.txt : hi sir good

Day-3 BufferedStream
Day-3 BufferedStream
Normal Stream :
FileReader
FileWriter

FileInputStream
FileOutputStream

a. Reads the data char by char , If the file contains more characters it required
more read & write operations it will effect on performance.
b. normal stream interact with HardDisk to read the data.

c. Object reation
new FileReader("abc.txt");
new FileWriter("anu.txt");
d. Normal streams read the data from HardDisk.

BufferedStreams:
BufferedReader
BufferedWriter

BufferedInputStream
BufferedOutputStream

a. Reads the data line by line format, it improves the performance.


b. first time read the data from harddisk it will store into buffered memory, Later
if we need the data it will read from buffered memory.

c. object creation : buffered streams are developed based on normal streams.


new BufferedReader(new FileReader("abc.txt"))
new BufferedWriter(new FileWriter("anu.txt"))

d. Buffered Streams reads the data from buffered memory.

ex-1:
package com.tcs;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

public class BufferedStreamEx1 {

public static void main(String[] args) {

BufferedReader reader = null;


BufferedWriter writer = null;

try
{ //step-1 : create the channel
reader = new BufferedReader(new FileReader("abc.txt"));
writer = new BufferedWriter(new FileWriter("guru.txt"));

//step 2: Read & write operations


String line;

while((line = reader.readLine())!=null)
{
writer.write(line);
writer.newLine();
}

}
catch(IOException e)
{
e.printStackTrace();
}
finally{
//step 3: Release the resources
try{
if(reader!=null)reader.close();
if(writer!=null)writer.close();
}
catch(IOException e)
{ e.printStackTrace();
}
}
System.out.println("Resources are released successfully...");
}
}

ex-2:
package com.tcs;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

public class BufferedStreamEx2 {

public static void main(String[] args) {

try(BufferedInputStream inputStream = new BufferedInputStream(new


FileInputStream("ram.jpg"));
BufferedOutputStream outputStream = new BufferedOutputStream(new
FileOutputStream("guru.jpg")))
{
int c;
while((c = inputStream.read())!=-1)
{ outputStream.write(c);
}

}
catch(IOException e)
{ e.printStackTrace();
}
System.out.println("operations are completed....");
}

Assignment : Assume abc.txt file contains some data


hi ratan sir
ratan sir great
ratan sir good
java by ratan
count the number of occurences of "ratan" in the file.

You might also like