21 February 2022

Exercise Copy a file using a stream. Optional feature: third command line argument (true to clobber, false to bail)

java CopyStream donor recipient (optional true/false, true = clobber)

JDK with FX


//By David B
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardOpenOption;

public class CopyStream {
    public static void main(String[] args) {
        if (args.length < 2) {
            System.err.println("Incorrect usage: Copy.class <origin> <target>");
        }

        StandardOpenOption overwriteOrAdd = StandardOpenOption.APPEND;

        if (args.length == 3 && args[2].equals("false")) {
            System.out.println("Exiting without copying");
            System.exit(0);
        } else if (args.length == 3 && args[2].equals("true")) {
            overwriteOrAdd = StandardOpenOption.TRUNCATE_EXISTING;
        }

        Path dpath = Path.of(args[0]);
        Path rpath = Path.of(args[1]);
        try (BufferedReader donor = Files.newBufferedReader(dpath);
                BufferedWriter recipient = Files.newBufferedWriter(rpath, overwriteOrAdd, StandardOpenOption.CREATE);) {
            recipient.newLine();
            donor.lines().forEach(x -> {
                try {
                    recipient.write(x + "\n");
                } catch (IOException e) {
                    e.printStackTrace();
                }
            });
            // could also just do donor.transferTo(recipient);
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println("Copied Successfully");
    }

}

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.NoSuchFileException;
import java.nio.file.Path;
import java.util.Optional;
public class CopyStream {
    private Optional<String> fileContainerR;
    private String donor;
    private String recipient;
    private boolean replace;
    public CopyStream(String donor, String recipient, String replace){
        this.fileContainerR = Optional.of(recipient);
        this.donor = donor;
        this.recipient = recipient;
        if (replace.toLowerCase().equals("false")) this.replace = false;
        else this.replace = true;
    }
    private void handleCopy(String file1, String file2){
        Path start = Path.of(file1);
        Path end = Path.of(file2);
        
        if (!replace && fileContainerR.isPresent()){
            System.out.println("File is already present, and replace mode is off. 
Copy will not occur.");
            return;
        }
        
        try (BufferedReader br = Files.newBufferedReader(start); BufferedWriter bw 
= Files.newBufferedWriter(end)){
            br.lines()
                .forEach(line -> {
                    try {
                        bw.write(line); 
                        bw.newLine();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                });
            bw.close();
            System.out.println("The file has been copied over to the new 
destination");
        }
        catch  (NoSuchFileException ex){
            System.err.printf("Input File %s not found.\n", donor);
        }
        
        catch (IOException ex){
            ex.printStackTrace();
        }
    
    }
    public void copyFiles(){
        handleCopy(donor, recipient);
    }
    public static void main(String[] args){
        //Argument 0 is the donor file, Argument 1 is the recipient file, 
        //and Argument 2 is a boolean which states whether replace mode is on or 
off.
        CopyStream copy = new CopyStream(args[0], args[1], args[2]);
        copy.copyFiles();
    }
    
}

Get a Word Stream from a File

Get a character IntStream from a File