Discuss / Java / 作业

作业

Topic source

copy(new File("E:\\Test\\hello.txt"), new File("E:\\Test\\newHello.txt"));

public void copy(File sourceFile, File copyFile) throws IOException {
        if (sourceFile != null) {
            try (InputStream fit = new FileInputStream(sourceFile);
                 FileOutputStream fot = new FileOutputStream(copyFile)) {
                if (copyFile.exists()) {
                    copyFile.delete();
                }
                copyFile.createNewFile();

                int len;
                byte[] buffer = new byte[1024];
                while ((len = fit.read(buffer)) != -1) {
                    fot.write(buffer, 0, len);
                }

                System.out.println("copy success");
            }
        }
    }



  • 1

Reply