Java-常用工具-收集-1
阿新 • • 發佈:2018-12-07
//判斷字串是否為空
public static boolean checkStringEmpty(String string) {
return string == null || string.isEmpty();
}
//日期格式轉字串
public static String dateToString(Date date, String pattern) { String dateString = null; if (date != null) { SimpleDateFormat dateFormat = new SimpleDateFormat(pattern); dateString = dateFormat.format(date); } return dateString; }
// 檔案複製
public static void copyFile(File copyFile, File newFile) { FileInputStream is = null; FileOutputStream os = null; try { is = new FileInputStream(copyFile); os = new FileOutputStream(newFile); byte[] bs = new byte[1024]; int len; //每一位元組、每一位元組的讀 while ((len = is.read(bs)) > 0) { os.write(bs, 0, len);//寫入 } } catch (Exception e) { e.printStackTrace(); } finally { try { if (os != null) { os.close(); } if (is != null) { is.close(); } } catch (Exception e2) { e2.printStackTrace(); } } }
///校驗檔案路徑,沒有則建立資料夾
public static void checkFiledirectory(String uri) { File file = new File(uri); if (!file.exists()) { file.mkdir(); } }