编写程序实现将一个文件复制到另一个文件中。
楼上的也叫简洁?import java.io.File;import java.nio.file.Files;public class copy {public static void main(String[] args)throws Exception{if (args.length < 2){System.out.println("请输入正确的参数");System.exit(1);}Files.copy(new File(args[0]).toPath(), new File(args[1]).toPath());}}
package cn.edu.jxnu.others; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.io.PrintWriter; public class FileReadToWrite { public static void main(String[] args) throws IOException { System.out.println("请依次输入:源文件路径 目标文件路径!"); if(args.length<2){ System.out.println("参数不合法!"); System.exit(0); } String path = args[0]; String target = args[1]; String resultString = FileReadToWrite.readFile(path); FileReadToWrite.writeToFile(target, resultString); } public static String readFile(String path) throws IOException { File file = new File(path); if(!file.exists()){ System.out.println("源文件不存在!"); System.exit(1); } BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(file), "utf-8")); char[] buf = new char[1024];// 设置一个1024字符的缓冲区 StringBuffer stringBuffer = new StringBuffer(); try { while (br.read(buf) > 0) { stringBuffer.append(buf); } } catch (Exception e) { e.printStackTrace(); } try { br.close(); } catch (Exception e) { e.printStackTrace(); } return stringBuffer.toString(); } /** * @description 复制的路径,和文件字节流 * @param path * @param bytes * @throws IOException */ public static void writeToFile(String path, String result) throws IOException { File f = new File(path); PrintWriter outPrintWriter = new PrintWriter( new BufferedWriter(new OutputStreamWriter(new FileOutputStream(f), "UTF-8"))); outPrintWriter.write(result); outPrintWriter.flush();// 迫使缓冲区被输出 outPrintWriter.close(); } }