copyjava
⑴ java如何深度一个object
方法一:把对象序列化之后再反序列化,之后得到的对象就是深度克隆的对象;
方法二:自己重写方法,不过有点麻烦。
⑵ java 文件夹 。下面所有的文件及其文件夹 。
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
public class FolderCopy {
/**
* @param args
*/
/*文件
* param src,des
* return ture 成功。false 失败
*/
public static boolean fileCopy(String src,String des){
File srcFile=new File(src);
File desFile=new File(des);
byte[]b=new byte[1024];
String string="";
try {
FileInputStream fis=new FileInputStream(srcFile);
FileOutputStream fos=new FileOutputStream(desFile,false);
while(true){
int i=fis.read(b);
if(i==-1)break;
fos.write(b,0,i);
}
fos.close();
fis.close();
return true;
} catch (Exception e){
e.printStackTrace();
}
return false;
}
/*
* 文件夹
* param src,des
* return ture 成功。false 失败
*
*/
public static boolean folderCopy(String src,String des){
File srcFile=new File(src);
File desFile=new File(des);
File []files=srcFile.listFiles();
boolean flag = false;
if(!desFile.exists())desFile.mkdir();
for(int i=0;i<files.length;i++){
String path=files[i].getAbsolutePath();
if(files[i].isDirectory()){
File newFile=new File("path.replace(src,des)");
if(!newFile.exists())newFile.mkdir();//不存在新建文件夹
folderCopy(path,path.replace(src,des));
}
else
flag=fileCopy(path,path.replace(src,des));//文件复制函数
}
return flag;
}
public static void main(String[] args) {
FolderCopy.folderCopy("d:\\1","C:\\1" );
}
}
希望能够帮助你。
⑶ java如何拷贝文件到另一个目录下
下面列举出4种方式:
1、使用FileStreams复制
这是最经典的方式将一个文件的内容复制到另一慎搜个文件中。 使用FileInputStream读取文件A的字节,使山顷用FileOutputStream写入到文件B。正如你所看到的我们执行几个读和写操作try的数据,所以这应该是一个低效率的,下一个方法我们将看到新的方式。 这是第一个方法的代码:
⑷ 利用JAVA语言编写一个 名为的程序 实现文件的拷贝功能,应该怎样做
import java.io.File;x0dx0aimport java.io.FileInputStream;x0dx0aimport java.io.FileNotFoundException;x0dx0aimport java.io.FileOutputStream;x0dx0aimport java.io.IOException;x0dx0apublic class Copy {x0dx0a/**x0dx0a* @param argsx0dx0a*/x0dx0apublic static void main(String[] args) {x0dx0a// TODO Auto-generated method stubx0dx0aif(args.length!=2){x0dx0aSystem.out.print("没有输孙并入正确数目的参数,程序退出!");x0dx0aSystem.exit(0);x0dx0a}x0dx0aFile fileS = new File("./"+args[0]);x0dx0aFile fileD = new File("./蔽此"+args[1]);x0dx0aif(fileD.exists())System.out.println("目标文件 "+args[1]+" 已存在!");x0dx0abyte[] temp = new byte[50];x0dx0aint totalSize = 0;x0dx0atry {x0dx0aFileInputStream fr = new FileInputStream(fileS);x0dx0aFileOutputStream fo = new FileOutputStream(fileD);x0dx0aint length = 0;x0dx0awhile((length = fr.read(temp, 0, temp.length)) != -1){x0dx0atotalSize += length;x0dx0afo.write(temp, 0, length);x0dx0a}x0dx0aSystem.out.println("文件 "+args[0]+" 有 "+totalSize+" 个字节");x0dx0aSystem.out.println("复制完成!");x0dx0a} catch (FileNotFoundException e) {x0dx0a// TODO Auto-generated catch blockx0dx0ae.printStackTrace();x0dx0aSystem.out.println("源宏凯迅文件 "+args[0]+" 不存在!");x0dx0a} catch (IOException e) {x0dx0a// TODO Auto-generated catch blockx0dx0ae.printStackTrace();x0dx0a}x0dx0a}x0dx0a}