1. Write a local file
/** * * @param sDestFile filename * @param sContent content * 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 (); } } }
2. Find all files with file names ending with a certain string
public static void findFile(File file) { //1. First determine whether the incoming path exists if (file.exists()) { // 2. If it is a file, determine whether it ends with java if (file.isFile()) { String filePath = file.getPath(); if (file.getPath().endsWith("createtable.sh")) { //limited to end with "createtable.sh" appendToFile("G:\\work\\compareHiveMetaToTreasury\\createtableFileName",filePath); // System.out.println(filePath); } } // 3 If the folder is given, it needs to be called recursively if (file.isDirectory()) { File[] otherFile = file.listFiles(); for (File f : otherFile) { findFile(f);// Called recursively } } } else { System.out.println("The folder you gave does not exist"); } }