Example usage for java.nio.file StandardOpenOption WRITE

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

Introduction

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

Prototype

StandardOpenOption WRITE

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

Click Source Link

Document

Open for write access.

Usage

From source file:org.cryptomator.webdav.jackrabbit.EncryptedDir.java

private void addMemberFile(DavResource resource, InputContext inputContext) throws DavException {
    final Path childPath = ResourcePathUtils.getPhysicalPath(resource);
    try (final SeekableByteChannel channel = Files.newByteChannel(childPath, StandardOpenOption.WRITE,
            StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING)) {
        cryptor.encryptFile(inputContext.getInputStream(), channel);
    } catch (SecurityException e) {
        throw new DavException(DavServletResponse.SC_FORBIDDEN, e);
    } catch (IOException e) {
        LOG.error("Failed to create file.", e);
        throw new IORuntimeException(e);
    } catch (CounterOverflowException e) {
        // lets indicate this to the client as a "file too big" error
        throw new DavException(DavServletResponse.SC_INSUFFICIENT_SPACE_ON_RESOURCE, e);
    } catch (EncryptFailedException e) {
        LOG.error("Encryption failed for unknown reasons.", e);
        throw new IllegalStateException("Encryption failed for unknown reasons.", e);
    } finally {/*from   www.  j av  a 2  s  . co m*/
        IOUtils.closeQuietly(inputContext.getInputStream());
    }
}

From source file:eu.freme.bpt.service.FailurePolicyTest.java

private File createDir() throws IOException {
    File dir = Files.createTempDirectory("testDir").toFile();
    try {/*from  ww  w . ja va 2s.  com*/
        FileUtils.forceDeleteOnExit(dir);
    } catch (IOException e) {
        e.printStackTrace();
    }
    // now create some files in it
    File file1 = new File(dir, "file1");
    File file2 = new File(dir, "file2");
    Files.write(file1.toPath(), "Hello".getBytes(StandardCharsets.UTF_8), StandardOpenOption.CREATE,
            StandardOpenOption.WRITE);
    Files.write(file2.toPath(), "World".getBytes(StandardCharsets.UTF_8), StandardOpenOption.CREATE,
            StandardOpenOption.WRITE);
    return dir;
}

From source file:record.wave.WaveWriter.java

/**
 * Opens the file and writes a wave header.
 *///w  w  w.j  a v a2s.  co  m
private void open() throws IOException {
    int version = 2;

    while (Files.exists(mFile)) {
        mFile = Paths.get(mFile.toFile().getAbsolutePath().replace(".wav", "_" + version + ".wav"));
        version++;
    }

    mFileChannel = (FileChannel.open(mFile, StandardOpenOption.WRITE, StandardOpenOption.CREATE_NEW));

    ByteBuffer header = WaveUtils.getWaveHeader(mAudioFormat);

    header.flip();

    while (header.hasRemaining()) {
        mFileChannel.write(header);
    }
}

From source file:org.wso2.carbon.user.mgt.recorder.DefaultUserDeletionEventRecorder.java

private void writeToCustomFile(String path, String content) throws RecorderException {

    // Create the file if it does not exist. Open with write permission and append to the end.
    try (OutputStream outputStream = Files.newOutputStream(Paths.get(path), StandardOpenOption.CREATE,
            StandardOpenOption.WRITE, StandardOpenOption.APPEND)) {
        BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream));
        bufferedWriter.write(content);//from  www .  j a va  2  s.  com
        bufferedWriter.newLine();
        bufferedWriter.flush();
    } catch (IOException e) {
        throw new RecorderException("Error while writing content to the file.", e);
    }
}

From source file:org.wikidata.wdtk.util.DirectoryManagerImpl.java

@Override
public long createFile(String fileName, InputStream inputStream) throws IOException {
    long fileSize;
    Path filePath = this.directory.resolve(fileName);
    try (ReadableByteChannel readableByteChannel = Channels.newChannel(inputStream);
            FileChannel fc = FileChannel.open(filePath, StandardOpenOption.WRITE,
                    StandardOpenOption.CREATE_NEW)) {
        fileSize = fc.transferFrom(readableByteChannel, 0, Long.MAX_VALUE);
    }/*  w ww.  ja  v a 2s  .c  o  m*/
    return fileSize;
}

From source file:org.cloudfoundry.dependency.resource.InAction.java

private void writeVersion() {
    try {//from   w w  w  .jav a2 s . c o m
        String version = new VersionHolder(this.request.getVersion().getRef()).toRepositoryVersion();
        Path versionFile = Files.createDirectories(this.destination).resolve("version");
        Files.write(versionFile, Collections.singletonList(version), StandardOpenOption.CREATE,
                StandardOpenOption.WRITE);
    } catch (IOException e) {
        throw Exceptions.propagate(e);
    }
}

From source file:jp.toastkid.script.Controller.java

/**
 * Save script to file.// w ww  .  j  a v a  2  s  .c  o  m
 */
@FXML
private void saveScript() {
    try {
        if (new File(scriptName.getText()) != null) {
            final File file = new File(
                    "script" + Language.extension(scriptLanguage.getSelectionModel().getSelectedItem()));
            Files.createFile(file.toPath());
            scriptName.setText(file.getAbsolutePath());
        }
        final File file = new File(scriptName.getText());
        Files.write(file.toPath(), scripterInput.getText().getBytes("UTF-8"), StandardOpenOption.WRITE);
    } catch (final IOException e) {
        LOGGER.error("Caught error.", e);
    }
}

From source file:com.spectralogic.ds3client.helpers.FileObjectPutter_Test.java

@Test
public void testRelativeSymlink() throws IOException, URISyntaxException {
    assumeFalse(Platform.isWindows());//from   w  w w .  j av a  2s .co m
    final Path tempDir = Files.createTempDirectory("ds3_file_object_rel_test_");
    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());
        final Path relPath = Paths.get("..", getParentDir(tempPath), tempPath.getFileName().toString());

        LOG.info("Creating symlink from " + symLinkPath.toString() + " to " + relPath.toString());

        Files.createSymbolicLink(symLinkPath, relPath);
        getFileWithPutter(tempDir, symLinkPath);

    } finally {
        Files.deleteIfExists(tempPath);
        Files.deleteIfExists(tempDir);
    }
}

From source file:org.cryptomator.frontend.webdav.servlet.DavFolder.java

private void addMemberFile(DavFile memberFile, InputStream inputStream) {
    try (ReadableByteChannel src = Channels.newChannel(inputStream); //
            WritableByteChannel dst = Files.newByteChannel(memberFile.path, StandardOpenOption.CREATE,
                    StandardOpenOption.TRUNCATE_EXISTING, StandardOpenOption.WRITE)) {
        ByteStreams.copy(src, dst);//w  ww.j  av a2s  .  c o m
    } catch (IOException e) {
        throw new UncheckedIOException(e);
    }
}

From source file:divconq.util.IOUtil.java

public static boolean saveEntireFile2(Path dest, Memory content) {
    try {//from w w w  . j  a va  2 s  .c om
        Files.createDirectories(dest.getParent());
        Files.write(dest, content.toArray(), StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING,
                StandardOpenOption.WRITE, StandardOpenOption.SYNC);
    } catch (Exception x) {
        return false;
    }

    return true;
}