散分60分,求教java 复制文件夹及其中文件,复制后文件的相对路径保持不变,请高手帮忙!在线等。。。 本帖最后由 fengfenglucky 于 2010-03-17 13:03:00 编辑 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 你一共有7分 怎么散60分?public static void getAllFile(String rootpath, String filepath, ArrayList<String> flist, boolean isconsubdir) { // 获得某个目录下的所有文件列表(可以包括子目录) String ffullpath = rootpath + File.separator + filepath; File file = new File(ffullpath); if (file.exists() && file.isDirectory()) { String[] fileStr = file.list(); for (int i = 0; i < fileStr.length; i++) { if (new File(ffullpath + File.separator + fileStr[i]) .isDirectory()) { if (isconsubdir) { // 是否包含子目录文件 flist.add("#");// 纪录以下是是目录 flist.add(filepath + File.separator + fileStr[i]); getAllFile(rootpath, filepath + File.separator + fileStr[i], flist, isconsubdir); } } else { flist.add(filepath + File.separator + fileStr[i]); System.out.println(filepath + File.separator + fileStr[i]); } } } }//调用此方法 复制文件函数public void creatFile(String oldPath, String newPath,String type) { String temp; int dirnum = 0; ArrayList<String> flist = new ArrayList<String>(); ReadFileandDir.getAllFile(oldPath, "", flist, true);//得到某个目录下的全部文件和文件夹 while (flist.size() != 0) { temp = flist.get(0);// if (temp.endsWith(type))// System.out.println(temp); if (temp.startsWith("#")) { dirnum++; } else { if (dirnum > 0) { //System.out.println("below is dir"); new File(newPath + temp).mkdir(); dirnum--; } else { // 如果是文件则dosometing if (temp.endsWith(type)) BmpToBit(oldPath + temp); //BMPReverse(oldPath + temp, newPath + temp); } } flist.remove(0); } } public static void main(String[] arg) { ReadBMP readBMP = new ReadBMP(); String oldPath = "D:/oldbimp"; String newPath = "d:/bimp"; //readBMP.creatFile(oldPath, newPath,".bmp"); readBMP.BmpToBit("D://11.bmp"); // readBMP.ReadFile("D:\\oldbimp\\Bliss11.bmp","d:\\bimp\\1.txt"); } import java.io.*;public class CopyFiles { public static void Copy(String src, String dst) { File srcFile = new File(src); File dstFile = new File(dst); if (!srcFile.exists() || srcFile.isFile()) return; if (!dstFile.exists() && !dstFile.mkdirs()) return; File srcChilds[] = srcFile.listFiles(); for (int i=0; i<srcChilds.length; ++i) { // Generate source and destination path String srcpath = srcChilds[i].getAbsolutePath(); String dstpath = dstFile.getAbsolutePath() + srcpath.substring(srcFile.getAbsolutePath().length()); if (srcChilds[i].isFile()) { // Copy file to destination FileInputStream in = null; FileOutputStream out = null; byte buffer[] = new byte[8192]; int bytesRead; try { in = new FileInputStream(srcpath); out = new FileOutputStream(dstpath); while ((bytesRead = in.read(buffer)) != -1) { out.write(buffer, 0, bytesRead); } } catch (Exception e) {} if (null != in) try { in.close(); } catch (Exception e) {} if (null != out) try { out.close(); } catch (Exception e) {} } else { // Recursion for directory Copy(srcpath, dstpath); } } } public static void main(String[] args) { if (args.length != 2) System.err.println("Wrong parameters. Correct usage: java CopyFiles <srcDir> <dstDir>"); else { Copy(args[0], args[1]); } } }保存成CopyFiles.java,先用javac编译,然后再调用,例如:java CopyFiles d:\temp d:\newtemp 看到你的问题,我随便写了一下。当中存在许多不足,但是你需要的功能可以实现。你自己按照思路,修改这个代码就可以了。public static void main(String[] args) throws IOException { File srcFile = new File("F:\\test\\test.java");//你需要copy的文件 String relativePath = "\\test\\";//你需要指定相对路径是什么 InputStream is = new FileInputStream(srcFile);//读取文件 String tarPath = "E:\\bak" + relativePath;//E:\\bak是你要考的路径,后面的是你所说的相对路径 File tarF = new File(tarPath);// tarF.mkdirs();//创建文件夹 File tarFile = new File(tarPath + srcFile.getName());// tarFile.createNewFile();//创建文件 OutputStream os = new FileOutputStream(tarFile);//输出 int i; byte[] b = new byte[255]; while ((i = is.read(b)) != -1) { os.write(b); } is.close(); os.flush(); os.close(); } Runtime.getRuntime().exec("COPY " + sourcePath + " " + targetPath);就可以了。 求帮助:新手请教2个java的小问题。 关于一个测试线程的小程序 刚学java 求高手指点 谢谢 怎么处理这样的式子,给个思路就行。 请教:正则表达式中替换的问题 移动MOUSE如何判断MOUSE移动的方向,就是说如果,我把MOUSE向上下左右4个方向移动,怎么判断是向哪个方向移动. Java开发MapInfo程序 老师昨天刚出的一道回家作业:一个旋转二维数组,望各位大哥指点迷泾? JBuilder8中编辑JSP文件不能保存问题 郁闷. 生气..... 散分.... jdbc驱动for oracle? 50分求解。谢谢! JavaMail的问题 问个问题,高手请进
ArrayList<String> flist, boolean isconsubdir) {
// 获得某个目录下的所有文件列表(可以包括子目录)
String ffullpath = rootpath + File.separator + filepath;
File file = new File(ffullpath); if (file.exists() && file.isDirectory()) {
String[] fileStr = file.list();
for (int i = 0; i < fileStr.length; i++) {
if (new File(ffullpath + File.separator + fileStr[i])
.isDirectory()) {
if (isconsubdir) {
// 是否包含子目录文件
flist.add("#");// 纪录以下是是目录
flist.add(filepath + File.separator + fileStr[i]);
getAllFile(rootpath, filepath + File.separator
+ fileStr[i], flist, isconsubdir);
}
} else {
flist.add(filepath + File.separator + fileStr[i]);
System.out.println(filepath + File.separator + fileStr[i]);
}
} }
}
//调用此方法 复制文件函数
public void creatFile(String oldPath, String newPath,String type) { String temp;
int dirnum = 0;
ArrayList<String> flist = new ArrayList<String>();
ReadFileandDir.getAllFile(oldPath, "", flist, true);//得到某个目录下的全部文件和文件夹
while (flist.size() != 0) {
temp = flist.get(0);
// if (temp.endsWith(type))
// System.out.println(temp);
if (temp.startsWith("#")) {
dirnum++;
} else {
if (dirnum > 0) {
//System.out.println("below is dir");
new File(newPath + temp).mkdir();
dirnum--;
} else {
// 如果是文件则dosometing
if (temp.endsWith(type))
BmpToBit(oldPath + temp);
//BMPReverse(oldPath + temp, newPath + temp);
}
}
flist.remove(0);
}
} public static void main(String[] arg) {
ReadBMP readBMP = new ReadBMP();
String oldPath = "D:/oldbimp";
String newPath = "d:/bimp";
//readBMP.creatFile(oldPath, newPath,".bmp");
readBMP.BmpToBit("D://11.bmp");
// readBMP.ReadFile("D:\\oldbimp\\Bliss11.bmp","d:\\bimp\\1.txt"); }
public static void Copy(String src, String dst) {
File srcFile = new File(src);
File dstFile = new File(dst);
if (!srcFile.exists() || srcFile.isFile()) return;
if (!dstFile.exists() && !dstFile.mkdirs()) return;
File srcChilds[] = srcFile.listFiles();
for (int i=0; i<srcChilds.length; ++i) {
// Generate source and destination path
String srcpath = srcChilds[i].getAbsolutePath();
String dstpath = dstFile.getAbsolutePath() + srcpath.substring(srcFile.getAbsolutePath().length());
if (srcChilds[i].isFile()) {
// Copy file to destination
FileInputStream in = null;
FileOutputStream out = null;
byte buffer[] = new byte[8192];
int bytesRead;
try {
in = new FileInputStream(srcpath);
out = new FileOutputStream(dstpath);
while ((bytesRead = in.read(buffer)) != -1) {
out.write(buffer, 0, bytesRead);
}
} catch (Exception e) {}
if (null != in) try { in.close(); } catch (Exception e) {}
if (null != out) try { out.close(); } catch (Exception e) {}
} else {
// Recursion for directory
Copy(srcpath, dstpath);
}
}
}
public static void main(String[] args) {
if (args.length != 2)
System.err.println("Wrong parameters. Correct usage: java CopyFiles <srcDir> <dstDir>");
else {
Copy(args[0], args[1]);
}
}
}保存成CopyFiles.java,先用javac编译,然后再调用,例如:java CopyFiles d:\temp d:\newtemp
你自己按照思路,修改这个代码就可以了。public static void main(String[] args) throws IOException {
File srcFile = new File("F:\\test\\test.java");//你需要copy的文件
String relativePath = "\\test\\";//你需要指定相对路径是什么
InputStream is = new FileInputStream(srcFile);//读取文件
String tarPath = "E:\\bak" + relativePath;//E:\\bak是你要考的路径,后面的是你所说的相对路径
File tarF = new File(tarPath);//
tarF.mkdirs();//创建文件夹
File tarFile = new File(tarPath + srcFile.getName());//
tarFile.createNewFile();//创建文件
OutputStream os = new FileOutputStream(tarFile);//输出
int i;
byte[] b = new byte[255];
while ((i = is.read(b)) != -1) {
os.write(b);
}
is.close();
os.flush();
os.close();
}
就可以了。