java中的copy文件程序 谁给个把文件copy到指定的路径的程序啊谢了a 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 import java.io.*;public class Test{ public static void main(String[] args) throws Exception { File a = new File("sourceFile"); System.out.println(a.getAbsolutePath()); System.out.println(a.renameTo(new File("aimFile"))); }} 晕,renameTo不知道什么意思啊 public static boolean copyTo(String src,String out) { try { File srcFile=new File(src); srcFile.renameTo(new File(out)); } catch (Exception e) { return false; } return true; }呵呵 写个工具方法更好用 上面的不对吧,那怎么是文件复制呢?import java.io.*;public class Copy { public static void copyFile(File src, File dest) { FileOutputStream fos = null; FileInputStream fis = null; try{ fis = new FileInputStream(src); fos = new FileOutputStream(dest); byte[] b = new byte[(int)src.length()]; fis.read(b); fos.write(b); fos.flush(); } catch(Exception e) { e.printStackTrace(); } finally { try{ if(null != fis) { fis.close(); } if(null != fos) { fos.close(); } } catch(Exception e) { e.printStackTrace(); } } } public static void main(String[] args) { String src = args[0].replace("\\","\\\\"); String dest = args[1].replace("\\","\\\\"); copyFile(new File(src), new File(dest)); }} renameTo只是在同一个磁盘分区上才会成功,而且使用renameTo是移动而不是复制哦。 public static void copyFiles(File src, File dest) { // 系统的隐藏文件或是源文件不存在,不进行Copy if (src.isHidden() || !src.exists()) { return; } // 对于文件夹需要递归的Copy if (src.isDirectory()) { if (!dest.exists()) { dest.mkdirs(); } File[] files = src.listFiles(); for (int i = 0; i < files.length; i++) { File destfile = new File(dest.getAbsolutePath() + File.separator + files[i].getName()); copyFiles(files[i], destfile); } } else { try { File destfile = dest; // 对于源文件是文件,而目标文件是目录的情况,将源文件以同名文件的形式Copy到目标目录下 if (dest.isDirectory()) { destfile = new File(dest.getAbsolutePath() + File.separator + src.getName()); } FileChannel sfc = new FileInputStream(src).getChannel(); FileChannel dfc = new FileOutputStream(destfile).getChannel(); sfc.transferTo(0, sfc.size(), dfc); sfc.close(); dfc.close(); } catch (IOException ex) { Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex); } } } 如果就是普通文件的话,这段就够了。而且这个速度比较快。 FileChannel sfc = new FileInputStream(src).getChannel(); FileChannel dfc = new FileOutputStream(destfile).getChannel(); sfc.transferTo(0, sfc.size(), dfc); sfc.close(); dfc.close(); toByteArray是ByteArrayOutputStream 的方法吗 字符串解析 新手问题麻烦大家看看~ 【求助】同一个程序在cmd中是死循环,在netbeans中正常~ 高手请进 截取字符串。在线等,向高手请教 [求助]求解Java基础题! 急 简单问题 100分 高手请指点,读文件为什末老报异常,谢谢! 这个在线游戏是如何实现的? 谁给个下载的网址了 谢谢了 我写的俄罗斯方块代码,有些小问题,代码发到这里,请各位帮忙看一下
{
public static void main(String[] args) throws Exception
{
File a = new File("sourceFile");
System.out.println(a.getAbsolutePath());
System.out.println(a.renameTo(new File("aimFile")));
}
}
晕,renameTo不知道什么意思啊
{
try {
File srcFile=new File(src);
srcFile.renameTo(new File(out));
} catch (Exception e) {
return false;
}
return true;
}呵呵 写个工具方法更好用
import java.io.*;public class Copy { public static void copyFile(File src, File dest) {
FileOutputStream fos = null;
FileInputStream fis = null;
try{
fis = new FileInputStream(src);
fos = new FileOutputStream(dest);
byte[] b = new byte[(int)src.length()];
fis.read(b);
fos.write(b);
fos.flush();
} catch(Exception e) {
e.printStackTrace();
} finally {
try{
if(null != fis) {
fis.close();
}
if(null != fos) {
fos.close();
}
} catch(Exception e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
String src = args[0].replace("\\","\\\\");
String dest = args[1].replace("\\","\\\\");
copyFile(new File(src), new File(dest));
}
}
// 系统的隐藏文件或是源文件不存在,不进行Copy
if (src.isHidden() || !src.exists()) {
return;
} // 对于文件夹需要递归的Copy
if (src.isDirectory()) {
if (!dest.exists()) {
dest.mkdirs();
}
File[] files = src.listFiles();
for (int i = 0; i < files.length; i++) {
File destfile = new File(dest.getAbsolutePath() + File.separator + files[i].getName());
copyFiles(files[i], destfile);
}
} else {
try {
File destfile = dest;
// 对于源文件是文件,而目标文件是目录的情况,将源文件以同名文件的形式Copy到目标目录下
if (dest.isDirectory()) {
destfile = new File(dest.getAbsolutePath() + File.separator + src.getName());
}
FileChannel sfc = new FileInputStream(src).getChannel();
FileChannel dfc = new FileOutputStream(destfile).getChannel();
sfc.transferTo(0, sfc.size(), dfc); sfc.close();
dfc.close();
} catch (IOException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
FileChannel dfc = new FileOutputStream(destfile).getChannel();
sfc.transferTo(0, sfc.size(), dfc); sfc.close();
dfc.close();