FileCopying.java
27 linesjava
DOWNLOAD
1// Aim: Program to perform File Handling operations (File Copying).
2import java.io.FileInputStream;
3import java.io.FileOutputStream;
4import java.util.Scanner;
5public class FileCopying {
6    public static void main(String[] args) {
7        Scanner s = new Scanner(System.in);
8        System.out.print("enter file name to read with .txt: ");
9        String inf= s.nextLine(); 
10        System.out.print("enter file name to write with .txt: ");
11        String onf= s.nextLine(); 
12        try{
13            FileInputStream fr = new FileInputStream(inf);
14            FileOutputStream fw = new FileOutputStream(onf);
15            int data;
16            while ((data = fr.read()) != -1) {
17                fw.write(data);
18            }
19            System.out.println("File has been copied successfully.");
20            fr.close();
21            fw.close();
22        } catch (Exception e) {
23            System.out.println("An error occurred while processing the file: " + e.getMessage());
24        }
25        s.close();
26    }
27}