List of usage examples for java.nio.file Files newOutputStream
public static OutputStream newOutputStream(Path path, OpenOption... options) throws IOException
From source file:edu.cmu.tetrad.cli.search.FgsCli.java
/** * @param args the command line arguments *///from ww w .j a va 2 s .co m public static void main(String[] args) { if (args == null || args.length == 0 || Args.hasLongOption(args, "help")) { Args.showHelp("fgs", MAIN_OPTIONS); return; } parseArgs(args); System.out.println("================================================================================"); System.out.printf("FGS Discrete (%s)%n", DateTime.printNow()); System.out.println("================================================================================"); String argInfo = createArgsInfo(); System.out.println(argInfo); LOGGER.info("=== Starting FGS Discrete: " + Args.toString(args, ' ')); LOGGER.info(argInfo.trim().replaceAll("\n", ",").replaceAll(" = ", "=")); Set<String> excludedVariables = (excludedVariableFile == null) ? Collections.EMPTY_SET : getExcludedVariables(); runPreDataValidations(excludedVariables, System.err); DataSet dataSet = readInDataSet(excludedVariables); runOptionalDataValidations(dataSet, System.err); Path outputFile = Paths.get(dirOut.toString(), outputPrefix + ".txt"); try (PrintStream writer = new PrintStream( new BufferedOutputStream(Files.newOutputStream(outputFile, StandardOpenOption.CREATE)))) { String runInfo = createOutputRunInfo(excludedVariables, dataSet); writer.println(runInfo); String[] infos = runInfo.trim().replaceAll("\n\n", ";").split(";"); for (String s : infos) { LOGGER.info(s.trim().replaceAll("\n", ",").replaceAll(":,", ":").replaceAll(" = ", "=")); } Graph graph = runFgs(dataSet, writer); writer.println(); writer.println(graph.toString()); } catch (IOException exception) { LOGGER.error("FGS failed.", exception); System.err.printf("%s: FGS failed.%n", DateTime.printNow()); System.out.println("Please see log file for more information."); System.exit(-128); } System.out.printf("%s: FGS finished! Please see %s for details.%n", DateTime.printNow(), outputFile.getFileName().toString()); LOGGER.info( String.format("FGS finished! Please see %s for details.", outputFile.getFileName().toString())); }
From source file:edu.cmu.tetrad.cli.search.FgsDiscrete.java
/** * @param args the command line arguments */// www. j a v a 2 s . com public static void main(String[] args) { if (args == null || args.length == 0 || Args.hasLongOption(args, "help")) { Args.showHelp("fgs-discrete", MAIN_OPTIONS); return; } parseArgs(args); System.out.println("================================================================================"); System.out.printf("FGS Discrete (%s)%n", DateTime.printNow()); System.out.println("================================================================================"); String argInfo = createArgsInfo(); System.out.println(argInfo); LOGGER.info("=== Starting FGS Discrete: " + Args.toString(args, ' ')); LOGGER.info(argInfo.trim().replaceAll("\n", ",").replaceAll(" = ", "=")); Set<String> excludedVariables = (excludedVariableFile == null) ? Collections.EMPTY_SET : getExcludedVariables(); runPreDataValidations(excludedVariables, System.err); DataSet dataSet = readInDataSet(excludedVariables); runOptionalDataValidations(dataSet, System.err); Path outputFile = Paths.get(dirOut.toString(), outputPrefix + ".txt"); try (PrintStream writer = new PrintStream( new BufferedOutputStream(Files.newOutputStream(outputFile, StandardOpenOption.CREATE)))) { String runInfo = createOutputRunInfo(excludedVariables, dataSet); writer.println(runInfo); String[] infos = runInfo.trim().replaceAll("\n\n", ";").split(";"); for (String s : infos) { LOGGER.info(s.trim().replaceAll("\n", ",").replaceAll(":,", ":").replaceAll(" = ", "=")); } Graph graph = runFgsDiscrete(dataSet, writer); writer.println(); writer.println(graph.toString()); if (graphML) { writeOutGraphML(graph, Paths.get(dirOut.toString(), outputPrefix + "_graph.txt")); } } catch (IOException exception) { LOGGER.error("FGS Discrete failed.", exception); System.err.printf("%s: FGS Discrete failed.%n", DateTime.printNow()); System.out.println("Please see log file for more information."); System.exit(-128); } System.out.printf("%s: FGS Discrete finished! Please see %s for details.%n", DateTime.printNow(), outputFile.getFileName().toString()); LOGGER.info(String.format("FGS Discrete finished! Please see %s for details.", outputFile.getFileName().toString())); }
From source file:com.github.ansell.shp.SHPDump.java
public static void main(String... args) throws Exception { final OptionParser parser = new OptionParser(); final OptionSpec<Void> help = parser.accepts("help").forHelp(); final OptionSpec<File> input = parser.accepts("input").withRequiredArg().ofType(File.class).required() .describedAs("The input SHP file"); final OptionSpec<File> output = parser.accepts("output").withRequiredArg().ofType(File.class).required() .describedAs("The output directory to use for debugging files"); final OptionSpec<String> outputPrefix = parser.accepts("prefix").withRequiredArg().ofType(String.class) .defaultsTo("shp-debug").describedAs("The output prefix to use for debugging files"); final OptionSpec<File> outputMappingTemplate = parser.accepts("output-mapping").withRequiredArg() .ofType(File.class).describedAs("The output mapping template file if it needs to be generated."); final OptionSpec<Integer> resolution = parser.accepts("resolution").withRequiredArg().ofType(Integer.class) .defaultsTo(2048).describedAs("The output image file resolution"); final OptionSpec<String> format = parser.accepts("format").withRequiredArg().ofType(String.class) .defaultsTo("png").describedAs("The output image format"); final OptionSpec<String> removeIfEmpty = parser.accepts("remove-if-empty").withRequiredArg() .ofType(String.class).describedAs( "The name of an attribute to remove if its value is empty before outputting the resulting shapefile. Use multiple times to specify multiple fields to check"); OptionSet options = null;/*from w w w. j a v a 2 s .com*/ try { options = parser.parse(args); } catch (final OptionException e) { System.out.println(e.getMessage()); parser.printHelpOn(System.out); throw e; } if (options.has(help)) { parser.printHelpOn(System.out); return; } final Path inputPath = input.value(options).toPath(); if (!Files.exists(inputPath)) { throw new FileNotFoundException("Could not find input SHP file: " + inputPath.toString()); } final Path outputPath = output.value(options).toPath(); if (!Files.exists(outputPath)) { throw new FileNotFoundException("Output directory does not exist: " + outputPath.toString()); } final Path outputMappingPath = options.has(outputMappingTemplate) ? outputMappingTemplate.value(options).toPath() : null; if (options.has(outputMappingTemplate) && Files.exists(outputMappingPath)) { throw new FileNotFoundException( "Output mapping template file already exists: " + outputMappingPath.toString()); } final Set<String> filterFields = ConcurrentHashMap.newKeySet(); if (options.has(removeIfEmpty)) { for (String nextFilterField : removeIfEmpty.values(options)) { System.out.println("Will filter field if empty value found: " + nextFilterField); filterFields.add(nextFilterField); } } if (!filterFields.isEmpty()) { System.out.println("Full set of filter fields: " + filterFields); } final String prefix = outputPrefix.value(options); FileDataStore store = FileDataStoreFinder.getDataStore(inputPath.toFile()); if (store == null) { throw new RuntimeException("Could not read the given input as an ESRI Shapefile: " + inputPath.toAbsolutePath().toString()); } for (String typeName : new LinkedHashSet<>(Arrays.asList(store.getTypeNames()))) { System.out.println(""); System.out.println("Type: " + typeName); SimpleFeatureSource featureSource = store.getFeatureSource(typeName); SimpleFeatureType schema = featureSource.getSchema(); Name outputSchemaName = new NameImpl(schema.getName().getNamespaceURI(), schema.getName().getLocalPart().replace(" ", "").replace("%20", "")); System.out.println("Replacing name on schema: " + schema.getName() + " with " + outputSchemaName); SimpleFeatureType outputSchema = SHPUtils.changeSchemaName(schema, outputSchemaName); List<String> attributeList = new ArrayList<>(); for (AttributeDescriptor attribute : schema.getAttributeDescriptors()) { System.out.println("Attribute: " + attribute.getName().toString()); attributeList.add(attribute.getName().toString()); } CsvSchema csvSchema = CSVUtil.buildSchema(attributeList); SimpleFeatureCollection collection = featureSource.getFeatures(); int featureCount = 0; Path nextCSVFile = outputPath.resolve(prefix + ".csv"); Path nextSummaryCSVFile = outputPath .resolve(prefix + "-" + outputSchema.getTypeName() + "-Summary.csv"); List<SimpleFeature> outputFeatureList = new CopyOnWriteArrayList<>(); try (SimpleFeatureIterator iterator = collection.features(); Writer bufferedWriter = Files.newBufferedWriter(nextCSVFile, StandardCharsets.UTF_8, StandardOpenOption.CREATE_NEW); SequenceWriter csv = CSVUtil.newCSVWriter(bufferedWriter, csvSchema);) { List<String> nextLine = new ArrayList<>(); while (iterator.hasNext()) { SimpleFeature feature = iterator.next(); featureCount++; if (featureCount <= 2) { System.out.println(""); System.out.println(feature.getIdentifier()); } else if (featureCount % 100 == 0) { System.out.print("."); } boolean filterThisFeature = false; for (AttributeDescriptor attribute : schema.getAttributeDescriptors()) { String featureString = Optional.ofNullable(feature.getAttribute(attribute.getName())) .orElse("").toString(); nextLine.add(featureString); if (filterFields.contains(attribute.getName().toString()) && featureString.trim().isEmpty()) { filterThisFeature = true; } if (featureString.length() > 100) { featureString = featureString.substring(0, 100) + "..."; } if (featureCount <= 2) { System.out.print(attribute.getName() + "="); System.out.println(featureString); } } if (!filterThisFeature) { outputFeatureList.add(SHPUtils.changeSchemaName(feature, outputSchema)); csv.write(nextLine); } nextLine.clear(); } } try (Reader csvReader = Files.newBufferedReader(nextCSVFile, StandardCharsets.UTF_8); Writer summaryOutput = Files.newBufferedWriter(nextSummaryCSVFile, StandardCharsets.UTF_8, StandardOpenOption.CREATE_NEW); final Writer mappingWriter = options.has(outputMappingTemplate) ? Files.newBufferedWriter(outputMappingPath) : NullWriter.NULL_WRITER) { CSVSummariser.runSummarise(csvReader, summaryOutput, mappingWriter, CSVSummariser.DEFAULT_SAMPLE_COUNT, false); } if (featureCount > 100) { System.out.println(""); } System.out.println(""); System.out.println("Feature count: " + featureCount); SimpleFeatureCollection outputCollection = new ListFeatureCollection(outputSchema, outputFeatureList); Path outputShapefilePath = outputPath.resolve(prefix + "-" + outputSchema.getTypeName() + "-dump"); if (!Files.exists(outputShapefilePath)) { Files.createDirectory(outputShapefilePath); } SHPUtils.writeShapefile(outputCollection, outputShapefilePath); // Create ZIP file from the contents to keep the subfiles together Path outputShapefileZipPath = outputPath .resolve(prefix + "-" + outputSchema.getTypeName() + "-dump.zip"); try (final OutputStream out = Files.newOutputStream(outputShapefileZipPath, StandardOpenOption.CREATE_NEW); final ZipOutputStream zip = new ZipOutputStream(out, StandardCharsets.UTF_8);) { Files.list(outputShapefilePath).forEachOrdered(Unchecked.consumer(e -> { zip.putNextEntry(new ZipEntry(e.getFileName().toString())); Files.copy(e, zip); zip.closeEntry(); })); } try (final OutputStream outputStream = Files.newOutputStream( outputPath.resolve(prefix + "." + format.value(options)), StandardOpenOption.CREATE_NEW);) { MapContent map = new MapContent(); map.setTitle(prefix + "-" + outputSchema.getTypeName()); Style style = SLD.createSimpleStyle(featureSource.getSchema()); Layer layer = new FeatureLayer(new CollectionFeatureSource(outputCollection), style); map.addLayer(layer); SHPUtils.renderImage(map, outputStream, resolution.value(options), format.value(options)); } } }
From source file:eu.itesla_project.iidm.datasource.Bzip2FileDataSource.java
@Override public OutputStream newOutputStream(String suffix, String ext, boolean append) throws IOException { Path path = getPath(suffix, ext); OutputStream os = new BZip2CompressorOutputStream(new BufferedOutputStream( Files.newOutputStream(path, append ? StandardOpenOption.APPEND : StandardOpenOption.CREATE))); return observer != null ? new ObservableOutputStream(os, path.toString(), observer) : os; }
From source file:com.yahoo.rdl.maven.RdlExecutableFileProviderImpl.java
@Override public Path getRdlExecutableFile() throws MojoExecutionException { if (configuredExecutableFile != null) { return configuredExecutableFile; }/*from w w w . j av a 2 s . c om*/ if (actualExecutableFile != null) { return actualExecutableFile; } File binFolder = new File(scratchSpace.toFile(), "bin"); if (!binFolder.mkdirs() && !binFolder.exists()) { throw new MojoExecutionException("Unable to create folder for rdl executable: " + binFolder); } Path rdlBinaryPath = scratchSpace.resolve(Paths.get("bin", "rdl")); try (OutputStream os = Files.newOutputStream(rdlBinaryPath, StandardOpenOption.CREATE)) { try (InputStream is = getClass().getClassLoader().getResourceAsStream("bin/" + osName + "/rdl")) { IOUtils.copy(is, os); } } catch (IOException e) { throw new MojoExecutionException("Unable to write rdl binary to " + rdlBinaryPath, e); } if (!rdlBinaryPath.toFile().setExecutable(true) && !rdlBinaryPath.toFile().canExecute()) { throw new MojoExecutionException("Unable to chmod +x executable: " + rdlBinaryPath.toAbsolutePath()); } Path rdlGenSwaggerBinaryPath = scratchSpace.resolve(Paths.get("bin", "rdl-gen-swagger")); try (OutputStream os = Files.newOutputStream(rdlGenSwaggerBinaryPath, StandardOpenOption.CREATE)) { try (InputStream is = getClass().getClassLoader() .getResourceAsStream("bin/" + osName + "/rdl-gen-swagger")) { IOUtils.copy(is, os); } } catch (IOException e) { throw new MojoExecutionException("Unable to write rdl-gen-swagger binary to " + rdlBinaryPath, e); } if (!rdlGenSwaggerBinaryPath.toFile().setExecutable(true) && !rdlGenSwaggerBinaryPath.toFile().canExecute()) { throw new MojoExecutionException("Unable to chmod +x executable: " + rdlBinaryPath.toAbsolutePath()); } actualExecutableFile = rdlBinaryPath; return rdlBinaryPath; }
From source file:com.google.jenkins.plugins.persistentmaster.volume.zip.ZipCreator.java
ZipCreator(Path zip) throws IOException { zipPath = Preconditions.checkNotNull(zip); Preconditions.checkArgument(!Files.exists(zipPath), "zip file exists"); logger.finer("Creating zip volume for path: " + zipPath); zipStream = new ZipArchiveOutputStream(Files.newOutputStream(zipPath, StandardOpenOption.CREATE_NEW)); // unfortunately there is no typesafe way of doing this zipStream.setEncoding(UTF_8);/* www . j a v a 2 s. c o m*/ zipStream.setUseZip64(Zip64Mode.AsNeeded); }
From source file:de.cebitec.readXplorer.plotting.ChartExporter.java
@Override public void run() { notifyObservers(ChartExportStatus.RUNNING); Rectangle bounds = new Rectangle(1920, 1080); DOMImplementation dom = GenericDOMImplementation.getDOMImplementation(); Document document = dom.createDocument(null, "svg", null); SVGGraphics2D generator = new SVGGraphics2D(document); chart.draw(generator, bounds);/*from w w w .ja va2 s .c o m*/ try (OutputStream outputStream = Files.newOutputStream(file, StandardOpenOption.CREATE)) { Writer out = new OutputStreamWriter(outputStream, "UTF-8"); generator.stream(out, true); outputStream.flush(); notifyObservers(ChartExportStatus.FINISHED); } catch (IOException ex) { notifyObservers(ChartExportStatus.FAILED); } }
From source file:org.apache.pulsar.io.file.TestFileGenerator.java
private final void createFile() { try {/*from w w w .ja va 2 s . c o m*/ Path path = Files.createTempFile(tempDir, prefix, suffix, attrs); try (OutputStream out = Files.newOutputStream(path, StandardOpenOption.APPEND)) { for (int idx = 0; idx < numLines; idx++) { IOUtils.write(RandomStringUtils.random(50, true, false) + "\n", out, "UTF-8"); } } producedFiles.put(path.toFile()); } catch (IOException | InterruptedException e) { e.printStackTrace(); } }
From source file:com.frostwire.gui.library.DownloadTask.java
@Override public void run() { if (!isRunning()) { return;//from w w w .j av a 2 s .co m } File lastFile = null; try { setProgress(0); if (!savePath.exists()) { savePath.mkdirs(); } long totalBytes = getTotalBytes(); long totalWritten = 0; for (int i = 0; i < fds.length; i++) { if (!isRunning()) { return; } currentFD = fds[i]; GUIMediator.safeInvokeLater(new Runnable() { public void run() { String status = String.format("%s from %s - %s", I18n.tr("Downloading"), device.getName(), currentFD.title); LibraryMediator.instance().getLibrarySearch().pushStatus(status); } }); URL url = new URL(device.getDownloadURL(currentFD)); InputStream is = null; OutputStream fos = null; try { is = url.openStream(); String filename = OSUtils.escapeFilename(FilenameUtils.getName(currentFD.filePath)); File file = buildFile(savePath, filename); Path incompleteFile = buildIncompleteFile(file).toPath(); lastFile = file.getAbsoluteFile(); fos = Files.newOutputStream(incompleteFile, StandardOpenOption.CREATE);// new FileOutputStream(incompleteFile); byte[] buffer = new byte[4 * 1024]; int n = 0; while ((n = is.read(buffer, 0, buffer.length)) != -1) { if (!isRunning()) { return; } fos.write(buffer, 0, n); fos.flush(); totalWritten += n; setProgress((int) ((totalWritten * 100) / totalBytes)); if (getProgress() % 5 == 0) { GUIMediator.safeInvokeLater(new Runnable() { public void run() { String status = String.format("%d%% %s from %s - %s", getProgress(), I18n.tr("Downloading"), device.getName(), currentFD.title); LibraryMediator.instance().getLibrarySearch().pushStatus(status); } }); } //System.out.println("Progress: " + getProgress() + " Total Written: " + totalWritten + " Total Bytes: " + totalBytes); } close(fos); Files.move(incompleteFile, file.toPath(), StandardCopyOption.REPLACE_EXISTING); } finally { close(is); close(fos); } } setProgress(100); } catch (Throwable e) { e.printStackTrace(); onError(e); GUIMediator.safeInvokeLater(new Runnable() { public void run() { LibraryMediator.instance().getLibrarySearch() .pushStatus(I18n.tr("Wi-Fi download error. Please try again.")); } }); } finally { GUIMediator.safeInvokeLater(new Runnable() { public void run() { LibraryMediator.instance().getLibrarySearch().revertStatus(); } }); if (lastFile != null) { GUIMediator.launchExplorer(lastFile); } } stop(); }
From source file:io.redlink.solrlib.embedded.EmbeddedCoreContainer.java
@Override @SuppressWarnings({ "squid:S3725", "squid:S3776" }) protected synchronized void init(ExecutorService executorService) throws IOException { Preconditions.checkState(Objects.isNull(coreContainer), "Already initialized!"); if (solrHome == null) { solrHome = Files.createTempDirectory("solr-home"); log.debug("No solr-home set, using temp directory {}", solrHome); deleteOnShutdown = true;//from w ww . j a va 2 s. c o m } final Path absoluteSolrHome = this.solrHome.toAbsolutePath(); if (Files.isDirectory(absoluteSolrHome)) { log.trace("solr-home exists: {}", absoluteSolrHome); } else { Files.createDirectories(absoluteSolrHome); log.debug("Created solr-home: {}", absoluteSolrHome); } final Path lib = absoluteSolrHome.resolve("lib"); if (Files.isDirectory(lib)) { log.trace("lib-directory exists: {}", lib); } else { Files.createDirectories(lib); log.debug("Created solr-lib directory: {}", lib); } final Path solrXml = absoluteSolrHome.resolve("solr.xml"); if (!Files.exists(solrXml)) { log.info("no solr.xml found, creating new at {}", solrXml); try (PrintStream writer = new PrintStream(Files.newOutputStream(solrXml, StandardOpenOption.CREATE))) { writer.printf("<!-- Generated by %s on %tF %<tT -->%n", getClass().getSimpleName(), new Date()); writer.println("<solr>"); writer.printf(" <str name=\"%s\">%s</str>%n", "sharedLib", absoluteSolrHome.relativize(lib)); writer.println("</solr>"); } } else { log.trace("found solr.xml: {}", solrXml); } for (SolrCoreDescriptor coreDescriptor : coreDescriptors) { final String coreName = coreDescriptor.getCoreName(); if (availableCores.containsKey(coreName)) { log.warn("CoreName-Clash: {} already initialized. Skipping {}", coreName, coreDescriptor.getClass()); continue; } final Path coreDir = absoluteSolrHome.resolve(coreName); Files.createDirectories(coreDir); coreDescriptor.initCoreDirectory(coreDir, lib); final Properties coreProperties = new Properties(); final Path corePropertiesFile = coreDir.resolve("core.properties"); if (Files.exists(corePropertiesFile)) { try (InputStream inStream = Files.newInputStream(corePropertiesFile, StandardOpenOption.CREATE)) { coreProperties.load(inStream); } log.debug("core.properties for {} found, updating", coreName); } else { log.debug("Creating new core {} in {}", coreName, coreDir); } coreProperties.setProperty("name", coreName); try (OutputStream outputStream = Files.newOutputStream(corePropertiesFile)) { coreProperties.store(outputStream, null); } if (coreDescriptor.getNumShards() > 1 || coreDescriptor.getReplicationFactor() > 1) { log.warn("Deploying {} to EmbeddedCoreContainer, ignoring config of shards={},replication={}", coreName, coreDescriptor.getNumShards(), coreDescriptor.getReplicationFactor()); } availableCores.put(coreName, coreDescriptor); } log.info("Starting {} in solr-home '{}'", getClass().getSimpleName(), absoluteSolrHome); coreContainer = CoreContainer.createAndLoad(absoluteSolrHome, solrXml); availableCores.values().forEach(coreDescriptor -> { final String coreName = coreDescriptor.getCoreName(); try (SolrClient solrClient = createSolrClient(coreName)) { final NamedList<Object> coreStatus = CoreAdminRequest.getStatus(coreName, solrClient) .getCoreStatus(coreName); final NamedList<Object> indexStatus = coreStatus == null ? null : (NamedList<Object>) coreStatus.get("index"); final Object lastModified = indexStatus == null ? null : indexStatus.get("lastModified"); // lastModified is null if there was never a update scheduleCoreInit(executorService, coreDescriptor, lastModified == null); } catch (SolrServerException | IOException e) { if (log.isDebugEnabled()) { log.error("Error initializing core {}", coreName, e); } //noinspection ThrowableResultOfMethodCallIgnored coreInitExceptions.put(coreName, e); } }); }