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

Experiment-8 Abhishek Choudhary 07311503113 AIM

The document describes an experiment involving file handling in Java. It has three parts: (A) writes a string to a file, (B) reads the content of that file, and (C) copies the content from one file to another. It includes the code for each part and notes there is output, but does not show the output.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
55 views

Experiment-8 Abhishek Choudhary 07311503113 AIM

The document describes an experiment involving file handling in Java. It has three parts: (A) writes a string to a file, (B) reads the content of that file, and (C) copies the content from one file to another. It includes the code for each part and notes there is output, but does not show the output.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 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