cmd指令(三个文件放在同一路径下)
javac CopyFile.java java CopyFile.java source.txt copy.txt
代码
package com.itranswarp.learnjava; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; /** * Learn Java from https://www.liaoxuefeng.com/ * * @author liaoxuefeng */ 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(args[0], args[1]); System.out.println("Done !"); } static void copy(String source, String target) throws IOException { // 友情提示:测试时请使用无关紧要的文件 // TODO: try (InputStream input = new FileInputStream(source); OutputStream output = new FileOutputStream(target)) { byte[] buffer = new byte[1024]; int n; String s; StringBuilder sb = new StringBuilder(); while ((n = input.read(buffer)) != -1) { sb.append(new String(buffer, "UTF-8" )); } s = sb.toString(); output.write(s.getBytes("UTF-8")); } } }
Sign in to make a reply
D
cmd指令(三个文件放在同一路径下)
代码