public boolean copyTo(String strSourceFileName, String strDestDir) { File fileSource = new File(strSourceFileName); File fileDest = new File(strDestDir); // 如果源文件不存或源文件是文件夹 if (!fileSource.exists() || !fileSource.isFile()) { System.out.println("错误: FileOperator.java copyTo函数,\n原因: 源文件[" + strSourceFileName + "],不存在或是文件夹!"); return false; } // 如果目标文件夹不存在 if (!fileDest.isDirectory() || !fileDest.exists()) { if (!fileDest.mkdirs()) { System.out .println("错误: FileOperator.java copyTo函数,\n原因:目录文件夹不存,在创建目标文件夹时失败!"); return false; } } try { String strAbsFilename = strDestDir + File.separator + fileSource.getName(); FileInputStream fileInput = new FileInputStream(strSourceFileName); FileOutputStream fileOutput = new FileOutputStream(strAbsFilename); int i = 0; int count = -1; long nWriteSize = 0; long nFileSize = fileSource.length(); byte[] data = new byte[BUFFER]; while (-1 != (count = fileInput.read(data, 0, BUFFER))) { fileOutput.write(data, 0, count); nWriteSize += count; long size = (nWriteSize * 100) / nFileSize; long t = nWriteSize; String msg = null; if (size <= 100 && size >= 0) { msg = "\r拷贝文件进度: " + size + "% \t" + "\t 已拷贝: " + t; } else if (size > 100) { msg = "\r拷贝文件进度: " + 100 + "% \t" + "\t 已拷贝: " + t; } } fileInput.close(); fileOutput.close(); System.out.println("\n拷贝文件成功!"); return true; } catch (Exception e) { System.out.println("异常信息:["); e.printStackTrace(); return false; } }
public boolean copyFileToFile(String fromFile, String sFile) 用的时候 调用这个就可以
1。用系统命令吧。 2。用File的renameTo。例子下面给。
package org.apache.tools.ant.taskdefs;import java.io.File; import java.io.IOException; import org.apache.tools.ant.BuildException; import org.apache.tools.ant.Project; import org.apache.tools.ant.Task; import org.apache.tools.ant.util.FileUtils;/** * Renames a file. * * @deprecated The rename task is deprecated since Ant 1.2. Use move instead. * @since Ant 1.1 */ public class Rename extends Task { private static final FileUtils FILE_UTILS = FileUtils.getFileUtils(); private File src; private File dest; private boolean replace = true; /** * Sets the file to be renamed. * @param src the file to rename */ public void setSrc(File src) { this.src = src; } /** * Sets the new name of the file. * @param dest the new name of the file. */ public void setDest(File dest) { this.dest = dest; } /** * Sets whether an existing file should be replaced. * @param replace <code>on</code>, if an existing file should be replaced. */ public void setReplace(String replace) { this.replace = Project.toBoolean(replace); } /** * Renames the file <code>src</code> to <code>dest</code> * @exception org.apache.tools.ant.BuildException The exception is * thrown, if the rename operation fails. */ public void execute() throws BuildException { log("DEPRECATED - The rename task is deprecated. Use move instead."); if (dest == null) { throw new BuildException("dest attribute is required", getLocation()); } if (src == null) { throw new BuildException("src attribute is required", getLocation()); } if (!replace && dest.exists()) { throw new BuildException(dest + " already exists."); } try { FILE_UTILS.rename(src, dest); } catch (IOException e) { throw new BuildException("Unable to rename " + src + " to " + dest, e, getLocation()); } } }
File oSource = new File("e:\\1.txt"); File oDesc = new File("c:\\test.txt"); System.out.println(oSource.renameTo(oDesc));
public static void copy(File source, File dest) throws IOException { FileChannel in = null, out = null; try { in = new FileInputStream(source).getChannel(); out = new FileOutputStream(dest).getChannel(); long size = in.size(); MappedByteBuffer buf = in.map(FileChannel.MapMode.READ_ONLY, 0, size); out.write(buf); } finally { if (in != null) in.close(); if (out != null) out.close(); } }这个方法也比较快。
File fileSource = new File(strSourceFileName);
File fileDest = new File(strDestDir); // 如果源文件不存或源文件是文件夹
if (!fileSource.exists() || !fileSource.isFile()) {
System.out.println("错误: FileOperator.java copyTo函数,\n原因: 源文件["
+ strSourceFileName + "],不存在或是文件夹!");
return false;
} // 如果目标文件夹不存在
if (!fileDest.isDirectory() || !fileDest.exists()) {
if (!fileDest.mkdirs()) {
System.out
.println("错误: FileOperator.java copyTo函数,\n原因:目录文件夹不存,在创建目标文件夹时失败!");
return false;
}
} try {
String strAbsFilename = strDestDir + File.separator
+ fileSource.getName(); FileInputStream fileInput = new FileInputStream(strSourceFileName);
FileOutputStream fileOutput = new FileOutputStream(strAbsFilename); int i = 0;
int count = -1; long nWriteSize = 0;
long nFileSize = fileSource.length(); byte[] data = new byte[BUFFER]; while (-1 != (count = fileInput.read(data, 0, BUFFER))) {
fileOutput.write(data, 0, count);
nWriteSize += count;
long size = (nWriteSize * 100) / nFileSize;
long t = nWriteSize;
String msg = null;
if (size <= 100 && size >= 0) {
msg = "\r拷贝文件进度: " + size + "% \t" + "\t 已拷贝: " + t;
} else if (size > 100) {
msg = "\r拷贝文件进度: " + 100 + "% \t" + "\t 已拷贝: " + t;
}
} fileInput.close();
fileOutput.close(); System.out.println("\n拷贝文件成功!");
return true; } catch (Exception e) {
System.out.println("异常信息:[");
e.printStackTrace();
return false;
}
}
//文件有效性
boolean ret = false;
if (!fromFile.exists() || !fromFile.isFile()) {
throw new Exception("源文件["
+ fromFile.toString() + "]不存在!");
}
if (!toFile.exists() || !toFile.isFile()) {
throw new Exception("目标文件["
+ toFile.toString() + "]不存在,!");
} else {
if (!toFile.canWrite()) {
throw new Exception("目标文件不可写!");
}
}
FileInputStream fileInput = null;
FileOutputStream fileOutput = null;
// 如果目标文件夹不存在
try {
fileInput = new FileInputStream(fromFile);
fileOutput = new FileOutputStream(toFile); int count = -1; long nWriteSize = 0;
long nFileSize = fromFile.length(); byte[] data = new byte[BUFFER]; while (-1 != (count = fileInput.read(data, 0, BUFFER))) {
fileOutput.write(data, 0, count);
}
fileOutput.flush();
ret = !ret;
} catch (Exception e) {
e.printStackTrace();
throw e;
} finally {
if(fileInput!=null)fileInput.close();
if(fileOutput!=null)fileOutput.close();
}
return ret;
}
/**
* 拷贝文件
* @param String fromFile 来源文件的全路径
* @param String sFile 目标文件的全路径
* @return
* @throws Exception
*/
public boolean copyFileToFile(String fromFile, String sFile) throws Exception {
File fileFrom=new File(fromFile);
File fileTo=new File(sFile);
if(!fileTo.exists()){
fileTo.createNewFile();
}
return this.copyFileToFile(fileFrom,fileTo);
}
File fileSource = new File(strSourceFileName);
File fileDest = new File(strDestDir); // 如果源文件不存或源文件是文件夹
if (!fileSource.exists() || !fileSource.isFile()) {
System.out.println("错误: FileOperator.java copyTo函数,\n原因: 源文件["
+ strSourceFileName + "],不存在或是文件夹!");
return false;
} // 如果目标文件夹不存在
if (!fileDest.isDirectory() || !fileDest.exists()) {
if (!fileDest.mkdirs()) {
System.out
.println("错误: FileOperator.java copyTo函数,\n原因:目录文件夹不存,在创建目标文件夹时失败!");
return false;
}
} try {
String strAbsFilename = strDestDir + File.separator
+ fileSource.getName(); FileInputStream fileInput = new FileInputStream(strSourceFileName);
FileOutputStream fileOutput = new FileOutputStream(strAbsFilename); int i = 0;
int count = -1; long nWriteSize = 0;
long nFileSize = fileSource.length(); byte[] data = new byte[BUFFER]; while (-1 != (count = fileInput.read(data, 0, BUFFER))) {
fileOutput.write(data, 0, count);
nWriteSize += count;
long size = (nWriteSize * 100) / nFileSize;
long t = nWriteSize;
String msg = null;
if (size <= 100 && size >= 0) {
msg = "\r拷贝文件进度: " + size + "% \t" + "\t 已拷贝: " + t;
} else if (size > 100) {
msg = "\r拷贝文件进度: " + 100 + "% \t" + "\t 已拷贝: " + t;
}
} fileInput.close();
fileOutput.close(); System.out.println("\n拷贝文件成功!");
return true; } catch (Exception e) {
System.out.println("异常信息:[");
e.printStackTrace();
return false;
}
}
用的时候 调用这个就可以
2。用File的renameTo。例子下面给。
import java.io.IOException;
import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.Project;
import org.apache.tools.ant.Task;
import org.apache.tools.ant.util.FileUtils;/**
* Renames a file.
*
* @deprecated The rename task is deprecated since Ant 1.2. Use move instead.
* @since Ant 1.1
*/
public class Rename extends Task { private static final FileUtils FILE_UTILS = FileUtils.getFileUtils(); private File src;
private File dest;
private boolean replace = true;
/**
* Sets the file to be renamed.
* @param src the file to rename
*/
public void setSrc(File src) {
this.src = src;
} /**
* Sets the new name of the file.
* @param dest the new name of the file.
*/
public void setDest(File dest) {
this.dest = dest;
} /**
* Sets whether an existing file should be replaced.
* @param replace <code>on</code>, if an existing file should be replaced.
*/
public void setReplace(String replace) {
this.replace = Project.toBoolean(replace);
}
/**
* Renames the file <code>src</code> to <code>dest</code>
* @exception org.apache.tools.ant.BuildException The exception is
* thrown, if the rename operation fails.
*/
public void execute() throws BuildException {
log("DEPRECATED - The rename task is deprecated. Use move instead."); if (dest == null) {
throw new BuildException("dest attribute is required", getLocation());
} if (src == null) {
throw new BuildException("src attribute is required", getLocation());
} if (!replace && dest.exists()) {
throw new BuildException(dest + " already exists.");
} try {
FILE_UTILS.rename(src, dest);
} catch (IOException e) {
throw new BuildException("Unable to rename " + src + " to "
+ dest, e, getLocation());
}
}
}
String[] command = {"cmd.exe", "/c", "copy e:\\1.txt c:\\test.txt /Y"};
try {
Runtime.getRuntime().exec(command);
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("Finished..Copy");读文件觉得是一个不好的方法。
File oDesc = new File("c:\\test.txt");
System.out.println(oSource.renameTo(oDesc));
FileChannel in = null, out = null;
try {
in = new FileInputStream(source).getChannel();
out = new FileOutputStream(dest).getChannel(); long size = in.size();
MappedByteBuffer buf = in.map(FileChannel.MapMode.READ_ONLY, 0, size); out.write(buf); } finally {
if (in != null) in.close();
if (out != null) out.close();
}
}这个方法也比较快。
直接搞定
try {
Runtime.getRuntime().exec(command);
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("Finished..Copy");
这个好