一、寫本地文件java
/** * * @param sDestFile 文件名 * @param sContent 內容 * autho whh */ public static void appendToFile(String sDestFile, String sContent) { // String sContent = "I love Ysm"; // String sDestFile = "F:/work/logParse/autoCreateHql/myWrite.txt"; File destFile = new File(sDestFile); BufferedWriter out = null; if (!destFile.exists()) { try { destFile.createNewFile(); } catch (IOException e) { e.printStackTrace(); } } try { out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(sDestFile, true))); out.write(sContent); out.newLine(); } catch (Exception e) { e.printStackTrace(); } finally { try { if (out != null) { out.close(); } } catch (IOException e) { e.printStackTrace(); } } }
二、找出全部以某個字符串結尾的文件名的文件app
public static void findFile(File file) { //1.首先判斷傳入的路徑是否存在 if (file.exists()) { // 2.若是是文件,判斷是否是以java結尾 if (file.isFile()) { String filePath = file.getPath(); if (file.getPath().endsWith("createtable.sh")) { //此處限定以"createtable.sh"結尾 appendToFile("G:\\work\\compareHiveMetaToTreasury\\createtableFileName",filePath); // System.out.println(filePath); } } // 3若是給的是文件夾 須要遞歸調用 if (file.isDirectory()) { File[] otherFile = file.listFiles(); for (File f : otherFile) { findFile(f);// 經過遞歸調用 } } } else { System.out.println("您給定的文件夾不存在"); } }