文件copy

xiaoxiao2021-02-28  96

import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; public class FileTest { /** * @param args * @throws Exception */ public static void main(String[] args) throws Exception { /* 判断文件名是否存在 File f = new File("D:/a/b/test.txt"); if (!f.exists()) { f.getParentFile().mkdirs(); f.createNewFile(); }*/ String url1 = "D:/a/b/mycv.docx";// 源文件路径 String url2 = "D:/a/b/new.docx";// 目标路径(复制到E盘,重命名为b.txt) copy(url1, url2); } private static void copy(String url1, String url2) throws Exception { FileInputStream in = new FileInputStream(new File(url1)); FileOutputStream out = new FileOutputStream(new File(url2)); byte[] buff = new byte[512]; int n = 0; while ((n = in.read(buff)) != -1) { out.write(buff, 0, n); } out.flush(); in.close(); out.close(); System.out.println("复制完成"); } }
转载请注明原文地址: https://www.6miu.com/read-40001.html

最新回复(0)