Example usage for java.nio.file StandardOpenOption CREATE

List of usage examples for java.nio.file StandardOpenOption CREATE

Introduction

In this page you can find the example usage for java.nio.file StandardOpenOption CREATE.

Prototype

StandardOpenOption CREATE

To view the source code for java.nio.file StandardOpenOption CREATE.

Click Source Link

Document

Create a new file if it does not exist.

Usage

From source file:com.yahoo.rdl.maven.RdlExecutableFileProviderImpl.java

@Override
public Path getRdlExecutableFile() throws MojoExecutionException {
    if (configuredExecutableFile != null) {
        return configuredExecutableFile;
    }/*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:nls.formacao.matriculador.descarregador.DesCarregadorFicheiro.java

@Override
public void escrever(Registo[] info) {
    String nomeFicheiro = Utils.obtemNomeFicheiro("txt");
    for (Registo registo : info) {
        if (registo == null) {
            continue;
        }/* ww  w . j av  a  2s .  c o  m*/
        try {
            Path p = Paths.get(nomeFicheiro);
            Files.write(p, registo.prettyPrint().getBytes(Charset.forName("UTF-8")), StandardOpenOption.CREATE,
                    StandardOpenOption.APPEND);
        } catch (IOException ex) {
            LOG.error("Erro a descarregar registo para o ecr.", ex);
            System.err.println("Erro a descarregar registo para o ficheiro.");
        }
    }
    LOG.info(String.format("Informao descarregada com sucesso para o ficheiro '%s'", nomeFicheiro));
    System.out.println(String.format("Criado o ficheiro %s.", nomeFicheiro));
}

From source file:org.apache.storm.metric.FileBasedEventLogger.java

private void initLogWriter(Path logFilePath) {
    try {/* w ww  .ja va  2  s  .  c  o m*/
        LOG.info("Event log path {}", logFilePath);
        eventLogPath = logFilePath;
        eventLogWriter = Files.newBufferedWriter(eventLogPath, StandardCharsets.UTF_8,
                StandardOpenOption.CREATE, StandardOpenOption.WRITE, StandardOpenOption.APPEND);
    } catch (IOException e) {
        LOG.error("Error setting up FileBasedEventLogger.", e);
        throw new RuntimeException(e);
    }
}

From source file:org.neo4j.adversaries.pagecache.AdversarialPageCache.java

@Override
public PagedFile map(File file, int pageSize, OpenOption... openOptions) throws IOException {
    if (ArrayUtils.contains(openOptions, StandardOpenOption.CREATE)) {
        adversary.injectFailure(IOException.class, SecurityException.class);
    } else {//  w ww .j av  a 2  s.c  o m
        adversary.injectFailure(FileNotFoundException.class, IOException.class, SecurityException.class);
    }
    PagedFile pagedFile = delegate.map(file, pageSize, openOptions);
    return new AdversarialPagedFile(pagedFile, adversary);
}

From source file:org.schedulesdirect.grabber.ScheduleTask.java

static void commit(FileSystem vfs) throws IOException {
    Iterator<String> itr = FULL_SCHEDS.keySet().iterator();
    while (itr.hasNext()) {
        String id = itr.next();/*  w  ww  .j av  a  2s . c o m*/
        JSONObject sched = new JSONObject();
        List<JSONObject> airs = FULL_SCHEDS.get(id);
        Collections.sort(airs, new Comparator<JSONObject>() {
            @Override
            public int compare(JSONObject arg0, JSONObject arg1) {
                return arg0.getString("airDateTime").compareTo(arg1.getString("airDateTime"));
            }
        });
        sched.put("programs", airs);
        Path p = vfs.getPath("schedules", String.format("%s.txt", id));
        Files.write(p, sched.toString(3).getBytes(ZipEpgClient.ZIP_CHARSET), StandardOpenOption.WRITE,
                StandardOpenOption.TRUNCATE_EXISTING, StandardOpenOption.CREATE);
    }
    FULL_SCHEDS.clear();
}

From source file:hudson.util.io.RewindableFileOutputStream.java

private synchronized OutputStream current() throws IOException {
    if (current == null) {
        if (!closed) {
            FileUtils.forceMkdir(out.getParentFile());
            try {
                current = Files.newOutputStream(out.toPath(), StandardOpenOption.CREATE,
                        StandardOpenOption.TRUNCATE_EXISTING);
            } catch (FileNotFoundException | NoSuchFileException | InvalidPathException e) {
                throw new IOException("Failed to open " + out, e);
            }//from ww w.  ja va2 s.  c o m
        } else {
            throw new IOException(out.getName() + " stream is closed");
        }
    }
    return current;
}

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  . j av  a 2s  .  com*/
    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:com.spectralogic.ds3client.helpers.FileObjectPutter_Test.java

/**
 * This test cannot run on Windows without extra privileges
 *///from w w w. j  av  a  2 s  .  co m
@Test
public void testSymlink() throws IOException {
    assumeFalse(Platform.isWindows());
    final Path tempDir = Files.createTempDirectory("ds3_file_object_putter_");
    final Path tempPath = Files.createTempFile(tempDir, "temp_", ".txt");

    try {
        try (final SeekableByteChannel channel = Files.newByteChannel(tempPath, StandardOpenOption.CREATE,
                StandardOpenOption.TRUNCATE_EXISTING, StandardOpenOption.WRITE)) {
            channel.write(ByteBuffer.wrap(testData));
        }

        final Path symLinkPath = tempDir.resolve("sym_" + tempPath.getFileName().toString());
        Files.createSymbolicLink(symLinkPath, tempPath);

        getFileWithPutter(tempDir, symLinkPath);
    } finally {
        Files.deleteIfExists(tempPath);
        Files.deleteIfExists(tempDir);
    }
}

From source file:fi.johannes.kata.ocr.utils.files.ExistingFileConnection.java

public void writeNew(List<String> lines) throws IOException {
    Files.delete(p);
    Files.write(p, lines, StandardOpenOption.CREATE);
}

From source file:org.cryptomator.ui.settings.Settings.java

public static synchronized void save() {
    if (INSTANCE != null) {
        try {/*w  ww .j  a va  2s .  c o m*/
            Files.createDirectories(SETTINGS_DIR);
            final Path settingsFile = SETTINGS_DIR.resolve(SETTINGS_FILE);
            final OutputStream out = Files.newOutputStream(settingsFile, StandardOpenOption.WRITE,
                    StandardOpenOption.TRUNCATE_EXISTING, StandardOpenOption.CREATE);
            JSON_OM.writeValue(out, INSTANCE);
        } catch (IOException e) {
            LOG.error("Failed to save settings.", e);
        }
    }
}