List of usage examples for org.apache.commons.io FileUtils writeLines
public static void writeLines(File file, Collection lines) throws IOException
toString()
value of each item in a collection to the specified File
line by line. From source file:com.sangupta.clitools.file.RightTrim.java
@Override protected boolean processFile(File file) throws IOException { // read entire file in memory List<String> contents = FileUtils.readLines(file); final long initial = file.length(); // now for each string - trim from end for (int index = 0; index < contents.size(); index++) { String line = contents.get(index); line = StringUtils.stripEnd(line, null); contents.set(index, line);//from w w w .ja v a2s . co m } // write back contents of file FileUtils.writeLines(file, contents); long current = file.length(); this.bytesSaved.addAndGet(initial - current); System.out.println("File " + file.getAbsoluteFile().getAbsolutePath() + " right-trimmed and saved " + (initial - current) + " bytes."); return true; }
From source file:com.sangupta.clitools.file.LeftTrim.java
@Override protected boolean processFile(File file) throws IOException { // read entire file in memory List<String> contents = FileUtils.readLines(file); final long initial = file.length(); // now for each string - trim from end for (int index = 0; index < contents.size(); index++) { String line = contents.get(index); line = StringUtils.stripStart(line, null); contents.set(index, line);//from w ww . j av a 2 s.co m } // write back contents of file FileUtils.writeLines(file, contents); long current = file.length(); this.bytesSaved.addAndGet(initial - current); System.out.println("File " + file.getAbsoluteFile().getAbsolutePath() + " left-trimmed and saved " + (initial - current) + " bytes."); return true; }
From source file:com.github.jrh3k5.plugin.maven.l10n.data.AbstractMessagesPropertiesParserTest.java
/** * Test the parsing of a properties file and detection of duplicate translation keys. * //from w ww. j a v a 2 s.c om * @throws Exception * If any errors occur during the test run. */ @Test public void testGetDuplicateTranslationKeys() throws Exception { final File testFile = getTestFile("messages.properties"); FileUtils.writeLines(testFile, Arrays.asList("dup=foo", "not.dup=bar", "dup=fizzbuzz")); assertThat(parser.getDuplicateTranslationKeys(testFile)).hasSize(1).contains("dup"); }
From source file:cz.cas.lib.proarc.common.process.GenericExternalProcessTest.java
@Test public void testIMConvert() throws Exception { // temp.setDeleteOnExit(false); String imageMagicExec = "/usr/bin/convert"; Assume.assumeTrue(new File(imageMagicExec).exists()); File confFile = temp.newFile("props.cfg"); File root = temp.getRoot();//from w w w . ja v a 2 s .co m URL pdfaResource = TiffImporterTest.class.getResource("pdfa_test.pdf"); File pdfa = new File(root, "pdfa_test.pdf"); FileUtils.copyURLToFile(pdfaResource, pdfa); FileUtils.writeLines(confFile, Arrays.asList("input.file.name=RESOLVED", "exec=" + imageMagicExec, // "arg=-verbose", "arg=-thumbnail", "arg=120x128", "arg=$${input.file}[0]", "arg=-flatten", "arg=$${output.file}", "id=test")); PropertiesConfiguration conf = new PropertiesConfiguration(confFile); GenericExternalProcess gep = new GenericExternalProcess(conf); gep.addInputFile(pdfa); File output = new File(root, "pdfa.jpg"); gep.addOutputFile(output); gep.run(); // System.out.printf("#exit: %s, out: %s\nresults: %s\n", // gep.getExitCode(), gep.getFullOutput(), gep.getResultParameters()); assertEquals("exit code", 0, gep.getExitCode()); assertTrue(output.toString(), output.exists()); assertTrue("Not JPEG", InputUtils.isJpeg(output)); }
From source file:de.unisb.cs.st.javalanche.mutation.analyze.DetectedByTestAnalyzer.java
@Override public String analyze(Iterable<Mutation> mutations, HtmlReport report) { List<String> lines = new ArrayList<String>(); for (Mutation mutation : mutations) { if (mutation.isKilled()) { MutationTestResult result = mutation.getMutationResult(); Set<TestMessage> detecting = new HashSet<TestMessage>(); Collection<TestMessage> failures = result.getFailures(); detecting.addAll(failures);//ww w . j av a2s. co m Collection<TestMessage> errors = result.getErrors(); detecting.addAll(errors); String tests = getIds(detecting); String line = mutation.getId() + "," + tests; lines.add(line); } } Set<Entry<String, Integer>> entrySet = testsIds.entrySet(); lines.add("Ids"); for (Entry<String, Integer> entry : entrySet) { lines.add(entry.getKey() + "," + entry.getValue()); } try { FileUtils.writeLines(new File("detectedByTest.csv"), lines); } catch (IOException e) { e.printStackTrace(); } return Joiner.on("\n").join(lines); }
From source file:es.uvigo.ei.sing.adops.operations.running.mrbayes.MrBayes3_2ProcessManager.java
@Override public void buildSummary(MrBayesOutput output) throws OperationException { try {// w ww .j a v a2s . c om FileUtils.moveFile(new File(output.getConFile().getAbsolutePath() + ".tre"), output.getConFile()); final List<String> lines = FileUtils.readLines(output.getConFile()); final ListIterator<String> itLines = lines.listIterator(); while (itLines.hasNext()) { final String line = itLines.next(); if (line.contains("tree con_50_majrule")) { final String[] lineSplit = line.split("="); final String tree = lineSplit[1].trim(); itLines.set(lineSplit[0] + "= " + Newick.parse(tree.trim())); } } FileUtils.writeLines(output.getConFile(), lines); super.buildSummary(output); } catch (Exception e) { throw new OperationException("Error while working with consensus tree", e); } }
From source file:net.fabricmc.loom.task.GenVSCodeProjectTask.java
@TaskAction public void genVsCodeProject() throws IOException { LoomGradleExtension extension = this.getProject().getExtensions().getByType(LoomGradleExtension.class); File classPathFile = new File("vscodeClasspath.txt"); File configFile = new File("javaconfig.json"); Gson gson = new Gson(); Version version = gson.fromJson(new FileReader(Constants.MINECRAFT_JSON.get(extension)), Version.class); List<String> libs = new ArrayList<>(); for (Version.Library library : version.libraries) { if (library.allowed() && library.getFile(extension) != null && library.getFile(extension).exists()) { libs.add(library.getFile(extension).getAbsolutePath()); }//www .j a va 2s . c o m } libs.add(Constants.MINECRAFT_FINAL_JAR.get(extension).getAbsolutePath()); for (File file : getProject().getConfigurations().getByName("compile").getFiles()) { libs.add(file.getAbsolutePath()); } FileUtils.writeLines(classPathFile, libs); JsonObject jsonObject = new JsonObject(); JsonArray jsonArray = new JsonArray(); jsonArray.add("src/main/java"); jsonArray.add("src/main/resorces"); jsonArray.add("src/test/java"); jsonArray.add("src/test/resorces"); jsonObject.add("sourcePath", jsonArray); JsonElement element = new JsonPrimitive(classPathFile.getName()); jsonObject.add("classPathFile", element); element = new JsonPrimitive("vscode"); jsonObject.add("outputDirectory", element); FileUtils.writeStringToFile(configFile, gson.toJson(jsonObject), "UTF-8"); }
From source file:com.groupcdg.maven.tidesdk.GenerateMojo.java
private void create(final File outputDirectory, final File resourcesDirectory) throws IOException, InterruptedException { FileUtils.copyDirectory(resourcesDirectory, new File(outputDirectory, "Resources")); FileUtils.writeLines(new File(outputDirectory, "manifest"), createManifest()); FileUtils.writeLines(new File(outputDirectory, "tiapp.xml"), createXml()); }
From source file:com.gargoylesoftware.js.CodeUpdater.java
private static void processFile(final File originalFile, final boolean isMain) throws IOException { String relativePath = originalFile.getPath().replace('\\', '/'); relativePath = "com/gargoylesoftware/js/" + relativePath.substring(relativePath.indexOf("/jdk/") + "/jdk/".length()); final String root = isMain ? "src/main/java/" : "src/test/java/"; final File localFile = new File(root + relativePath); if (!localFile.exists()) { System.out.println("File doesn't locally exist: " + relativePath); return;/*from w w w. j av a 2 s .com*/ } final List<String> originalLines = FileUtils.readLines(originalFile); final List<String> localLines = FileUtils.readLines(localFile); while (!isCodeStart(originalLines.get(0))) { originalLines.remove(0); } for (int i = 0; i < localLines.size(); i++) { if (isCodeStart(localLines.get(i))) { while (i < localLines.size()) { localLines.remove(i); } break; } } for (int i = 0; i < originalLines.size(); i++) { String line = originalLines.get(i); line = line.replace("jdk.internal.org.objectweb.asm", "org.objectweb.asm"); line = line.replace("jdk.nashorn.internal", "com.gargoylesoftware.js.nashorn.internal"); line = line.replace("jdk/nashorn/internal", "com/gargoylesoftware/js/nashorn/internal"); line = line.replace("jdk/nashorn/javaadapters", "com/gargoylesoftware/js/nashorn/javaadapters"); line = line.replace("jdk.nashorn.api", "com.gargoylesoftware.js.nashorn.api"); line = line.replace("jdk.nashorn.tools", "com.gargoylesoftware.js.nashorn.tools"); line = line.replace("jdk.internal.dynalink", "com.gargoylesoftware.js.internal.dynalink"); line = line.replace(" @Constructor", " @com.gargoylesoftware.js.nashorn.internal.objects.annotations.Constructor"); line = line.replace(" @Property", " @com.gargoylesoftware.js.nashorn.internal.objects.annotations.Property"); originalLines.set(i, line); if (line.equals("@jdk.Exported")) { originalLines.remove(i--); } } localLines.addAll(originalLines); FileUtils.writeLines(localFile, localLines); }
From source file:com.mythesis.profileanalysis.WordVectorFinder.java
/** * a method that calculates the word vector for a specific domain * @param domain the general domain/*from w ww . j a v a 2 s . c o m*/ * @param wordVectorDirectory the directory where i will save the word vector * @param LDAdirectory LDA directory * @param nTopTopics number of top topics * @param choice how to select the top topics (1 for average, 2 for median, 3 for number of documents that have probability higher than 1/nTopics) * @param top_words number of top words * @param nTopics total number of topics */ public void getWordVector(String domain, String wordVectorDirectory, String LDAdirectory, int nTopTopics, int choice, int top_words, int nTopics) { String path = LDAdirectory; LDAtopicsWords rk = new LDAtopicsWords(); if (nTopTopics > nTopics) nTopTopics = nTopics; //get a number of top topics and a number of top words from every topic HashMap<Integer, HashMap<String, Double>> topicwordprobmap = rk.readFile(path, top_words, nTopics, nTopTopics, choice); List<String> wordVector = new ArrayList<>(); for (Integer topicindex : topicwordprobmap.keySet()) { //iterate through every topic Set keySet = topicwordprobmap.get(topicindex).keySet(); Iterator iterator = keySet.iterator(); while (iterator.hasNext()) { //iterate through every word String word = iterator.next().toString(); if (!wordVector.contains(word)) { wordVector.add(word); //put the word in word vector } } } //store the word vector File wordVectorFile = new File(wordVectorDirectory + "wordVector" + domain + ".txt"); try { FileUtils.writeLines(wordVectorFile, wordVector); } catch (IOException ex) { Logger.getLogger(WordVectorFinder.class.getName()).log(Level.SEVERE, null, ex); } }