Java如何实现文件拷贝操作和如何正确关闭资源
使用字節(jié)流完成文件的拷貝:
使用字節(jié)輸入流(FileInputStream)將源文件中的數(shù)據(jù)讀進(jìn)來,同時使用字節(jié)輸出流(FileOutputStream)將讀進(jìn)來的數(shù)據(jù)寫到目標(biāo)文件中,即一邊讀一邊寫,完成文件的拷貝。
//使用字節(jié)流完成文件的拷貝操作
public class FileStremCopyDemo {
?? ?public static void main(String[] args) throws IOException {
?? ??? ?//創(chuàng)建目標(biāo)與源對象
?? ??? ?File srcFile = new File("file/src.txt");//源對象
?? ??? ?File desFile = new File("file/des.txt");//目標(biāo)文件
?? ??? ?
?? ??? ?//創(chuàng)建輸入輸出流
?? ??? ?InputStream in = new FileInputStream(srcFile);
?? ??? ?OutputStream out = new FileOutputStream(desFile);
?? ??? ?
?? ??? ?//IO 操作
?? ??? ?byte[] buffer = new byte[1024];//創(chuàng)建容量為1024的緩沖區(qū)(存儲已經(jīng)讀取的字節(jié)數(shù)據(jù))
?? ??? ?int len = -1;//表示已經(jīng)讀取了多少個字節(jié)數(shù)據(jù),若果等于-1,表示已經(jīng)讀到最后
?? ??? ?while((len = in.read(buffer)) != -1){
?? ??? ??? ?//數(shù)據(jù)在buffer數(shù)組中
?? ??? ??? ?out.write(buffer, 0, len);
?? ??? ?}
?? ??? ?//關(guān)閉資源
?? ??? ?in.close();
?? ??? ?out.close();
?? ?}
?
}
上面程序?qū)崿F(xiàn)文件的拷貝中,是直接將異常拋出去,一般這種情況是要處理異常的,按正常的try catch處理異常,我們會發(fā)現(xiàn)關(guān)閉資源的代碼會很繁瑣,如下:
?? ?//繁瑣的資源關(guān)閉方式
?? ?private static void test1() {
?? ??? ?File srcFile = new File("file/src.txt");
?? ??? ?File desFile = new File("file/des.txt");
?? ??? ?
?? ??? ?InputStream in = null;
?? ??? ?OutputStream ?out = null;
?? ??? ?try {
?? ??? ??? ?in = new FileInputStream(srcFile);
?? ??? ??? ?out = new FileOutputStream(desFile);
?
?? ??? ??? ?byte[] buffer = new byte[1024];
?? ??? ??? ?int len = -1;
?
?? ??? ??? ?while ((len = in.read(buffer)) != -1) {
?? ??? ??? ??? ?out.write(buffer);
?? ??? ??? ?}
?
?? ??? ?} catch (FileNotFoundException e) {
?? ??? ??? ?e.printStackTrace();
?? ??? ?} catch (IOException e) {
?? ??? ??? ?e.printStackTrace();
?? ??? ?} finally {
?? ??? ??? ?try {
?? ??? ??? ??? ?if (in != null) {
?? ??? ??? ??? ??? ?in.close();
?? ??? ??? ??? ?}
?? ??? ??? ?} catch (IOException e) {
?? ??? ??? ??? ?e.printStackTrace();
?? ??? ??? ?}
?? ??? ??? ?
?? ??? ??? ?if (out != null) {
?? ??? ??? ??? ?try {
?? ??? ??? ??? ??? ?out.close();
?? ??? ??? ??? ?} catch (IOException e) {
?? ??? ??? ??? ??? ?e.printStackTrace();
?? ??? ??? ??? ?}
?? ??? ??? ?}
?? ??? ?}
?? ?}
從Java7開始,Java新添了一個特性,在try后面加上一個圓括號,將需要關(guān)閉的資源放到里面定義,程序執(zhí)行完畢會自動幫我們關(guān)閉圓括號里面的資源,如下:
?? ?//Java7自動關(guān)閉資源
?? ?private static void test2() {
?? ??? ?File srcFile = new File("file/src.txt");
?? ??? ?File desFile = new File("file/des.txt");
?
?? ??? ?try (
?? ??? ??? ??? ?InputStream in = new FileInputStream(srcFile);
?? ??? ??? ??? ?OutputStream out = new FileOutputStream(desFile);
?? ??? ??? ??? ?) {
?
?? ??? ??? ?byte[] buffer = new byte[1024];
?? ??? ??? ?int len = -1;
?
?? ??? ??? ?while ((len = in.read(buffer)) != -1) {
?? ??? ??? ??? ?out.write(buffer);
?? ??? ??? ?}
?
?? ??? ?}catch (IOException e) {
?? ??? ??? ?e.printStackTrace();
?? ??? ?}
?
?? ?}
————————————————
版權(quán)聲明:本文為CSDN博主「caidie_huang」的原創(chuàng)文章,遵循CC 4.0 BY-SA版權(quán)協(xié)議,轉(zhuǎn)載請附上原文出處鏈接及本聲明。
原文鏈接:https://blog.csdn.net/caidie_huang/article/details/52738225
總結(jié)
以上是生活随笔為你收集整理的Java如何实现文件拷贝操作和如何正确关闭资源的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 医疗保险交多少年才能终身享受
- 下一篇: Java中资源关闭的处理方式