Discuss / Java / CopyFile练习

CopyFile练习

Topic source

韦雪松

#1 Created at ... [Delete] [Delete and Lock User]
import java.io.*;

public class CopyFile {
    public static void main(String[] args) throws Exception {
        String source = "src/test.java";
        String target = "out/test.java";
        copy(source, target);
    }

    static void copy(String source, String target) throws IOException {
        byte[] buffer = new byte[1024 * 8];

        try (InputStream input = new FileInputStream(source);
             OutputStream output = new FileOutputStream(target)) {
            int length;
            while ((length = input.read(buffer)) != -1) {
                output.write(buffer, 0, length);
            }
        }
    }
}

  • 1

Reply