Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package copyfile;
- /*
- * This program accepts two filenames either at
- * the command-line or in the source code itself.
- *
- * To use this program in the command-line:
- *
- * java CopyFile filename1 filename2
- *
- * Otherwise, just type in the path to both filenames
- * in the source code.
- *
- * If the second file does not exist,
- * the program will create the file.
- */
- import java.io.*;
- // Copies a file to another file.
- class CopyFile {
- public static void main(String args[]) {
- String first;
- String second;
- String line;
- // Sets first and second using either command-line or source code
- if(args.length == 2) {
- first = args[0];
- second = args[1];
- } else {
- first = "src/copyfile/first.txt";
- second = "src/copyfile/second.txt";
- }
- // Tries to read and write from first to second.
- try(BufferedReader br = new BufferedReader(
- new FileReader(first));
- FileWriter fw = new FileWriter(second)) {
- do {
- line = br.readLine();
- if(line != null) {
- fw.write(line);
- }
- } while(line != null);
- } catch(FileNotFoundException exc) {
- System.out.println("FileNotFound Exception: File not found!");
- return;
- } catch(IOException exc) {
- System.out.println("I/O Exception: " + exc);
- return;
- }
- System.out.println("Successfully copied file!");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement