檔案的拷貝以及封裝成工具類

2021-09-12 12:56:28 字數 2122 閱讀 9528

拷貝:以程式作為中轉,把輸入流和輸出流合二為一

* 工具類:1.封裝操作和釋放資源步驟,簡化**

檔案的拷貝

/*

檔案的拷貝

1.檔案到程式

2.程式到檔案

*/public static void filecopy(string srcpath,string detapath)

os.flush();

} catch (filenotfoundexception e) catch (ioexception e) finally

} catch (ioexception e)

try

} catch (ioexception e)

}}

檔案和資料夾都可以拷貝

public static void dircopy(file file, file tofile)  

//遍歷資料夾

for (file f : file.listfiles())

} else

try

} catch (filenotfoundexception e) catch (ioexception e) finally

} catch (ioexception e)

try

} catch (ioexception e) }}

}

工具類:jdk1.7對資源釋放做了處理:try...with...resource:(因為我的是1.8的,沒做測試)

public static void filecopytopack1(inputstream is ,outputstream os)

// os.flush();

// } catch (filenotfoundexception e) catch (ioexception e)

// }

自己封裝:先對釋放資源步驟進行封裝

public static void close(inputstream is ,outputstream os)

} catch (ioexception e)

try

} catch (ioexception e)

}

釋放資源步驟再簡化

/*

釋放資源再簡化,引數是可變引數,類似於陣列,可以乙個也可以多個,增加了靈活性。

因為inputstream和outputstream都實現於closeable介面

*/public static void close(closeable... ios)

} catch (ioexception e)

}}

inputstream和outputstream都實現於closeable介面
public abstract class inputstream implements closeable 

public abstract class outputstream implements closeable, flushable

封裝操作步驟和釋放步驟

//封裝操作和釋放步驟,只要傳入輸入輸出流就行了。

public static void filecopytopack(inputstream is ,outputstream os)

os.flush();

} catch (filenotfoundexception e) catch (ioexception e) finally

}

呼叫:

//檔案到檔案

try catch (ioexception e)

//檔案到位元組陣列

byte datas =null;

try catch (ioexception e)

//位元組陣列到檔案

try catch (ioexception e)

PHP封裝成類(檔案上傳)

uploadfile.class.php created by phpstorm.user huang date 2017 7 19 time 23 20 header content type text html charset utf 8 new new upload class upload ...

音效工具類的封裝

與單例類的比較 類方法的特點 import inte ce sppsoundplaymanager nsobject param name 聲音檔名 帶字尾 param alert 是否振動 void playsoundwithname nsstring name alert bool alert ...

spUtil工具類的封裝

工具類的作用是為了讓我們更方便的使用,一次封裝,多次使用,也是乙個合格的工程師必備的 技術,下面就是sp存值和取值的工具類封裝public class sputil else if value instanceof boolean else if value instanceof float els...