java文件操作,简单,急用!谢谢

kaymo 2005-11-15 11:35:52
怎样把一个磁盘文件(比如xml,txt文件)从一个文件目录copy到另外一个目录,并且换成指定的文件名

谢谢!!!!!!!!!!!
...全文
466 27 打赏 收藏 转发到动态 举报
写回复
用AI写文章
27 条回复
切换为时间正序
请发表友善的回复…
发表回复
nwpulipeng 2005-11-15
  • 打赏
  • 举报
回复
哦,看错了,renameTo可以移动文件,但是不能实现拷贝
nwpulipeng 2005-11-15
  • 打赏
  • 举报
回复
File.renameto(File newname)
直接搞定
Croatia 2005-11-15
  • 打赏
  • 举报
回复
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();
}
}

这个方法也比较快。
Croatia 2005-11-15
  • 打赏
  • 举报
回复
有人说,移植的话,可以判断一下系统,然后执行不同的命令就可以了。
Croatia 2005-11-15
  • 打赏
  • 举报
回复
File oSource = new File("e:\\1.txt");
File oDesc = new File("c:\\test.txt");
System.out.println(oSource.renameTo(oDesc));

ahFaye 2005-11-15
  • 打赏
  • 举报
回复
命令好像移植不太好
gemouzhi 2005-11-15
  • 打赏
  • 举报
回复
楼上的方法是最好的
gemouzhi 2005-11-15
  • 打赏
  • 举报
回复
一句话的事,搞的这么麻烦
Croatia 2005-11-15
  • 打赏
  • 举报
回复
命令的方式:
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");

读文件觉得是一个不好的方法。
bob_thb 2005-11-15
  • 打赏
  • 举报
回复
搞定了吗?
Croatia 2005-11-15
  • 打赏
  • 举报
回复
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());
}
}
}
Croatia 2005-11-15
  • 打赏
  • 举报
回复
1。用系统命令吧。
2。用File的renameTo。

例子下面给。
ahFaye 2005-11-15
  • 打赏
  • 举报
回复
public boolean copyFileToFile(String fromFile, String sFile)
用的时候 调用这个就可以
老無所依 2005-11-15
  • 打赏
  • 举报
回复
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;
}
}
kaymo 2005-11-15
  • 打赏
  • 举报
回复
先式式,ok就给分
谢了
vvpang 2005-11-15
  • 打赏
  • 举报
回复
楼上的 msg 的信息,怎么没有 System.out. 啊。。
ahFaye 2005-11-15
  • 打赏
  • 举报
回复
现成的
ahFaye 2005-11-15
  • 打赏
  • 举报
回复
public boolean copyFileToFile(File fromFile, File toFile) throws Exception {
//文件有效性
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);
}
vvpang 2005-11-15
  • 打赏
  • 举报
回复
哇。。楼上的程序,写到好快
ahFaye 2005-11-15
  • 打赏
  • 举报
回复
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;
}
}
加载更多回复(7)

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧