通过历遍当前文件夹下所有文件和文件夹信息,然后(处理文件夹自己去处理):
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);
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);
解决方案 »
- 无法解析类型 org.springframework.dao.DataAccessException。从必需的 .class 文件间接引用了它
- 主线程等待子线程的执行结果问题,解决立刻给分。
- 如何使<script></script>中的参数与<%%>中的参数实现交互操作!?在线等!!!!!!!!!!
- JSP對話框問題
- 两个Number类型的变量相加,如何写这个语句(在线等)
- SUN OpenSSO的下载资源问题
- 第一次装weblogic8.1,什么都不懂,请大家多多指教
- 初级问题!!!
- 求异常问题在哪里
- java jdbc使用事物处理死锁
- 如何架设本地服务器?
- 请推荐一个免费的支持J2EE服务器,谢谢!
System.out.print( f.renameTo(new File("d:\\tmp33")));如果是在同一硬盘的话这样的移动是合法的 true
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);
这个不行吗/
/**
* 文件查找,复制文件
* 作者:吴成旺
* 创建时间: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");
}
}