0% found this document useful (0 votes)
24 views4 pages

Java Lec-54 Programs On File Handling.344f835

The document discusses two Java programs for file handling: 1) a program to display the contents of a text file, and 2) a program to copy the contents of one file into another file.

Uploaded by

Dnyanesh Chopade
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views4 pages

Java Lec-54 Programs On File Handling.344f835

The document discusses two Java programs for file handling: 1) a program to display the contents of a text file, and 2) a program to copy the contents of one file into another file.

Uploaded by

Dnyanesh Chopade
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Course: Java Programming, Prepared By: Atul Kabra, 9422279260

Java Lecture-54
Topic: Programs on File Handling

1) Write a program to display content of text file.


import java.io.*;
import java.util.*;
class FileRead
{
public static void main(String [] args)
{
Scanner in = new Scanner(System.in);

System.out.println("Enter the file name ");


String filename = in.nextLine();

try
{
//open a file with InputStream
FileInputStream fin = new FileInputStream(filename);

int x = fin.read(); //read byte from file

while(x!=-1) // repeat upto End of File


{
System.out.print((char)x); //print character of file
x=fin.read();
}

fin.close(); //closes the file


}
catch(FileNotFoundException e)
{
System.out.println("Error, File does not exists");
}
catch(IOException e)
{
System.out.println(e);

Course: Java Programming, Info Planet Programming Classes Prepared By: Atul Kabra, 9422279260
Course: Java Programming, Prepared By: Atul Kabra, 9422279260

}
} // main
} // class

Output

Course: Java Programming, Info Planet Programming Classes Prepared By: Atul Kabra, 9422279260
Course: Java Programming, Prepared By: Atul Kabra, 9422279260

Write a program to copy contents of one file into another file.


import java.io.*;
import java.util.*;

class FileCopy
{
public static void main(String [] args)
{
Scanner in = new Scanner(System.in);
System.out.println("Enter source file name ");
String sfile = in.nextLine();
try
{
FileInputStream fin = new FileInputStream(sfile);
System.out.println("Enter the destination file name ");
String dfile = in.nextLine();

FileOutputStream fout = new FileOutputStream(dfile);


int b = fin.read();
while(b!=-1)
{
fout.write((byte) b);
b= fin.read();
}
fin.close();
fout.close();
System.out.println("File is copied");
}
catch(FileNotFoundException e)
{
System.out.println("Can't copy, source file does not exists ");
}
catch(IOException e)
{
System.out.println(e);
}
}// main
}// class

Course: Java Programming, Info Planet Programming Classes Prepared By: Atul Kabra, 9422279260
Course: Java Programming, Prepared By: Atul Kabra, 9422279260

Output

Course: Java Programming, Info Planet Programming Classes Prepared By: Atul Kabra, 9422279260

You might also like