用java实现本地文件或者文件夹的复制,这个程序其实没什么难度!直接贴一下代码吧!!
import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; public class Test { private static int a = 5; public static void main(String[] args) { //需要复制的目标文件或目标文件夹 String pathname = "C:/Users/zhangsan/Desktop/git"; File file = new File(pathname); //复制到的位置 String topathname = "C:/Users/zhangsan/Desktop/"; File toFile = new File(topathname); try { copy(file, toFile); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } public static void copy(File file, File toFile) throws Exception { byte[] b = new byte[1024]; int a; FileInputStream fis; FileOutputStream fos; if (file.isDirectory()) { String filepath = file.getAbsolutePath(); filepath=filepath.replaceAll("\\\\", "/"); String toFilepath = toFile.getAbsolutePath(); toFilepath=toFilepath.replaceAll("\\\\", "/"); int lastIndexOf = filepath.lastIndexOf("/"); toFilepath = toFilepath + filepath.substring(lastIndexOf ,filepath.length()); File copy=new File(toFilepath); //复制文件夹 if (!copy.exists()) { copy.mkdir(); } //遍历文件夹 for (File f : file.listFiles()) { copy(f, copy); } } else { if (toFile.isDirectory()) { String filepath = file.getAbsolutePath(); filepath=filepath.replaceAll("\\\\", "/"); //同意文件的分割符号 String toFilepath = toFile.getAbsolutePath(); toFilepath=toFilepath.replaceAll("\\\\", "/"); int lastIndexOf = filepath.lastIndexOf("/"); toFilepath = toFilepath + filepath.substring(lastIndexOf ,filepath.length()); //写文件 File newFile = new File(toFilepath); fis = new FileInputStream(file); fos = new FileOutputStream(newFile); while ((a = fis.read(b)) != -1) { fos.write(b, 0, a); } } else { //写文件 fis = new FileInputStream(file); fos = new FileOutputStream(toFile); while ((a = fis.read(b)) != -1) { fos.write(b, 0, a); } } } } }
本地的文件复制实现了,那么可以再写一个加上Socket实现网络传送文件或者文件夹!!
以后与有时间再来总结下吧!!
还没有评论,来说两句吧...