Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Experiment-8 Abhishek Choudhary 07311503113 AIM

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

EXPERIMENT-8

ABHISHEK CHOUDHARY
07311503113
AIM
File Handling
(A)To write content into a file
(B)To read content of a file
(C)To copy content of one file into another file
(A) SOURCE CODE
import java.io.*;
class write_ch
{
public static void main(String args[])
{
File outfile=new File("Demo.txt");
FileWriter fw=null;
try
{
fw=new FileWriter(outfile);
int ch;
String s="Happiness is when what you think,what you say and what you do are
in harmony";
fw.write(s);
}
catch(IOException ie)
{
ie.printStackTrace();
}
catch(Exception e)
{
e.printStackTrace();
}
finally
{
try
{
fw.close();
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
}

OUTPUT

(B) SOURCE CODE


import java.io.*;
class read_ch
{
public static void main(String args[])
{
File infile=new File("Demo.txt");
FileReader fr=null;
try
{
fr=new FileReader(infile);
int ch;
while((ch=fr.read())!=-1)
{
System.out.print((char)ch);
}
}
catch(IOException ie)
{
ie.printStackTrace();
}
catch(Exception e)
{
e.printStackTrace();
}
finally
{
try
{
fr.close();

}
catch(Exception e)
{
e.printStackTrace();
}
}
}
}
OUTPUT

(C) SOURCE CODE


import java.io.*;
class copy_ch
{
public static void main(String args[])
{
File infile=new File("Demo.txt");
FileReader fr=null;
File outfile=new File("NewDemo.txt");
FileWriter fw=null;
try
{
fr=new FileReader(infile);
fw=new FileWriter(outfile);
int ch;
while((ch=fr.read())!=-1)
{
fw.write((char)ch);
}
}
catch(IOException ie)
{
ie.printStackTrace();
}
catch(Exception e)
{
e.printStackTrace();
}

finally
{
try
{
fr.close();
fw.close();
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
}
OUTPUT

You might also like