久久久精品一区ed2k-女人被男人叉到高潮的视频-中文字幕乱码一区久久麻豆樱花-俄罗斯熟妇真实视频

java復(fù)制文件代碼實(shí)例,java實(shí)現(xiàn)復(fù)制文件

Java 將一個(gè)文件復(fù)制到另一處

test.copy("G:\\G盤寄存資料\\我的文檔1\\音樂(lè)課堂.doc","G:\\G盤寄存資料");

在日喀則等地區(qū),都構(gòu)建了全面的區(qū)域性戰(zhàn)略布局,加強(qiáng)發(fā)展的系統(tǒng)性、市場(chǎng)前瞻性、產(chǎn)品創(chuàng)新能力,以專注、極致的服務(wù)理念,為客戶提供成都做網(wǎng)站、成都網(wǎng)站建設(shè) 網(wǎng)站設(shè)計(jì)制作按需求定制開(kāi)發(fā),公司網(wǎng)站建設(shè),企業(yè)網(wǎng)站建設(shè),成都品牌網(wǎng)站建設(shè),網(wǎng)絡(luò)營(yíng)銷推廣,外貿(mào)營(yíng)銷網(wǎng)站建設(shè),日喀則網(wǎng)站建設(shè)費(fèi)用合理。

請(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盤寄存資料");//++++++++++++++++++++++

}

}

java實(shí)現(xiàn):將一個(gè)記事本文件內(nèi)容復(fù)制到另一個(gè)文件

主要是用到j(luò)ava里面的i/o流。代碼例子如下:

import?java.io.BufferedReader;

import?java.io.File;

import?java.io.FileInputStream;

import?java.io.FileNotFoundException;

import?java.io.FileOutputStream;

import?java.io.FileWriter;

import?java.io.IOException;

import?java.io.InputStream;

import?java.io.InputStreamReader;

/**

*?java讀寫文件,復(fù)制文件

*?讀取d:/1.txt文件內(nèi)容,寫入f:/text.txt文件中.

*?@author?young

*

*/

public?class?FileWriterTest?{

//?讀寫文件

public?static?void?rwFile(){

FileWriter?fw?=?null;

BufferedReader?br?=?null;

try?{

fw?=?new?FileWriter("f:\\text.txt",?true);

br?=?new?BufferedReader(new?InputStreamReader(

new?FileInputStream("d:\\1.txt"),?"UTF-8"));

String?line?=?null;

while?((line?=?br.readLine())?!=?null)?{

System.out.println("文件內(nèi)容:?"?+?line);

fw.write(line);

fw.flush();

}

br.close();

}?catch?(FileNotFoundException?e)?{

e.printStackTrace();

}?catch?(IOException?e)?{

e.printStackTrace();

}?finally?{

if?(fw?!=?null)?{

try?{

fw.close();

}?catch?(IOException?e)?{

//?TODO?Auto-generated?catch?block

e.printStackTrace();

}

}

}

}

public?static?void?main(String[]?args)?{

rwFile();

}

}

首先在D盤新建文件1.txt,輸入任意內(nèi)容。然后執(zhí)行java代碼即可。

Java怎么實(shí)現(xiàn)文件拷貝

工具/原料

一臺(tái)配置了java環(huán)境的電腦

一款適合自己的開(kāi)發(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.在開(kāi)始前,輸出被拷貝的源文件的大小。

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

java文件復(fù)制粘貼

復(fù)制粘貼實(shí)際上是文件的流讀取和寫入可以通過(guò)如下方法實(shí)現(xiàn):

讀寫是兩個(gè)不同的分支,通常都是分開(kāi)單獨(dú)使用的。

可以通過(guò)BufferedReader 流的形式進(jìn)行流緩存,之后通過(guò)readLine方法獲取到緩存的內(nèi)容。

BufferedReader bre = null;

try {

String file = "D:/test/test.txt";

bre = new BufferedReader(new FileReader(file));//此時(shí)獲取到的bre就是整個(gè)文件的緩存流

while ((str = bre.readLine())!= null) // 判斷最后一行不存在,為空結(jié)束循環(huán)

{

System.out.println(str);//原樣輸出讀到的內(nèi)容

};

備注: 流用完之后必須close掉,如上面的就應(yīng)該是:bre.close(),否則bre流會(huì)一直存在,直到程序運(yùn)行結(jié)束。

可以通過(guò)“FileOutputStream”創(chuàng)建文件實(shí)例,之后過(guò)“OutputStreamWriter”流的形式進(jìn)行存儲(chǔ),舉例:

OutputStreamWriter pw = null;//定義一個(gè)流

pw = new OutputStreamWriter(new FileOutputStream(“D:/test.txt”),"GBK");//確認(rèn)流的輸出文件和編碼格式,此過(guò)程創(chuàng)建了“test.txt”實(shí)例

pw.write("我是要寫入到記事本文件的內(nèi)容");//將要寫入文件的內(nèi)容,可以多次write

pw.close();//關(guān)閉流

備注:文件流用完之后必須及時(shí)通過(guò)close方法關(guān)閉,否則會(huì)一直處于打開(kāi)狀態(tài),直至程序停止,增加系統(tǒng)負(fù)擔(dān)。

利用JAVA語(yǔ)言編寫一個(gè) 名為copy的程序 實(shí)現(xiàn)文件的拷貝功能,應(yīng)該怎樣做?

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("沒(méi)有輸入正確數(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}

當(dāng)前標(biāo)題:java復(fù)制文件代碼實(shí)例,java實(shí)現(xiàn)復(fù)制文件
網(wǎng)站路徑:http://sd-ha.com/article18/dseshgp.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供做網(wǎng)站、網(wǎng)頁(yè)設(shè)計(jì)公司、網(wǎng)站建設(shè)、自適應(yīng)網(wǎng)站網(wǎng)站排名、網(wǎng)站設(shè)計(jì)公司

廣告

聲明:本網(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)

成都定制網(wǎng)站建設(shè)