Read data form one file and write in another file using byte oriented class:-
FileInputStream finput=null;
FileOutputStream foutput=null;
try{
finput=new FileInputStream("d:/nk.txt");
foutput=new FileOutputStream("d:/nk1.txt");
int c;
while((c=finput.read())!=-1){
foutput.write(c);
}
}catch(Exception e){
System.out.print(e);
}
finally{
if(finput!=null)
{
try {
finput.close();
} catch (IOException ex) {
}
}
if(foutput!=null){
try{
foutput.close();
}catch(Exception e){}
}
}
Read data form one file and write in another file using char oriented stream class:
FileReader fread=null;
FileWriter fwrite=null;
try{
fread=new FileReader("d:/nk.txt");
fwrite=new FileWriter("d:/nk2.txt");
int c;
while((c=fread.read())!=-1){
fwrite.write(c);
}
}catch(Exception e)
{
System.out.println(e);
}
finally{
try {
fread.close();
} catch (IOException ex) {
Logger.getLogger(CharStream.class.getName()).log(Level.SEVERE, null, ex);
}
try {
fwrite.close();
} catch (IOException ex) {
Logger.getLogger(CharStream.class.getName()).log(Level.SEVERE, null, ex);
}
}
Read Line by Line and Write Line by Line:
BufferedReader bread = null;
PrintWriter outprint = null;
try {
bread=new BufferedReader(new FileReader("d:/nk.txt"));
outprint=new PrintWriter(new FileWriter("d:/nk3.txt"));
String i;
while((i=bread.readLine())!=null){
outprint.println(i);
}
} catch (Exception e) {
}
finally{
try {
bread.close();
} catch (IOException ex) {
Logger.getLogger(LineReaderWriter.class.getName()).log(Level.SEVERE, null, ex);
}
outprint.close();
}
No comments:
Post a Comment