java File

dingbs 2003-08-23 04:10:35
怎样把文件考到另一个目录,而不删除原来的文件呀~~谢谢大哥们的回答~~
...全文
38 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
dingbs 2003-08-25
  • 打赏
  • 举报
回复
谢谢各位的帮助,了解。还有,就是有时候怎么文件是乱马呀,(xls)文件。接分吧~~呵呵
huhaowen1978 2003-08-24
  • 打赏
  • 举报
回复
clone
onefox 2003-08-24
  • 打赏
  • 举报
回复
这里有一个全套的 Copy 类, 里面有但文件 何 目录的 copy方法

======================== Copys.java ==========================================
/**
try this class,any question ,please mail me:ybem@sina.com

* 一些常用的方法
* 创建日期:(2002-1-18 13:24:35)
* @author:yb
*/

import java.io.*;


public class Copys {
/**
* Replace 构造子注解。
*/
public Copys() {}

/**
* 文件拷贝(单个)
* 创建日期:(2002-1-24 9:52:47)
* @param from java.lang.String
* @param to java.lang.String
*/
public boolean copy(String from, String to)
{
try
{
to = replace(to,"\\","/");
String toPath = to.substring(0,to.lastIndexOf("/")); //提取文件路径
File f = new File(toPath); //建立文件目录路
if(!f.exists())
f.mkdirs();

BufferedInputStream bin = new BufferedInputStream(new FileInputStream(from));
BufferedOutputStream bout = new BufferedOutputStream(new FileOutputStream(to));
int c;
while((c=bin.read()) != -1) //复制
bout.write(c);
bin.close();
bout.close();
return true;
}
catch(Exception e)
{
e.printStackTrace();
return false;
}
}


/**
* 判断文件是否存在
* 创建日期:(2002-1-24 9:19:48)
* @return boolean
* @param fileName java.lang.String
*/
public boolean exists(String fileName)
{
try
{
FileReader f = new FileReader(new File(fileName));
return true;
}
catch(FileNotFoundException e)
{
return false;
}
}


/**
* 此处插入方法描述。
* 创建日期:(2002-1-18 13:26:53)
* @param args java.lang.String[]
*/
public static void main(String[] args)
{
Copys u = new Copys();
u.copys("C:\\Downloads","D:\\新建文件夹");
}


/**
* 替换函数,性能比较强
* 创建日期:(2002-1-18 13:25:21)
* @return java.lang.String
* @param ss java.lang.String
*/
public String replace(String srcStr,String oldStr,String newStr)
{
int i = srcStr.indexOf(oldStr);
StringBuffer sb = new StringBuffer();
if (i == -1)
return srcStr;
sb.append(srcStr.substring(0,i) + newStr);
if (i+oldStr.length() < srcStr.length()) //自身方法递归,替换剩余子串
sb.append(replace(srcStr.substring(i+oldStr.length(),srcStr.length()),oldStr,newStr));
return sb.toString();
}


/**
* 目录拷贝
* 创建日期:(2002-1-24 10:10:46)
* @return boolean
* @param from java.lang.String
* @param to java.lang.String
*/
public boolean copys(String from, String to)
{
from = replace(from,"\\","/");
to = replace(to,"\\","/");

if(!from.endsWith("/"))
from = from +"/";
if(!to.endsWith("/"))
to = to +"/";

File tt = new File(to);
if(!tt.exists())
tt.mkdirs();

String ss = "";
File ff = new File(from);
if(ff.isDirectory())
{
File f[] = ff.listFiles();
for(int i=0;i<f.length;i++)
{
String temp = f[i].getName();
if(f[i].isDirectory()) //如果是目录则新建目录
{
File g = new File(to+temp);
if(!g.exists())
g.mkdirs();
}
else
copy(from+temp,to+temp);
copys(from+temp,to+temp); //自身方法递归复制子目录
}
}

return true;
}
}
NewStarter 2003-08-23
  • 打赏
  • 举报
回复
同上
JavaBoyCaoJi 2003-08-23
  • 打赏
  • 举报
回复
public boolean CopyFile()//文件的复制
{
try{
File f1=new File(copyfirst);
FileInputStream fis1=new FileInputStream(f1);
DataInputStream dis1=new DataInputStream(fis1);
File f2=new File(copyend);
FileOutputStream fos2=new FileOutputStream(f2);
DataOutputStream dos2=new DataOutputStream(fos2);
for(long j=0;j<f1.length();j++){
dos2.write(dis1.readByte());
dos2.flush();
}
dos2.close();
fos2.close();
dis1.close();
fis1.close();
return true;
}catch(Exception e){
System.out.println(e.toString());
return false;
}
}
info21 2003-08-23
  • 打赏
  • 举报
回复
……………………………………
File to_file = new File(directory,from_file.getName());//directory是目标目录
//from_file是原文件
FileInputStream from = NULL;//创建输入流
FileOutputStream to = NULL;//创建输出流

try{
from = new FileInputStream(from_file);
to = new FileInputStream(to_File);
byte[] buffer = new byte[4096];
int bytes_read;

while((bytes_read = from.read(buffer)) != -1)//读到文件末尾
to.write(buffer, 0, bytes_read);
……………………………………………………

62,614

社区成员

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

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