File Copy Program

Classic programmed-learning exercises, refreshed for modern Java and presented in the current MrStyner.com portfolio style.

Modern Java noteThis archive has been refreshed for Java 25 LTS. Core language concepts remain useful, while outdated setup instructions and browser-era Java are labeled or replaced. Java 26 is the current feature release; Java 25 is used here as the stable teaching baseline.
go to previous page   go to home page   go to next page        

Could a byte read from a file by readUnsignedByte() be written to another file by writeByte().

Answer:

Yes. Both methods use the same part of an int for their data.

File Copy Program

The next example program is a file copy program. The text file copy program (of two chapters ago) did character-based IO with FileReader and FileWriter. It could not be used for binary files. The new copy program will do byte-oriented IO and will work with any file. The program is invoked from the command line, like this:

C:/Programs > java copyBytes sourceFile to destinationFile

As with the previous program, the "to" argument is there to remind the user of the correct order for the file names. The program operates by reading the original file one byte at a time and writing to the destination file one byte at a time.

QUESTION 14:

What two stream classes are likely to be useful?