社区
Web 开发
帖子详情
java中如何把一个目录下的文件移到另一个指定的目录?
Sunboyjava
2005-01-12 01:11:32
如题
...全文
4256
14
打赏
收藏
java中如何把一个目录下的文件移到另一个指定的目录?
如题
复制链接
扫一扫
分享
转发到动态
举报
写回复
配置赞助广告
用AI写文章
14 条
回复
切换为时间正序
请发表友善的回复…
发表回复
打赏红包
wuchengwang
2005-01-15
打赏
举报
回复
如果有不理解,请问我!
wuchengwang
2005-01-15
打赏
举报
回复
//package com.etong.system;
/**
* 文件查找,复制文件
* 作者:吴成旺
* 创建时间:2005-1-10
* 最后修改时间:
**/
import java.io.*;
import javax.naming.*;
import com.roger.database.DBPersistence;
import com.etong.hr.*;
import com.etong.system.*;
import com.etong.workflow.*;
import com.etong.statetaxbureau.workflow.*;
import javax.sql.DataSource;
import javax.naming.InitialContext;
import javax.naming.*;
import java.util.*;
public class FileBeing
{
private String message;
private String path;
private boolean filejudge(String path,String filename)//判断文件是否存在操作
{
File f=new File(path,filename);
if(f.exists())
{
return true;
}
return false;
}
private boolean folderjudge(String path) //判断文件夹是否存在操作
{
File d=new File(path);
if (d.exists())
{
return true;
}
return false;
}
public boolean parentjudge(String path)
{
File d=new File(path);
String parent = d.getParent();
File f=new File(parent);
if (!f.exists())
{
return true;
}
return false;
}
public void creatfolder(String path)
{
File file = new File(path);
file.mkdir();
}
//其中path为数据库找到的路径,insid为实例ID,frd可以为空,为扩展路径
public void Searchfolder(String path) throws Exception{
String[] parefile=path.split("/");
if (parefile.length>0)
{
String diskRoot = parefile[0];
System.out.print(parefile.length);
for (int j=1;j<parefile.length;j++)
{
diskRoot = diskRoot + "/" + parefile[j].toString();
creatfolder(diskRoot);
System.out.println(diskRoot);
}
}
}
/**
* 该方法用于创建文件
*/
public void Searchfile(String path,String filename,String frd) throws Exception{
/*解决路径问题*/
Properties pro = new Properties();
pro.load(new FileInputStream(new File("C:/pro.properties")));
String url = pro.getProperty("skyurl");
path=url+"/"+path;
/*解决路径问题*/
if (!filejudge(path,filename))
{
setMessagebox("您想打开的文件或文件未创建,系统将为你添加相关的文件或文件夹.");
//如果目录不存在创建相关目录
if (!folderjudge(path))
{
Searchfolder(path);
}
//在相关目录创建相关文件
File f=new File(path,filename);
f.createNewFile();
//setMessagebox("'"+path+"'下的'"+filename+"'文件已创建成功!");
setMessagebox("您所需要打开的文件即将打开.");
}else{
setMessagebox("您所需要打开的文件即将打开.");
}
setFilepath(path+"\\"+filename);
}
public void setFilepath(String path){
this.path=path;
}
public String getFilepath(){
return path;
}
/**
* 该方法用于删除文件
*/
public void Deletefile(String path,String filename) throws IOException{
File f=new File(path,filename);
f.delete();
}
/**
*该方法用于删除文件夹
*/
public void Deletefolder(String path) {
File d=new File(path);
d.delete();
}
/**
*此方法用于复制文件夹下的文件
**/
public void copyfile(String from_path,String to_path,String from_name, String to_name) throws IOException
{
//首先定义两个文件,它们都是File的实例
Properties pro = new Properties();
pro.load(new FileInputStream(new File("C:/pro.properties")));
String url = pro.getProperty("skyurl");
from_path=url+"/"+from_path;
to_path=url+"/"+to_path;
File from_file = new File(from_path,from_name);
File to_file = new File(to_path,to_name);
//首先确定源文件存在,并且是可读的文件
if (!from_file.exists())
abort("没有这样的源文件: " + from_name);
if (!from_file.isFile())
abort("不能复制目录: " + from_name);
if (!from_file.canRead())
abort("源文件不是可读文件: " + from_name);
//如果目标是一个目录,则将源文件名作为目标文件名
if (to_file.isDirectory())
to_file = new File(to_file, from_file.getName());
//如果目标文件存在,首先要确定它是可写的文件,并且在写入前询问用户。
//如果目标文件不存在,那么要确定该目录存在并且是可写的。
if (to_file.exists()) {
System.out.flush();
}
else {
String parent = to_file.getParent(); //目标目录
if (parent == null) //如果是空值,使用当前目录
parent = System.getProperty("user.dir");
File dir = new File(parent); //将它转换成文件。
if (!dir.exists())
try{
Searchfolder(to_path);//如果没有指定目录,就创建一个新目录
}catch(Exception e)
{
System.out.print(e);
}
if (dir.isFile())
abort("指定的目标不是目录 " + parent);
if (!dir.canWrite())
abort("指定的目标不是目录 " + parent);
// 如果程序运行到这里,证明一切正常,这时开始复制文件。
FileInputStream from = null; //读取源文件的数据流
FileOutputStream to = null; //写入目标文件的数据流
try {
from = new FileInputStream(from_file); //创建输入流
to = new FileOutputStream(to_file); //创建输出流
byte[] buffer = new byte[4096];
int bytes_read; //缓存中的字节数目
//将一段字节流读到缓存中,然后将它们写出来,循环直到文件末尾(当read()返回-1时)停止
while((bytes_read = from.read(buffer)) != -1) //读取流直到文件末尾
to.write(buffer, 0, bytes_read); //写入
}
//关闭流
finally {
if (from != null) try { from.close(); } catch (IOException e) { ; }
if (to != null) try { to.close(); } catch (IOException e) { ; }
}
}
}
private static void abort(String msg) throws IOException {
throw new IOException("文件复制: " + msg);
}
public String getMessagebox() {
return message;
}
public void setMessagebox(String message) {
this.message = message;
}
public static void main(String[] args) throws Exception{
FileBeing g=new FileBeing();
g.copyfile("tes2121t/123/123/att/sd/mod/sd","test12223/123","123.txt","245.txt");
}
}
Jeffbean
2005-01-15
打赏
举报
回复
up
Sunboyjava
2005-01-15
打赏
举报
回复
哥哥们够意思..... 解决了..
结帐........
xwt799023
2005-01-14
打赏
举报
回复
通过历遍当前文件夹下所有文件和文件夹信息,然后(处理文件夹自己去处理):
File fold = new File("c:\\test.properties");
String strNewPath = "c:\\testok\\";
File fnewpath = new File(strNewPath);
if(!fnewpath.exists())
fnewpath.mkdirs();
File fnew = new File(strNewPath+fold.getName());
fold.renameTo(fnew);
这个不行吗/
Sunboyjava
2005-01-14
打赏
举报
回复
我只想一次移动一个文件?
以梦为马
2005-01-14
打赏
举报
回复
友情up。
GJA106
2005-01-14
打赏
举报
回复
没有直接的方法进行移动操作!!只能自己历遍。
zealVampire
2005-01-13
打赏
举报
回复
下面所有的文件都会移动 但是同意分区 说错了。
zealVampire
2005-01-13
打赏
举报
回复
File f = new File("D:\\Software\\新建文件夹");
System.out.print( f.renameTo(new File("d:\\tmp33")));
如果是在同一硬盘的话这样的移动是合法的 true
samkuang
2005-01-13
打赏
举报
回复
学习
Sunboyjava
2005-01-13
打赏
举报
回复
没有直接的方法进行移动操作吗?
dabo1980
2005-01-12
打赏
举报
回复
读文件,写文件。楼上都给出代码了!
GJA106
2005-01-12
打赏
举报
回复
通过历遍当前文件夹下所有文件和文件夹信息,然后(处理文件夹自己去处理):
File fold = new File("c:\\test.properties");
String strNewPath = "c:\\testok\\";
File fnewpath = new File(strNewPath);
if(!fnewpath.exists())
fnewpath.mkdirs();
File fnew = new File(strNewPath+fold.getName());
fold.renameTo(fnew);
linux复制
指定
目录
下的全部
文件
到另
一个
目录
中
阅读全文请点击linux复制
指定
目录
下的全部
文件
到另
一个
目录
中
复制
指定
目录
下的全部
文件
到另
一个
目录
中
文件
及
目录
的复制是经常要用到的。linux下进行复制的命令为cp。假设复制源
目录
为 dir1 ,目标
目录
为dir2。怎样才能将dir1下所有
文件
复制到dir2下了如果dir2
目录
不存在,则可以直接使用cp -r dir1 dir2即可。如果dir2
目录
已存在,则需要使用cp -r dir1/. d...
java
移动
目录
_使用
Java
将
文件
从
一个
目录
移动到另
一个
目录
- Break易站
Java
提供了在
目录
之间移动
文件
的功能。这里描述了实现此目的的两种方法。第一种方法利用Files包进行移动,而另一种方法首先将
文件
复制到目标,然后从源
中
删除原始副本。使用Files.Path move()方法:将
文件
永久重命名并移动到新位置。句法:public static Path move(Path source, Path target, CopyOption..options)throws...
java
中
如何把
一个
目录
下的
文件
移到
另
一个
指定
目录
中
import
java
.io.*;public class FileToFile{ public static void main(String[] args) { File fold = new File("e://
java
//file.
java
");//某路径下的
文件
String strNewPath = "e://
java
//new file//";//新路径 File fn
java
文件
移动到
目录
,使用
Java
将
文件
从
一个
目录
移动到另
一个
目录
我们可以使用Files.move()API将
文件
从
一个
目录
移动到另
一个
目录
。以下是move方法的语法。publicstaticPathmove(Pathsource,Pathtarget,CopyOption...options)throwsIOException哪里source-要移动的
文件
的源路径目标-
文件
的目标路径移动options-选项,例如REPLACE_EXISTING...
java
实现
文件
的移动到
指定
文件
夹
java
实现
文件
移动到
指定
文件
夹的方法
Web 开发
81,091
社区成员
341,718
社区内容
发帖
与我相关
我的任务
Web 开发
Java Web 开发
复制链接
扫一扫
分享
社区描述
Java Web 开发
社区管理员
加入社区
获取链接或二维码
近7日
近30日
至今
加载中
查看更多榜单
社区公告
暂无公告
试试用AI创作助手写篇文章吧
+ 用AI写文章