import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; /** * 利用InputStream和OutputStream,编写一个复制文件的程序,它可以带参数运行: */ public class copyTest { public static void main(String[] args) throws IOException { if (args.length != 2) { System.out.println("请输入2个参数运行(java CopyTest.java source.txt copy.txt)"); } else { copy(new File(args[0]), new File(args[1])); } } public static void copy(File sourceFile, File copyFile) throws IOException { try (InputStream in = new FileInputStream(sourceFile)) { int n; byte[] b = new byte[1024]; if ((n = in.read(b)) != -1) { try (OutputStream out = new FileOutputStream(copyFile)) { if (copyFile.exists()) { copyFile.delete(); } copyFile.createNewFile(); out.write(b, 0, n); } } } } }
Sign in to make a reply
大黑与small白