File Handling
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.
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
//step 2:
int c;
while((c = reader.read())!=-1)
{ System.out.println((char)c);
writer.write(c);
}
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;
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
====
package com.tcs;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
try
{ //step 1: create the channel
inputStream = new FileInputStream("ram.jpg");
outputStream = new FileOutputStream("usman.jpg");
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
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
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;
try
{ //step-1 : create the channel
reader = new BufferedReader(new FileReader("abc.txt"));
writer = new BufferedWriter(new FileWriter("guru.txt"));
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;
}
catch(IOException e)
{ e.printStackTrace();
}
System.out.println("operations are completed....");
}