List of usage examples for java.nio.file Files write
public static Path write(Path path, Iterable<? extends CharSequence> lines, Charset cs, OpenOption... options) throws IOException
From source file:org.eclipse.winery.generators.ia.Generator.java
/** * Iterates recursively through all the files in the project working * directory and tries to replace the global placeholders. * /* w ww .j av a 2s. c o m*/ * @param folderOrFile to start with */ private void updateFilesRecursively(File folderOrFile) { if (folderOrFile.isFile()) { if (folderOrFile.getAbsolutePath().endsWith(".jar")) { return; } Generator.logger.trace("Updating file " + folderOrFile); try { // Read file and replace placeholders Charset cs = Charset.defaultCharset(); List<String> lines = new ArrayList<>(); for (String line : Files.readAllLines(folderOrFile.toPath(), cs)) { line = line.replaceAll(Generator.PLACEHOLDER_CLASS_NAME, this.name); line = line.replaceAll(Generator.PLACEHOLDER_JAVA_PACKAGE, this.javaPackage); line = line.replaceAll(Generator.PLACEHOLDER_NAMESPACE, this.namespace); line = line.replaceAll(Generator.PLACEHOLDER_IA_ARTIFACT_TEMPLATE_UPLOAD_URL, this.iaArtifactTemplateUploadUrl.toString()); lines.add(line); } // Write file OpenOption[] options = new OpenOption[] { StandardOpenOption.WRITE, StandardOpenOption.TRUNCATE_EXISTING }; Files.write(folderOrFile.toPath(), lines, cs, options); } catch (IOException e) { e.printStackTrace(); } } else { Generator.logger.trace("Updating folder " + folderOrFile); for (File childFile : folderOrFile.listFiles()) { this.updateFilesRecursively(childFile); } } }
From source file:org.ballerinalang.test.packaging.PackagingNegativeTestCase.java
/** * Override content in file./*from w w w . j a v a2 s . com*/ * * @param filePath path of file to override * @param content content to be written * @throws IOException */ private void writeToFile(Path filePath, String content) throws IOException { Files.write(filePath, content.getBytes(), StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING); }
From source file:org.codice.ddf.configuration.migration.ConfigurationMigrationManagerTest.java
private void createBrandingFile(String branding) throws IOException { final File brandingFile = new File(ddfHome.resolve("Branding.txt").toString()); brandingFile.createNewFile();// ww w.j a va 2s. c om Files.write(brandingFile.toPath(), branding.getBytes(), StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING); }
From source file:org.codice.ddf.configuration.migration.ConfigurationMigrationManagerTest.java
private void createVersionFile(String version) throws IOException { File versionFile = new File(ddfHome.resolve("Version.txt").toString()); versionFile.createNewFile();/*from ww w. j a va 2 s . co m*/ Files.write(versionFile.toPath(), version.getBytes(), StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING); }
From source file:org.codice.ddf.configuration.migration.ConfigurationMigrationManagerTest.java
private void createMigrationPropsFile(String contents) throws IOException { Path migrationPropsPath = ddfHome.resolve(Paths.get("etc", "migration.properties")); Files.createDirectories(migrationPropsPath.getParent()); Files.createFile(migrationPropsPath); Files.write(migrationPropsPath, contents.getBytes(), StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING); }
From source file:com.streamsets.pipeline.stage.origin.logtail.TestFileTailSource.java
@Test public void testPendingFilesMetric() throws Exception { final File testDataDir = new File("target", UUID.randomUUID().toString()); Assert.assertTrue(testDataDir.mkdirs()); final List<File> files = Arrays.asList(new File(testDataDir, "file.txt-1"), new File(testDataDir, "file.txt-2"), new File(testDataDir, "file.txt-3"), new File(testDataDir, "file.txt-4"), new File(testDataDir, "file.txt-5"), new File(testDataDir, "file.txt-6"), new File(testDataDir, "file.txt-7"), new File(testDataDir, "file.txt-8")); //We will create first 4 files here. Rest of the four files will be created //before we calculate the pending files metric. for (int i = 0; i < 4; i++) { File file = files.get(i); Files.write(file.toPath(), Arrays.asList("A", "B", "C"), StandardCharsets.UTF_8, StandardOpenOption.CREATE_NEW); }/*from w ww . j a va 2 s .co m*/ FileTailSource source = PowerMockito.spy((FileTailSource) createSourceForPeriodicFile( testDataDir.getAbsolutePath() + "/file.txt-${PATTERN}", "[0-9]")); //Intercept calculatePendingFilesMetric private method which calculates the pendingFiles //and create new files. PowerMockito.replace(MemberMatcher.method(FileTailSource.class, "calculatePendingFilesMetric")) .with(new InvocationHandler() { @Override public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { //Create the remaining 4 files so as to have files which are pending and not being started for processing. for (int i = 4; i < 8; i++) { File file = files.get(i); Files.write(file.toPath(), Arrays.asList("A", "B", "C"), StandardCharsets.UTF_8, StandardOpenOption.CREATE_NEW); } //call the real getOffsetsLag private method return method.invoke(proxy, args); } }); SourceRunner runner = createRunner(source); try { runner.runInit(); StageRunner.Output output = runner.runProduce(null, 36); // Make sure there are only 12 (3 for each file we read). Assert.assertEquals(12, output.getRecords().get("lane").size()); Map<String, Counter> pendingFilesMetric = (Map<String, Counter>) Whitebox.getInternalState(source, "pendingFilesMetric"); Assert.assertEquals(4L, pendingFilesMetric .get(testDataDir.getAbsolutePath() + "/file.txt-${PATTERN}||[0-9]").getCount()); } finally { runner.runDestroy(); } }
From source file:com.streamsets.pipeline.stage.origin.logtail.TestFileTailSource.java
@Test public void testGlobbingForPeriodicRollPattern() throws Exception { final File testDataDir = new File("target", UUID.randomUUID().toString()); Assert.assertTrue(testDataDir.mkdirs()); final List<Path> dirPaths = Arrays.asList( Paths.get(testDataDir.getAbsolutePath() + File.separatorChar + "a1" + File.separatorChar + "b1"), Paths.get(testDataDir.getAbsolutePath() + File.separatorChar + "a2" + File.separatorChar + "b2"), Paths.get(testDataDir.getAbsolutePath() + File.separatorChar + "a3" + File.separatorChar + "b3"), Paths.get(testDataDir.getAbsolutePath() + File.separatorChar + "a4" + File.separatorChar + "b4"), Paths.get(testDataDir.getAbsolutePath() + File.separatorChar + "a5" + File.separatorChar + "b5"), Paths.get(testDataDir.getAbsolutePath() + File.separatorChar + "a6" + File.separatorChar + "b6")); for (int i = 0; i < dirPaths.size(); i++) { Path dirPath = dirPaths.get(i); Files.createDirectories(dirPath); Path filePath1 = Paths .get(dirPath.toString() + File.separatorChar + "file-" + String.valueOf(i) + ".txt"); Files.write(filePath1, Arrays.asList("A", "B", "C"), StandardCharsets.UTF_8, StandardOpenOption.CREATE_NEW); }/*from w w w . j a va 2 s .co m*/ FileInfo fileInfo = new FileInfo(); //We are looking for testdataDir/*/*/file-[0-9]+.txt fileInfo.fileFullPath = testDataDir.getAbsolutePath() + File.separatorChar + "*" + File.separatorChar + "*" + File.separatorChar + "file-${PATTERN}.txt"; fileInfo.fileRollMode = FileRollMode.PATTERN; fileInfo.firstFile = ""; fileInfo.patternForToken = "[0-9]+"; FileTailConfigBean conf = new FileTailConfigBean(); conf.dataFormat = DataFormat.TEXT; conf.multiLineMainPattern = ""; conf.batchSize = 25; conf.maxWaitTimeSecs = 1; conf.fileInfos = Arrays.asList(fileInfo); conf.postProcessing = PostProcessingOptions.NONE; conf.dataFormatConfig.textMaxLineLen = 1024; FileTailSource source = new FileTailSource(conf, SCAN_INTERVAL); SourceRunner runner = createRunner(source); try { // run till current end and stop pipeline runner.runInit(); StageRunner.Output output = runner.runProduce(null, 30); // (6 folders * 1 file * 3 records) = 18 records Assert.assertEquals(18L, output.getRecords().get("lane").size()); } finally { runner.runDestroy(); } }
From source file:com.streamsets.pipeline.stage.origin.logtail.TestFileTailSource.java
@Test public void testMetricsWithGlobbingAndLateDirectory() throws Exception { final File testDataDir = new File("target", UUID.randomUUID().toString()); final List<Path> dirPaths = Arrays.asList( Paths.get(testDataDir.getAbsolutePath() + File.separatorChar + "a1" + File.separatorChar + "const" + File.separatorChar + "b1"), Paths.get(testDataDir.getAbsolutePath() + File.separatorChar + "a2" + File.separatorChar + "const" + File.separatorChar + "b2"), Paths.get(testDataDir.getAbsolutePath() + File.separatorChar + "a3" + File.separatorChar + "const" + File.separatorChar + "b3"), Paths.get(testDataDir.getAbsolutePath() + File.separatorChar + "a4" + File.separatorChar + "const" + File.separatorChar + "b4"), Paths.get(testDataDir.getAbsolutePath() + File.separatorChar + "a5" + File.separatorChar + "const" + File.separatorChar + "b5"), Paths.get(testDataDir.getAbsolutePath() + File.separatorChar + "a6" + File.separatorChar + "const" + File.separatorChar + "b6"), Paths.get(testDataDir.getAbsolutePath() + File.separatorChar + "a7" + File.separatorChar + "const" + File.separatorChar + "b7"), Paths.get(testDataDir.getAbsolutePath() + File.separatorChar + "a8" + File.separatorChar + "const" + File.separatorChar + "b8"), Paths.get(testDataDir.getAbsolutePath() + File.separatorChar + "a9" + File.separatorChar + "const" + File.separatorChar + "b9"), Paths.get(testDataDir.getAbsolutePath() + File.separatorChar + "a10" + File.separatorChar + "const" + File.separatorChar + "b10")); FileInfo fileInfo = new FileInfo(); //We are looking for testdataDir/*/const/*/file-[0-9]+.txt fileInfo.fileFullPath = testDataDir.getAbsolutePath() + File.separatorChar + "*" + File.separatorChar + "const" + File.separatorChar + "*" + File.separatorChar + "file-${PATTERN}.txt"; fileInfo.fileRollMode = FileRollMode.PATTERN; fileInfo.firstFile = ""; fileInfo.patternForToken = "[0-9]+"; FileTailConfigBean conf = new FileTailConfigBean(); conf.dataFormat = DataFormat.TEXT;/*from w w w . j a v a 2 s. c om*/ conf.multiLineMainPattern = ""; conf.batchSize = 40; conf.maxWaitTimeSecs = 40; conf.fileInfos = Arrays.asList(fileInfo); conf.postProcessing = PostProcessingOptions.NONE; conf.allowLateDirectories = true; conf.dataFormatConfig.textMaxLineLen = 1024; FileTailSource source = PowerMockito.spy(new FileTailSource(conf, SCAN_INTERVAL)); SourceRunner runner = createRunner(source); // run till current end and stop pipeline runner.runInit(); //Late directory(testDataDir) creation Assert.assertTrue(testDataDir.mkdirs()); for (int i = 0; i < dirPaths.size(); i++) { Path dirPath = dirPaths.get(i); Files.createDirectories(dirPath); Path filePath = Paths .get(dirPath.toString() + File.separatorChar + "file-" + String.valueOf(i) + ".txt"); Files.write(filePath, Arrays.asList("A", "B", "C", "D", "E", "F", "G", "H", "I", "J"), //10 records * 2 bytes = 20 bytes. StandardCharsets.UTF_8, StandardOpenOption.CREATE_NEW); } setSleepTimeForFindingPaths(10000L, 2000L); try { //We have totally 10(folders) * 1(file) * 10 (records) = 100 records //Also means totally 100 records * 2 bytes = 200 bytes. //We will read only 20 records. //This means the total is (remaining 80 records * 2 bytes) = 160 bytes yet to be read. //including pending files and offsetLag. StageRunner.Output output = runner.runProduce(null, 20); Assert.assertEquals(20L, output.getRecords().get("lane").size()); checkPendingAndOffsetLag(source, 160L); //All files would have been found, don't need to wait for finding them. setSleepTimeForFindingPaths(0L, 0L); //We are gonna read another 40 records and check //If we read 40 more records this means, we will end up reading another 40*2 = 80 bytes //Total remaining bytes to read is 160 - 80 = 80 bytes output = runner.runProduce(output.getNewOffset(), 40); Assert.assertEquals(40L, output.getRecords().get("lane").size()); checkPendingAndOffsetLag(source, 80L); //We are gonna read 40 records and check //If we read 40 more records this means, we will end up reading another 40*2 = 80 bytes //Total remaining bytes to read is 80 - 80 = 0 bytes. output = runner.runProduce(output.getNewOffset(), 40); Assert.assertEquals(40L, output.getRecords().get("lane").size()); checkPendingAndOffsetLag(source, 0L); } finally { runner.runDestroy(); } }