Pages

Saturday, 7 January 2017

Chapter 17 Exercise 14, Introduction to Java Programming, Tenth Edition Y. Daniel LiangY.

17.14 (Encrypt files)
Encode the file by adding 5 to every byte in the file. Write a program
that prompts the user to enter an input file name and an output file name
and saves the encrypted version of the input file to the output file.


import java.io.*;

public class Exercise_14 {

    public static void main(String[] args) {

        if (args.length != 2) {
            System.out.println("Invalid number of arguments. Usage: src target");
        }

        try (BufferedInputStream in = new BufferedInputStream(new FileInputStream(args[0]));
             BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(args[1]))) {

            int buffer;
            while ((buffer = in.read()) != -1) {
                out.write(buffer + 5);
            }

            System.out.println("Encrypted file saved");
        } catch (IOException ex) {
            ex.printStackTrace();
        }

    }

}

No comments :

Post a Comment