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}