急救啊!怎么将文件复制到另一个目录下面?

christina_sll 2005-11-25 11:45:32
想把D盘目录下的test.xls文件复制到C盘的Temp文件夹里,
我的代码是这么写的:

oldfile= new File("D:test.xls");
newfile= new File("C:/Temp/");
oldfile.renameTo(newfile);

可是怎么也没放到那个目录下面,
是怎么回事啊?拜托大家了
...全文
373 20 打赏 收藏 转发到动态 举报
写回复
用AI写文章
20 条回复
切换为时间正序
请发表友善的回复…
发表回复
christina_sll 2005-11-29
  • 打赏
  • 举报
回复
谢谢大家
jin2005 2005-11-28
  • 打赏
  • 举报
回复
mark
christina_sll 2005-11-25
  • 打赏
  • 举报
回复
哇,太感动了,谢谢大家,^_^ 试试各位的方案先
shine333 2005-11-25
  • 打赏
  • 举报
回复
to regandy(红狼):

复制文件,而不是读取文件的时候,千万不要用BufferedWriter.readLine()
而是InputStream

to yuzl32(Hello!) :
renameTo? 请仔细阅读renameTo方法的javadoc!!!
beyondbyb 2005-11-25
  • 打赏
  • 举报
回复
先读文件,保存到你需要的目录下面,然后把这个文件删除,
yyouyou 2005-11-25
  • 打赏
  • 举报
回复
呵呵,看了yuzl32(Hello!) 的回复才知道自己理解错了renameTo函数,呵呵,多谢多谢!
yyouyou 2005-11-25
  • 打赏
  • 举报
回复
public boolean renameTo(File dest)

将这个 File 对象指定的文件更名为给定参数 File 指定的路径名。

这个不是复制函数啊。

程序应该是建立两个流,一个输入一个输出,先读入xls文件,这个要用到jxl类包,你去网上搜索一下。然后写出。

还可以通过直接调用windows的copy命令进行复制。以下为转载:

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");
yuzl32 2005-11-25
  • 打赏
  • 举报
回复
import java.io.*;

public class Test {
public static void main(String[] args)
{
File oldfile= new File("D:/test.xls");
File newfile= new File("C:/Temp/test.xls");

oldfile.renameTo(newfile);
}
}

// renameTo() 需要全路径名
regandy 2005-11-25
  • 打赏
  • 举报
回复
通过流,先把文件信息读近来,然后再写到你要复制的地方.
FileReader input =new FileReader("oldFile.txt");
BufferedReader bufInput=new BufferedReader(input);
FileWriter output=new FileWriter("newFile.txt");
BufferedWriter bufOutput=new BufferedWriter(output);

line=buffIntput.readLine();
while(line!=null){
bufOutput.write(line,0,line.length())
bufOutput.newLine();
line=bufInput.readLine();
}


大概就是这个意思,主要也就是调用这几个方法,具体写法你看下文档就明白了.
tramp73 2005-11-25
  • 打赏
  • 举报
回复
路过学习
christina_sll 2005-11-25
  • 打赏
  • 举报
回复
呵呵,是我把那个根目录没弄清,现在ok了,thank you very much ^_^
christina_sll 2005-11-25
  • 打赏
  • 举报
回复
"temp/test.xls" 这也试过了,还是不行啊
shine333 2005-11-25
  • 打赏
  • 举报
回复
这样变成了绝对路径

"temp/test.xls"才是当前目录(不是sourceFile的目录)下的temp/test.xls
nofeelingzjy 2005-11-25
  • 打赏
  • 举报
回复
mark
christina_sll 2005-11-25
  • 打赏
  • 举报
回复
To shine333(enihs), 关于复制的问题问题终于解决了,真的很感谢你,可是那个相对路径是怎么写都不行啊?
当destinationFile为:"/temp/test.xls"(此目录一定存在)

系统总是报这样的错误信息:\temp\test.xls <the system cannot find the path specifed>

这又是怎么回事啊?能否再指点一下,劳驾了
shine333 2005-11-25
  • 打赏
  • 举报
回复
InputStream in = null;
OutputStream out = null;
try {
in = new BufferedInputStream(new FileInputStream(sourceFile)); //
out = new BufferedOutputStream(new FileOutputStream(destinationFile));
byte[] buff = new byte[4096]; //这里是缓冲的大小,可以根据实际情况指定
int len;
while ( (len = in.read(buff, 0, buff.length)) != -1) {
out.write(buff, 0, len);
}
} finally {
if (in != null) {
try {
in.close();
} catch (IOException ex) {
}
}
if (out != null) {
try {
out.close();
} catch (IOException ex) {
}
}
}
这段代码没有做catch处理,如果需要自己在finally之前添加
nofeelingzjy 2005-11-25
  • 打赏
  • 举报
回复
学习中,,,,
yuzl32 2005-11-25
  • 打赏
  • 举报
回复
当然如果是简单的移动文件只需用renameTo,复制的话,就另说了.
这也怪我把题目看错了, 呵呵.
christina_sll 2005-11-25
  • 打赏
  • 举报
回复


我试了一下regandy(红狼) 的代码,那个好像只对txt文件有效啊,而我要读取的是excel文件,是以单元存储的,该怎么解决啊?还有怎么设置相对的路径呢?就是说如果我的程序在Temp目录下,那么我想把这文件放到这个Temp的子目录Document文件里?除了写绝对路径(如D:/program/Temp/Document/test.xls)外,能否就直接写成:/Document/test.xls?
shine333 2005-11-25
  • 打赏
  • 举报
回复
简单补充一下原因
BufferedReader/Writer读写行的时候,返回/设置的String不包含那个换行符

Windows默认\r\n, Unix:\n Mac:\r

只要符合任何一种组合的,都被BufferedReader视作换行,而写的时候,永远使用当前OS的换行符。
同时,永远会在最后加上一个空行,而实际文件可能根本没有

renameTo,故名思义,是改名,如果指定了不同的目录,各个OS的表现形式是不一致的
/**
* Renames the file denoted by this abstract pathname.
*
* <p> Many aspects of the behavior of this method are inherently
* platform-dependent: The rename operation might not be able to move a
* file from one filesystem to another, it might not be atomic, and it
* might not succeed if a file with the destination abstract pathname
* already exists. The return value should always be checked to make sure
* that the rename operation was successful.
即使会移动吧,那个也是move而不是copy!

81,092

社区成员

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

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