List of usage examples for org.apache.commons.io IOUtils writeLines
public static void writeLines(Collection lines, String lineEnding, Writer writer) throws IOException
toString()
value of each item in a collection to a Writer
line by line, using the specified line ending. From source file:com.alexholmes.hadooputils.test.TextIOJobBuilder.java
/** * Gathers all the inputs buffered by calls to {@link #addInput(String)} or * {@link #addInput(String...)} and writes them to the input directory, in * preparation for running the MapReduce job. * * @return a reference to this object/* w ww . ja v a 2s. c o m*/ * @throws IOException if something goes wrong */ public TextIOJobBuilder writeInputs() throws IOException { if (fs.exists(outputPath)) { fs.delete(outputPath, true); } if (fs.exists(inputPath)) { fs.delete(inputPath, true); } fs.mkdirs(inputPath); DataOutputStream stream = fs.create(new Path(inputPath, "part-0")); IOUtils.writeLines(inputs, String.format("%n"), stream); stream.close(); return this; }
From source file:hu.bme.mit.sette.tools.catg.CatgGenerator.java
private void copyTool(EclipseProject eclipseProject) throws IOException, SetteConfigurationException { FileUtils.copyDirectory(getTool().getToolDirectory(), getRunnerProjectSettings().getBaseDirectory()); // edit build.xml // TODO make better File buildXml = new File(getRunnerProjectSettings().getBaseDirectory(), "build.xml"); List<String> newLines = new ArrayList<>(); InputStream fis = null;/*from w w w.ja v a 2s . co m*/ try { fis = new FileInputStream(buildXml); List<String> lines = IOUtils.readLines(fis); for (String line : lines) { if (line.contains("[SETTE]")) { String indent = ""; for (int i = 0; i < line.length(); i++) { char ch = line.charAt(i); if (ch == ' ' || ch == '\t') { indent += ch; } else { break; } } line = line.trim(); if (line.equals("<!-- [SETTE][Libraries] -->")) { for (EclipseClasspathEntry entry : eclipseProject.getClasspathDescriptor() .classpathEntries()) { if (entry.getKind().equals(Kind.LIBRARY)) { newLines.add( String.format("%s<pathelement location=\"%s\"/>", indent, entry.getPath())); } } } else if (line.equals("<!-- [SETTE][Sources] -->")) { for (EclipseClasspathEntry entry : eclipseProject.getClasspathDescriptor() .classpathEntries()) { if (entry.getKind().equals(Kind.SOURCE)) { newLines.add(String.format("%s<src path=\"%s\"/>", indent, entry.getPath())); } } } else { throw new SetteConfigurationException( "Invalid SETTE command (XML comment) in CATG build.xml: " + line); } } else { newLines.add(line); } } } finally { IOUtils.closeQuietly(fis); } FileOutputStream fos = null; try { fos = new FileOutputStream(buildXml); IOUtils.writeLines(newLines, null, fos); } finally { IOUtils.closeQuietly(fos); } }
From source file:gov.nih.nci.nbia.servlet.DownloadServletV2.java
private void downloadManifestFile(String fileName, HttpServletResponse response, String userId, String password) {/*from ww w .j ava2 s .c om*/ logger.info("looking for manifest file name ..." + fileName); response.setContentType("text/plain"); response.setHeader("Content-Disposition", "attachment;filename=downloadname.txt"); try { List<String> readLines = IOUtils.readLines(new FileReader(fileName)); OutputStream os = response.getOutputStream(); IOUtils.writeLines(readLines, System.getProperty("line.separator"), os); os.close(); } catch (IOException e) { e.printStackTrace(); } }
From source file:com.redhat.red.offliner.PomArtifactListReaderTest.java
/** * Checks if type of a dependency is mapped correctly, if its mapping to extension-classifier is defined by an * external properties file. First it runs with an empty mapping file to ensure the mapping is not applied. Then the * test simply stores the default properties contents into a temporary file and runs checks if mapping was applied. *//* www. ja va 2 s . com*/ @Test public void readPathsMapTypeWithExternalMapping() throws Exception { // create empty mapping properties file File mappingFile = getFile("test.properties"); try (OutputStream os = new FileOutputStream(mappingFile)) { // nothing to do, just create an empty file } // create reader with the empty mapping PomArtifactListReader artifactListReader = new PomArtifactListReader(null, mappingFile.getPath(), new BasicCredentialsProvider()); ArtifactList artList = artifactListReader.readPaths(getFile("repo.pom")); List<String> paths = artList.getPaths(); checkPath(paths, "org/apache/maven/plugins/maven-assembly-plugin/2.5.5/maven-assembly-plugin-2.5.5.pom"); checkPath(paths, "org/apache/maven/plugins/maven-assembly-plugin/2.5.5/maven-assembly-plugin-2.5.5.jar", false); checkPath(paths, "org/apache/maven/plugins/maven-assembly-plugin/2.5.5/maven-assembly-plugin-2.5.5.maven-plugin"); checkPath(paths, "org/apache/ant/ant/1.8.0/ant-1.8.0-tests.jar", false); checkPath(paths, "org/apache/ant/ant/1.8.0/ant-1.8.0.test-jar"); // read the contents of the internal properties file List<String> contents; try (InputStream is = getClass().getClassLoader() .getResourceAsStream(PomArtifactListReader.DEFAULT_TYPE_MAPPING_RES)) { contents = IOUtils.readLines(is); } // write the mapping into the external file try (OutputStream os = new FileOutputStream(mappingFile)) { IOUtils.writeLines(contents, null, os); } // create reader with the copied mapping artifactListReader = new PomArtifactListReader(null, mappingFile.getPath(), new BasicCredentialsProvider()); artList = artifactListReader.readPaths(getFile("repo.pom")); paths = artList.getPaths(); checkPath(paths, "org/apache/maven/plugins/maven-assembly-plugin/2.5.5/maven-assembly-plugin-2.5.5.pom"); checkPath(paths, "org/apache/maven/plugins/maven-assembly-plugin/2.5.5/maven-assembly-plugin-2.5.5.jar"); checkPath(paths, "org/apache/ant/ant/1.8.0/ant-1.8.0-tests.jar"); }
From source file:gov.nih.nci.nbia.servlet.DownloadServletV3.java
private void downloadManifestFile(List<String> seriesList, HttpServletResponse response, String userId) { logger.info("constructing manifest file from the serieList"); response.setContentType("text/plain"); response.setHeader("Content-Disposition", "attachment;filename=downloadname.txt"); try {/*w w w . j av a 2s . c om*/ List<String> readLines = null; readLines = getManifestRec(seriesList, userId); if (readLines == null) { if (userId.equals(NCIAConfig.getGuestUsername()) || (userId == null)) { response.sendError(CLIENT_LOGIN_NEEDED, "Login needed."); return; } else if ((userId != null) && !(userId.equals(NCIAConfig.getGuestUsername()))) { response.sendError(CLIENT_NOT_AUTHORIZED, "Insufficient privilege."); return; } } else { OutputStream os = response.getOutputStream(); IOUtils.writeLines(readLines, System.getProperty("line.separator"), os); os.close(); } } catch ( IOException e) { e.printStackTrace(); } }
From source file:com.doculibre.constellio.services.AutocompleteServicesImpl.java
private synchronized void writeBlacklistFile(List<String> blacklist) { if (blacklist != null) { try {//from w w w . j a v a 2 s . c o m ByteArrayOutputStream output = new ByteArrayOutputStream(); IOUtils.writeLines(blacklist, null, output); SolrServicesImpl.writePlainConfigInCloud("autocomplete-blacklist.txt", output.toByteArray()); } catch (IOException e) { throw new RuntimeException(e); } } }
From source file:it.greenvulcano.util.txt.TextUtils.java
/** * Writes a text String into a file/*ww w. j a va 2 s. c o m*/ * * @param contentStrings * The list of strings to be written into the file * @param filename * The name of the file * @param endline * The line terminator, if null or empty is used '\n' * @param append * If true the data are appended to existent file * @throws IOException */ public static void writeFile(List<String> contentStrings, String filename, String endline, boolean append) throws IOException { FileOutputStream out = null; try { filename = adjustPath(filename); if ((endline == null) || endline.equals("")) { endline = "\n"; } out = new FileOutputStream(filename, append); IOUtils.writeLines(contentStrings, endline, out); out.flush(); } finally { if (out != null) { out.close(); } } }
From source file:com.streamsets.pipeline.stage.origin.spooldir.TestSpoolDirSource.java
@Test public void testRecoverableDataParserError() throws Exception { File f = new File("target", UUID.randomUUID().toString()); f.mkdir();//from w ww . j a v a 2 s . c o m SpoolDirConfigBean conf = new SpoolDirConfigBean(); conf.dataFormat = DataFormat.DELIMITED; conf.spoolDir = f.getAbsolutePath(); conf.batchSize = 10; conf.overrunLimit = 100; conf.poolingTimeoutSecs = 1; conf.filePattern = "file-[0-9].log"; conf.pathMatcherMode = PathMatcherMode.GLOB; conf.maxSpoolFiles = 10; conf.initialFileToProcess = "file-0.log"; conf.dataFormatConfig.compression = Compression.NONE; conf.dataFormatConfig.filePatternInArchive = "*"; conf.dataFormatConfig.csvHeader = CsvHeader.WITH_HEADER; conf.errorArchiveDir = null; conf.postProcessing = PostProcessingOptions.NONE; conf.retentionTimeMins = 10; conf.allowLateDirectory = false; conf.dataFormatConfig.textMaxLineLen = 10; conf.dataFormatConfig.onParseError = OnParseError.ERROR; conf.dataFormatConfig.maxStackTraceLines = 0; FileOutputStream outputStream = new FileOutputStream(new File(conf.spoolDir, "file-0.log")); IOUtils.writeLines(ImmutableList.of("A,B", "a,b,c", "a,b"), "\n", outputStream); outputStream.close(); SpoolDirSource source = new SpoolDirSource(conf); PushSourceRunner runner = new PushSourceRunner.Builder(SpoolDirDSource.class, source) .setOnRecordError(OnRecordError.TO_ERROR).addOutputLane("lane").build(); final List<Record> records = Collections.synchronizedList(new ArrayList<>(10)); runner.runInit(); try { runner.runProduce(new HashMap<>(), 10, output -> { synchronized (records) { records.addAll(output.getRecords().get("lane")); } runner.setStop(); }); runner.waitOnProduce(); // Verify proper record Assert.assertNotNull(records); Assert.assertEquals(1, records.size()); // And error record List<Record> errorRecords = runner.getErrorRecords(); Assert.assertEquals(1, errorRecords.size()); } finally { runner.runDestroy(); } }
From source file:com.streamsets.pipeline.stage.origin.spooldir.TestSpoolDirSource.java
@Test public void testMultipleFilesSameTimeStamp() throws Exception { File f = new File("target", UUID.randomUUID().toString()); f.mkdir();/*from www .jav a 2 s .co m*/ SpoolDirConfigBean conf = new SpoolDirConfigBean(); conf.dataFormat = DataFormat.DELIMITED; conf.useLastModified = FileOrdering.TIMESTAMP; conf.spoolDir = f.getAbsolutePath(); conf.batchSize = 10; conf.overrunLimit = 100; conf.poolingTimeoutSecs = 1; conf.filePattern = "*"; conf.pathMatcherMode = PathMatcherMode.GLOB; conf.maxSpoolFiles = 10; conf.dataFormatConfig.compression = Compression.NONE; conf.dataFormatConfig.filePatternInArchive = "*"; conf.dataFormatConfig.csvHeader = CsvHeader.WITH_HEADER; conf.errorArchiveDir = null; conf.postProcessing = PostProcessingOptions.NONE; conf.retentionTimeMins = 10; conf.allowLateDirectory = false; conf.dataFormatConfig.textMaxLineLen = 10; conf.dataFormatConfig.onParseError = OnParseError.ERROR; conf.dataFormatConfig.maxStackTraceLines = 0; long timestamp = System.currentTimeMillis() - 100000; for (int i = 0; i < 8; i++) { File current = new File(conf.spoolDir, Utils.format("file-{}.log", i)); try (FileOutputStream outputStream = new FileOutputStream(current)) { IOUtils.writeLines(ImmutableList.of("A,B", Utils.format("a-{},b-{}", i, i), "a,b"), "\n", outputStream); } Assert.assertTrue(current.setLastModified(timestamp)); } // for ctime delays, there's no way to set ctime (change timestamp) explicitly by rule Thread.sleep(5000L); File current = new File(conf.spoolDir, "a.log"); try (FileOutputStream outputStream = new FileOutputStream(current)) { IOUtils.writeLines(ImmutableList.of("A,B", "Gollum,Sauron", "Aragorn,Boromir"), "\n", outputStream); } Assert.assertTrue(current.setLastModified(System.currentTimeMillis())); SpoolDirSource source = new SpoolDirSource(conf); PushSourceRunner runner = new PushSourceRunner.Builder(SpoolDirDSource.class, source) .setOnRecordError(OnRecordError.TO_ERROR).addOutputLane("lane").build(); AtomicInteger batchCount = new AtomicInteger(0); runner.runInit(); Assert.assertEquals(0, runner.getErrors().size()); try { runner.runProduce(new HashMap<>(), 10, output -> { int i = batchCount.getAndIncrement(); if (i < 8) { List<Record> records = output.getRecords().get("lane"); Assert.assertNotNull(records); Assert.assertTrue(!records.isEmpty()); Assert.assertEquals(2, records.size()); Assert.assertEquals(Utils.format("file-{}.log", i), records.get(0).getHeader().getAttribute(HeaderAttributeConstants.FILE_NAME)); try { Assert.assertEquals( String.valueOf(Files .getLastModifiedTime( Paths.get(f.getAbsolutePath(), Utils.format("file-{}.log", i))) .toMillis()), records.get(0).getHeader() .getAttribute(HeaderAttributeConstants.LAST_MODIFIED_TIME)); } catch (IOException ex) { Assert.fail(ex.toString()); } Assert.assertEquals("a-" + i, records.get(0).get("/A").getValueAsString()); Assert.assertEquals("b-" + i, records.get(0).get("/B").getValueAsString()); Assert.assertEquals("a", records.get(1).get("/A").getValueAsString()); Assert.assertEquals("b", records.get(1).get("/B").getValueAsString()); // And error record List<Record> errorRecords = runner.getErrorRecords(); Assert.assertEquals(0, errorRecords.size()); } else if (i < 9) { List<Record> records = output.getRecords().get("lane"); Assert.assertNotNull(records); Assert.assertTrue(!records.isEmpty()); Assert.assertEquals(2, records.size()); Assert.assertEquals("a.log", records.get(0).getHeader().getAttribute(HeaderAttributeConstants.FILE_NAME)); Assert.assertEquals("Gollum", records.get(0).get("/A").getValueAsString()); Assert.assertEquals("Sauron", records.get(0).get("/B").getValueAsString()); Assert.assertEquals("Aragorn", records.get(1).get("/A").getValueAsString()); Assert.assertEquals("Boromir", records.get(1).get("/B").getValueAsString()); } else if (i < 10) { List<Record> records = output.getRecords().get("lane"); Assert.assertTrue(records.isEmpty()); // And error record records = runner.getErrorRecords(); Assert.assertEquals(0, records.size()); } else if (i < 11) { // And a bunch of event records... // new-file event, finished-file event for each file. // file-0.log through file-7.log and a.log (9 files) // two no-more-data events. Assert.assertEquals(20, runner.getEventRecords().size()); Map<String, Integer> map = new HashMap<>(); for (EventRecord rec : runner.getEventRecords()) { if (map.get(rec.getEventType()) != null) { map.put(rec.getEventType(), map.get(rec.getEventType()) + 1); } else { map.put(rec.getEventType(), 1); } } Assert.assertNotNull(map.get("new-file")); Assert.assertNotNull(map.get("finished-file")); Assert.assertNotNull(map.get("no-more-data")); int numEvents = map.get("new-file"); Assert.assertEquals(9, numEvents); numEvents = map.get("finished-file"); Assert.assertEquals(9, numEvents); numEvents = map.get("no-more-data"); Assert.assertEquals(2, numEvents); } else { runner.setStop(); } }); runner.waitOnProduce(); Assert.assertEquals(12, batchCount.get()); } finally { runner.runDestroy(); } }
From source file:it.greenvulcano.util.txt.TextUtils.java
/** * Writes a text String into a file/*w w w . j a v a 2s . co m*/ * * @param contentStrings * The StringBuffer to be written into the file * @param file * The destination file * @param endline * The line terminator, if null or empty is used '\n' * @param append * If true the data are appended to existent file * @throws IOException */ public static void writeFile(List<String> contentStrings, File file, String endline, boolean append) throws IOException { FileOutputStream out = null; try { if ((endline == null) || endline.equals("")) { endline = "\n"; } out = new FileOutputStream(file, append); IOUtils.writeLines(contentStrings, endline, out); out.flush(); } finally { if (out != null) { out.close(); } } }