private void deleteTempDir(File dir) { IOUtils.recursiveDeleteDir(dir); }
/** * Copies the n first lines of a file to another file. * If the out file exists it will be overwritten * * @throws IOException if copying fails */ public static void copy(String inFile, String outFile, int lineCount) throws IOException { BufferedReader reader = null; BufferedWriter writer = null; try { reader = createReader(inFile); writer = createWriter(outFile, false); int c; int newLines = 0; while (-1 != (c=reader.read()) && newLines<lineCount) { writer.write(c); if (c=='\n') newLines++; } } finally { closeReader(reader); closeWriter(writer); } }
/** Read an input stream completely into a string */ public static String readAll(InputStream stream, Charset charset) throws IOException { return readAll(new InputStreamReader(stream, charset)); }
/** * Writes the given string to the file */ public static void writeFile(File file, String text, boolean append) throws IOException { BufferedWriter out = null; try { out = createWriter(file, append); out.write(text); } finally { closeWriter(out); } }
/** * Returns the number of lines in a file. * If the file does not exists, 0 is returned */ public static int countLines(String file) { BufferedReader reader = null; int lineCount = 0; try { reader = createReader(file,"utf8"); while (reader.readLine() != null) lineCount++; return lineCount; } catch (IOException e) { return lineCount; } finally { closeReader(reader); } }
@Override public ApplicationPackage preprocess(Zone zone, DeployLogger logger) throws IOException, TransformerException, ParserConfigurationException, SAXException { IOUtils.recursiveDeleteDir(preprocessedDir); IOUtils.copyDirectory(appDir, preprocessedDir, -1, (dir, name) -> ! name.equals(preprocessed) && ! name.equals(SERVICES) && ! name.equals(HOSTS) && ! name.equals(CONFIG_DEFINITIONS_DIR)); preprocessXML(new File(preprocessedDir, SERVICES), getServicesFile(), zone); if (getHostsFile().exists()) { preprocessXML(new File(preprocessedDir, HOSTS), getHostsFile(), zone); } FilesApplicationPackage preprocessed = FilesApplicationPackage.fromFile(preprocessedDir, includeSourceFiles); preprocessed.copyUserDefsIntoApplication(); return preprocessed; }
@Override public ApplicationFile appendFile(String value) { try { IOUtils.writeFile(file, value, true); return this; } catch (IOException e) { throw new UncheckedIOException(e); } }
@Override public Reader getRankingExpression(String name) { File expressionFile = new File(searchDefinitionDir, name); try { return IOUtils.createReader(expressionFile, "utf-8"); } catch (IOException e) { throw new IllegalArgumentException("Could not read ranking expression file '" + expressionFile.getAbsolutePath() + "'", e); } }
closeInputStream(in); closeOutputStream(out); copyDirectory(new File(sourceLocation, children[i]), new File(targetLocation, children[i]), maxRecurseLevel-1);
/** * Copies all files and subdirectories (infinitely recursively) in a directory to another. * Any existing files are overwritten. * * @param sourceLocation the source directory * @param targetLocation the target directory * @throws IOException if copying any file fails. This will typically result in some files being copied and * others not, i.e this method is not exception safe */ public static void copyDirectory(File sourceLocation , File targetLocation) throws IOException { copyDirectory(sourceLocation, targetLocation, -1); }
/** Convenience method for closing a list of readers. Does nothing if the given reader list is null. */ public static void closeAll(List<Reader> readers) { if (readers==null) return; for (Reader reader : readers) closeReader(reader); }
/** * Returns a list containing the lines in the given file as strings * * @return a list of Strings for the lines of the file, in order * @throws IOException if the file could not be read */ public static List<String> getLines(String fileName) throws IOException { BufferedReader reader = null; try { List<String> lines = new java.util.ArrayList<>(); reader = createReader(fileName,"utf8"); String line; while (null != (line = reader.readLine())) lines.add(line); return lines; } finally { closeReader(reader); } }
/** * Writes the given string to the file */ public static void writeFile(String file, String text, boolean append) throws IOException { writeFile(new File(file), text, append); }
@Override public Reader createReader() throws FileNotFoundException { try { if ( ! exists()) throw new FileNotFoundException("File '" + file + "' does not exist"); return IOUtils.createReader(file, "UTF-8"); } catch (IOException e) { throw new UncheckedIOException(e); } }
private static void exportCfg(ConfigInstance instance, String fileName) throws IOException { Writer writer = null; try { writer = IOUtils.createWriter(fileName, false); if (writer != null) { writer.write(instance.toString()); writer.write("\n"); } } finally { if (writer != null) { IOUtils.closeWriter(writer); } } }
/** * Copies all files and subdirectories in a directory to another. * Any existing files are overwritten. * * @param sourceLocation the source directory * @param targetLocation the target directory * @param maxRecurseLevel if this is 1, only files immediately in sourceLocation are copied, * if it is 2, then files contained in immediate subdirectories are copied, etc. * If it is 0, sourceLocation will only be copied if it is a file, not a directory. * If it is negative, recursion is infinite. * @throws IOException if copying any file fails. This will typically result in some files being copied and * others not, i.e this method is not exception safe */ public static void copyDirectory(File sourceLocation , File targetLocation, int maxRecurseLevel) throws IOException { copyDirectory(sourceLocation, targetLocation, maxRecurseLevel, new FilenameFilter() { @Override public boolean accept(File dir, String name) { return true; } }); }
/** * Recursive deletion of directories */ public static boolean recursiveDeleteDir(File dir) { if (dir.isDirectory()) { String[] children = dir.list(); for (String child : children) { boolean success = recursiveDeleteDir(new File(dir, child)); if (!success) return false; } } // The directory is now empty so delete it return dir.delete(); }
private String readConfigDefinition(File defPath) { try (Reader reader = retrieveConfigDefReader(defPath)) { return IOUtils.readAll(reader); } catch (IOException e) { throw new RuntimeException("Error reading config definition '" + defPath + "'", e); } }