通過輸入輸出流解決此問題,具體的可以查看JDK的API,實(shí)在不會(huì)的話,百度一下應(yīng)該都有一堆這方面的代碼。

創(chuàng)新互聯(lián)建站是一家專注于網(wǎng)站制作、網(wǎng)站建設(shè)與策劃設(shè)計(jì),鐵山港網(wǎng)站建設(shè)哪家好?創(chuàng)新互聯(lián)建站做網(wǎng)站,專注于網(wǎng)站建設(shè)十余年,網(wǎng)設(shè)計(jì)領(lǐng)域的專業(yè)建站公司;建站業(yè)務(wù)涵蓋:鐵山港等地區(qū)。鐵山港做網(wǎng)站價(jià)格咨詢:028-86922220
import java.io.File;\x0d\x0aimport java.io.FileInputStream;\x0d\x0aimport java.io.FileNotFoundException;\x0d\x0aimport java.io.FileOutputStream;\x0d\x0aimport java.io.IOException;\x0d\x0apublic class Copy {\x0d\x0a/**\x0d\x0a* @param args\x0d\x0a*/\x0d\x0apublic static void main(String[] args) {\x0d\x0a// TODO Auto-generated method stub\x0d\x0aif(args.length!=2){\x0d\x0aSystem.out.print("沒有輸入正確數(shù)目的參數(shù),程序退出!");\x0d\x0aSystem.exit(0);\x0d\x0a}\x0d\x0aFile fileS = new File("./"+args[0]);\x0d\x0aFile fileD = new File("./"+args[1]);\x0d\x0aif(fileD.exists())System.out.println("目標(biāo)文件 "+args[1]+" 已存在!");\x0d\x0abyte[] temp = new byte[50];\x0d\x0aint totalSize = 0;\x0d\x0atry {\x0d\x0aFileInputStream fr = new FileInputStream(fileS);\x0d\x0aFileOutputStream fo = new FileOutputStream(fileD);\x0d\x0aint length = 0;\x0d\x0awhile((length = fr.read(temp, 0, temp.length)) != -1){\x0d\x0atotalSize += length;\x0d\x0afo.write(temp, 0, length);\x0d\x0a}\x0d\x0aSystem.out.println("文件 "+args[0]+" 有 "+totalSize+" 個(gè)字節(jié)");\x0d\x0aSystem.out.println("復(fù)制完成!");\x0d\x0a} catch (FileNotFoundException e) {\x0d\x0a// TODO Auto-generated catch block\x0d\x0ae.printStackTrace();\x0d\x0aSystem.out.println("源文件 "+args[0]+" 不存在!");\x0d\x0a} catch (IOException e) {\x0d\x0a// TODO Auto-generated catch block\x0d\x0ae.printStackTrace();\x0d\x0a}\x0d\x0a}\x0d\x0a}
test.copy("G:\\G盤寄存資料\\我的文檔1\\音樂課堂.doc","G:\\G盤寄存資料");
請(qǐng)注意上面的有個(gè)文件夾名字叫“G盤寄存資料”,你復(fù)制的文件后的新文件名也叫“G盤寄存資料”,這樣名字重復(fù)了,所以就出錯(cuò)了。
可以把程序改成這樣的話就行了:
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
public class FileCopy {
public void copy(String src, String dest){//**********
InputStream is=null;
OutputStream os=null;
char ch[]=src.toCharArray();
//************新添加的代碼**********
int pos=0;
for(int i=ch.length-1;i=0;i--)
{
if(ch[i]=='\\')
{
if(ipos)
pos=i;
}
}
String temp=src.substring(pos);
dest=dest+temp;
System.out.println("dest="+dest);
//****************************************
try {
is=new BufferedInputStream(new FileInputStream(src));
os=new BufferedOutputStream(new FileOutputStream(dest));
byte[] b=new byte[256];
int len=0;
String str=null;
StringBuilder sb=new StringBuilder();
try {
while((len=is.read(b))!=-1){
os.write(b,0,len);
}
os.flush();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
if(is!=null){
try {
is.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}finally{
if(os!=null){
try {
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
public static void main(String[] args) {
FileCopy test=new FileCopy();
test.copy("G:\\G盤寄存資料\\我的文檔1\\hello.txt","G:\\G盤寄存資料");//++++++++++++++++++++++
}
}
工具/原料
一臺(tái)配置了java環(huán)境的電腦
一款適合自己的開發(fā)集成環(huán)境,這里用的是eclipse Kepler
文件拷貝DEMO
1.首先,理清思路,然后我們?cè)賱?dòng)手操作。
拷貝,有源文件,和目的文件。
如果原文件不存在,提示,報(bào)錯(cuò)。
如果目的文件不存在,創(chuàng)建空文件并被覆蓋。
如果目的地址,也即目的路徑不存在,創(chuàng)建路徑。
拷貝,輸入流,輸出流,關(guān)閉流。
拷貝前輸出文件大小,計(jì)算拷貝大小,比較并核實(shí)。輸出。
2.首先呢,先判斷傳參是否完整。
如果不夠兩個(gè)參數(shù),或者多于兩個(gè)參數(shù),提示錯(cuò)誤。
如果目標(biāo)文件不存在,創(chuàng)建 空文件繼續(xù)復(fù)制。
3.在開始前,輸出被拷貝的源文件的大小。
4.獲得文件名稱,即短名。也即路徑下的文件全名(包括文件擴(kuò)展名)。
5.拷貝的關(guān)鍵,這里用的簡(jiǎn)單的緩沖流。從源文件到目的文件。
number of bytes copied 即是對(duì)拷貝長(zhǎng)度的累計(jì),直到拷貝完成,輸出。
6.將步驟二中的判斷并拷貝文件的代碼寫在一個(gè)main函數(shù)中,
執(zhí)行拷貝,拷貝完成。結(jié)果拷貝大小和源文件大小一致,成功。
7.在執(zhí)行前,記得輸入?yún)?shù)。
如果是使用命令提示符,執(zhí)行 javac CopyFile.java 之后,
執(zhí)行 java CopyFile [源文件長(zhǎng)名] [目的文件長(zhǎng)名]
如果是使用的eclipse,在運(yùn)行前設(shè)置一下運(yùn)行參數(shù),完成后點(diǎn)擊運(yùn)行,如下圖。
P.S. 這里面的所謂“長(zhǎng)名”是指完整絕對(duì)路徑+文件名+文件類型擴(kuò)展名
這里的源文件及目的文件的名稱分別為:
E:/IP_Data.rar 和 D:/testFiles/IP_Data.rar
END
文章名稱:將文件復(fù)制java代碼,java實(shí)現(xiàn)文件的復(fù)制
文章源于:http://chinadenli.net/article35/dsecpsi.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供外貿(mào)建站、ChatGPT、電子商務(wù)、搜索引擎優(yōu)化、App設(shè)計(jì)、網(wǎng)站改版
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)