使用DataOutputStream復(fù)制圖片
在io包中,提供了數(shù)據(jù)操作流:數(shù)據(jù)輸出流(DataOutputStream)和數(shù)據(jù)輸入流(DataInputStream)。通常數(shù)據(jù)輸出流會(huì)按照一定的格式將數(shù)據(jù)輸出,再通過(guò)數(shù)據(jù)輸入流按照一定的格式將數(shù)
在io包中,提供了數(shù)據(jù)操作流:數(shù)據(jù)輸出流(DataOutputStream)和數(shù)據(jù)輸入流(DataInputStream)。通常數(shù)據(jù)輸出流會(huì)按照一定的格式將數(shù)據(jù)輸出,再通過(guò)數(shù)據(jù)輸入流按照一定的格式將數(shù)據(jù)讀入。
創(chuàng)建Java項(xiàng)目
首先,在你使用的IDE中(比如MyEclipse或Eclipse),新建一個(gè)Java項(xiàng)目。右鍵點(diǎn)擊項(xiàng)目,選擇"new" -> "Java Project",輸入項(xiàng)目名稱并點(diǎn)擊"finish"。然后,右鍵項(xiàng)目,選擇"new" -> "package",輸入包名并點(diǎn)擊"finish"。接著,右鍵點(diǎn)擊包名,選擇"new" -> "class",輸入類名并點(diǎn)擊"finish"。
編寫代碼
在新建的class文件中,輸入以下代碼:
```java
package ;
import ;
import ;
import ;
import ;
import ;
import ;
public class CopyPictureDemo {
// 需求:使用DataOutputStream復(fù)制圖片
public static void main(String[] args) throws IOException {
// 數(shù)據(jù)源:本機(jī)照片中的任意一個(gè)圖片
File srcFile new File("D:圖片Camera Roll壁紙_");
// 目的地:百度經(jīng)驗(yàn)文件夾的位置
File destFile new File("D:圖片百度經(jīng)驗(yàn)");
// 調(diào)用方法
method(srcFile, destFile);
}
// 創(chuàng)建一個(gè)方法進(jìn)行圖片的復(fù)制操作
public static void method(File srcFile, File destFile) throws IOException {
// 封裝數(shù)據(jù)源
DataInputStream inputStream new DataInputStream(new FileInputStream(srcFile));
// 封裝目的地
DataOutputStream outputStream new DataOutputStream(new FileOutputStream(destFile));
// 創(chuàng)建一個(gè)字節(jié)數(shù)組
byte[] bys new byte[1024];
int len 0;
while ((len (bys)) ! -1) {
outputStream.write(bys, 0, len);
}
// 釋放資源
();
();
}
}
```
運(yùn)行程序
右鍵點(diǎn)擊項(xiàng)目,選擇"run as",然后選擇"Java Application"執(zhí)行即可。程序會(huì)按照指定的路徑將圖片復(fù)制到指定的目錄中。
注意:以上代碼是使用DataOutputStream進(jìn)行圖片復(fù)制的基本示例,你可以根據(jù)實(shí)際需求進(jìn)行修改和擴(kuò)展。