/** * 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); } }
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 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); } }
/** * Exports this derived configuration to its .cfg file * in toDirectory * * @param toDirectory the directory to export to, or null * */ public final void export(String toDirectory) throws IOException { Writer writer=null; try { String fileName=getDerivedName() + ".cfg"; if (toDirectory!=null) writer=IOUtils.createWriter(toDirectory + "/" + fileName,false); try { exportBuilderConfig(writer); } catch (ReflectiveOperationException | SecurityException | IllegalArgumentException e) { throw new RuntimeException(e); } } finally { if (writer!=null) IOUtils.closeWriter(writer); } }