Java工具類 Java檔案工具類

2021-09-17 22:33:54 字數 1844 閱讀 1658

public class fileutils 

/*** 讀取檔案並作為byte返回

** @param file 目標檔案

* @return

* @throws ioexception

*/public static byte readfileasbytes(file file) throws ioexception

/*** 讀取檔案並作為base64編碼string返回

** @param file 目標檔案

* @return

* @throws ioexception

*/public static string readfileasstringbybase64(file file) throws ioexception

/*** 向目標檔案寫資料

** @param file 目標檔案

* @param bytes 待寫資料

* @throws ioexception

*/public static void writefile(file file, byte bytes) throws ioexception

/*** 向目標檔案寫資料

** @param file 目標檔案

* @param bytes 待寫資料

* @throws ioexception

*/outputstream.write(bytes);

outputstream.close();

}/**

* 獲取檔案字尾,不包含「.」

** @param file 目標檔案

* @return

*/public static string getfilesuffix(file file)

/*** 將檔案或資料夾複製到目標資料夾內

** @param src 源位址,支援檔案或者資料夾

* @param des 目標位址,支援資料夾

* @throws ioexception

*/public static void copyfile(file src, file des) throws ioexception

inputstream inputstream = new bufferedinputstream(new fileinputstream(src));

outputstream outputstream = new bufferedoutputstream(new fileoutputstream(path));

readandwrite(inputstream, outputstream);

} else if (src.isdirectory()) }}

}/**

* 刪除檔案及資料夾內所有內容

** @param path 待刪除檔案或資料夾

*/public static void deletefiles(file path) else if (path.isdirectory())

}path.delete();}}

/*** 讀取並寫入

** @param inputstream 讀取

* @param outputstream 寫入

* @throws ioexception

*/public static void readandwrite(inputstream inputstream, outputstream outputstream) throws ioexception

inputstream.close();

outputstream.close();

}}

java複製檔案工具類

public int copy string source name,string dest name,int type throws ioexception if source filefile.canread if dest filefile.exists else else else if p...

Java反射工具類

反射工具類 public class reflectionutil return field.get object catch exception e return null 通過反射獲取靜態成員的值 param classvar param fieldname return 靜態成員的值 supp...

Java工具類 排序

最近想著封裝一些常用的工具類。先拿最常用最簡單的陣列排序來練練手。日常操作中常見的排序方法有 氣泡排序 快速排序 選擇排序 插入排序 希爾排序 堆排序 基數排序 歸併排序等。數字排序演算法通常用來作為演算法入門課程的基本內容,在實際應用 尤其是普通商業軟體 中使用的頻率較低,但是通過排序演算法的實現...