Home > Java > javaTutorial > body text

Case Study: Copying Files

PHPz
Release: 2024-07-16 16:27:20
Original
1141 people have browsed it

This section develops a useful utility for copying files. In this section, you will learn how to write a program that lets users copy files. The user needs to provide a source file and a target file as command-line arguments using the command:

java Copy source target

The program copies the source file to the target file and displays the number of bytes in the file. The program should alert the user if the source file does not exist or if the target file already exists. A sample run of the program is shown in Figure below.

Image description

To copy the contents from a source file to a target file, it is appropriate to use an input stream to read bytes from the source file and an output stream to send bytes to the target file, regardless of the file’s contents. The source file and the target file are specified from the command line. Create an InputFileStream for the source file and an OutputFileStream for the target file. Use the read() method to read a byte from the input stream, and then use the write(b) method to write the byte to the output stream. Use BufferedInputStream and BufferedOutputStream to improve the performance. The code below gives the solution to the problem.

package demo;
import java.io.*;

public class Copy {
    public static void main(String[] args) throws IOException {
        // Check command-line parameter usage
        if(args.length != 2) {
            System.out.println("Usage: java Copy sourceFile targetfile");
            System.exit(1);
        }

        // Check if source file exists
        File sourceFile = new File(args[0]);
        if(!sourceFile.exists()) {
            System.out.println("Source file " + args[0] + " does not exist");
            System.exit(2);
        }

        // Check if source file exists
        File targetFile = new File(args[1]);
        if(!targetFile.exists()) {
            System.out.println("Target file " + args[1] + " already exist");
            System.exit(3);
        }

        try(
            // Create an input  stream
                BufferedInputStream input = new BufferedInputStream(new FileInputStream(sourceFile));

                // Create an output stream
                BufferedOutputStream output = new BufferedOutputStream(new FileOutputStream(targetFile));
            ) {
            // COntinuously read a byte from input and write it to output
            int r, numberOfBytesCopied = 0;
            while((r = input.read()) != -1) {
                output.write((byte)r);
                numberOfBytesCopied++;
            }

            // Display the file size
            System.out.println(numberOfBytesCopied + " bytes copied");
        }
    }
}

Copy after login

The program first checks whether the user has passed the two required arguments from the command line in lines 7–10.

The program uses the File class to check whether the source file and target file exist. If the source file does not exist (lines 14–17) or if the target file already exists (lines 20–24), the program ends.

An input stream is created using BufferedInputStream wrapped on FileInputStream in lines 28, and an output stream is created using BufferedOutputStream wrapped on FileOutputStream in lines 31.

The expression ((r = input.read()) != -1) (line 35) reads a byte from
input.read(), assigns it to r, and checks whether it is -1. The input value of -1 signifies the end of a file. The program continuously reads bytes from the input stream and sends them to the output stream until all of the bytes have been read.

The above is the detailed content of Case Study: Copying Files. For more information, please follow other related articles on the PHP Chinese website!

source:dev.to
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template