Discuss / Java / 交作业

交作业

Topic source

package IO;

import java.io.*;

public class CopyFile {

    public static void main(String[] args) throws IOException {

        if (args.length != 2) {

            System.err.println("Usage:\n  java CopyFile.java <source> <target>");

            System.exit(1);

        }

        copy("src/readme.txt", "src/IO/copy.txt");

    }

    static void copy(String source, String target) throws IOException {

        // 友情提示:测试时请使用无关紧要的文件

        // TODO:

        File f = new File(source);

        int file_length = (int) f.length();

        try (InputStream input = new FileInputStream(source)) {

            byte[] buffer = new byte[file_length];

            int n;

            while ((n = input.read(buffer)) != -1) {

                System.out.println("准备复制!\n文件大小是:" + file_length + "字节");

            }

            try (OutputStream output = new FileOutputStream(target)) {

                output.write(buffer);

                System.out.println("复制完成!");

            }

        }

    }

}


  • 1

Reply