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:Main.java

public static void main(String[] args) throws Exception {
    Path src = Paths.get("test2.txt");
    SeekableByteChannel sbc = Files.newByteChannel(src, StandardOpenOption.READ, StandardOpenOption.WRITE,
            StandardOpenOption.CREATE);
}

From source file:Main.java

public static void main(String[] args) throws IOException {
    Path path = Paths.get("/users.txt");
    byte[] contents = Files.readAllBytes(path);

    Path newPath = Paths.get("/newUsers.txt");

    Files.write(newPath, contents, StandardOpenOption.CREATE);

}

From source file:Test.java

public static void main(String[] args) throws Exception {
    AsynchronousFileChannel fileChannel = AsynchronousFileChannel.open(Paths.get("/asynchronous.txt"),
            StandardOpenOption.READ, StandardOpenOption.WRITE, StandardOpenOption.CREATE);

    Future<Integer> writeFuture1 = fileChannel.write(ByteBuffer.wrap("Sample".getBytes()), 0);
    Future<Integer> writeFuture2 = fileChannel.write(ByteBuffer.wrap("Box".getBytes()), 0);

    int result;/*from   w w  w .  j  a  v a2 s . co  m*/
    result = writeFuture1.get();
    System.out.println("Sample write completed with " + result + " bytes written");
    result = writeFuture2.get();
    System.out.println("Box write completed with " + result + " bytes written");

}

From source file:Main.java

public static void main(String[] args) throws IOException {
    Path path = Paths.get("/users.txt");
    byte[] contents = Files.readAllBytes(path);

    Path newPath = Paths.get("/newUsers.txt");
    byte[] newContents = "aaa".getBytes();
    Files.write(newPath, contents, StandardOpenOption.CREATE);
    Files.write(newPath, newContents, StandardOpenOption.APPEND);
}

From source file:Test.java

public static void main(String[] args) throws Exception {
    AsynchronousFileChannel fileChannel = AsynchronousFileChannel.open(Paths.get("/asynchronous.txt"),
            StandardOpenOption.READ, StandardOpenOption.WRITE, StandardOpenOption.CREATE);
    CompletionHandler<Integer, Object> handler = new CompletionHandler<Integer, Object>() {

        @Override//from  ww w.j  a v a 2s  .  c  om
        public void completed(Integer result, Object attachment) {
            System.out.println("Attachment: " + attachment + " " + result + " bytes written");
            System.out.println("CompletionHandler Thread ID: " + Thread.currentThread().getId());
        }

        @Override
        public void failed(Throwable e, Object attachment) {
            System.err.println("Attachment: " + attachment + " failed with:");
            e.printStackTrace();
        }
    };

    System.out.println("Main Thread ID: " + Thread.currentThread().getId());
    fileChannel.write(ByteBuffer.wrap("Sample".getBytes()), 0, "First Write", handler);
    fileChannel.write(ByteBuffer.wrap("Box".getBytes()), 0, "Second Write", handler);

}

From source file:Test.java

public static void main(String[] args) throws IOException {
    Path file = Paths.get("/home/docs/users.txt");
    Path newFile = Paths.get("/home/docs/newUsers.txt");
    try (InputStream in = Files.newInputStream(file);
            OutputStream out = Files.newOutputStream(newFile, StandardOpenOption.CREATE,
                    StandardOpenOption.APPEND)) {
        int data = in.read();
        while (data != -1) {
            out.write(data);// w w  w  .j a  v a 2 s.  c o  m
            data = in.read();
        }
    }

}

From source file:Main.java

public static void main(String[] args) throws Exception {
    Path path = Paths.get("test.txt");
    AsynchronousFileChannel afc = AsynchronousFileChannel.open(path, StandardOpenOption.WRITE,
            StandardOpenOption.CREATE);
    WriteHandler handler = new WriteHandler();
    ByteBuffer dataBuffer = getDataBuffer();
    Attachment attach = new Attachment();
    attach.asyncChannel = afc;/*from w w  w.j  av a2  s  .  co  m*/
    attach.buffer = dataBuffer;
    attach.path = path;

    afc.write(dataBuffer, 0, attach, handler);

    System.out.println("Sleeping for 5  seconds...");
    Thread.sleep(5000);
}

From source file:Main.java

public static void main(String[] args) {
    List<String> texts = new ArrayList<>();
    texts.add("test");
    texts.add("test");
    Path dest = Paths.get("twinkle.txt");
    Charset cs = Charset.forName("US-ASCII");
    try {/* w w  w. jav  a  2 s . c  o m*/
        Path p = Files.write(dest, texts, cs, StandardOpenOption.WRITE, StandardOpenOption.CREATE);
        System.out.println("Text was written to " + p.toAbsolutePath());
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    Path path = Paths.get("test.txt");

    try (AsynchronousFileChannel afc = AsynchronousFileChannel.open(path, StandardOpenOption.WRITE,
            StandardOpenOption.CREATE)) {
        ByteBuffer dataBuffer = getDataBuffer();
        Future<Integer> result = afc.write(dataBuffer, 0);
        while (!result.isDone()) {
            System.out.println("Sleeping for 2  seconds...");
            Thread.sleep(2000);/*ww w .ja  v  a 2  s .  c  o  m*/
        }
        int writtenBytes = result.get();
        System.out.format("%s bytes written  to  %s%n", writtenBytes, path.toAbsolutePath());

    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:org.roda_project.commons_ip.command_line.Main.java

public static void main(String[] args) throws ParseException, IOException {
    if (args.length == 3) {
        OutputStream outputStream = null;
        try {/* w w  w  . java 2  s .com*/
            String sipPath = args[0];
            String validationPath = args[1];
            boolean deleteExtractedSIP = Boolean.parseBoolean(args[2]);
            Path validationFile = Paths.get(validationPath);
            outputStream = Files.newOutputStream(validationFile, StandardOpenOption.CREATE,
                    StandardOpenOption.TRUNCATE_EXISTING);
            PrintWriter printWriter = new PrintWriter(outputStream);
            SIP sip = EARKSIP.parse(Paths.get(sipPath));
            printWriter.print(sip.getValidationReport().toHtml());
            printWriter.flush();
            printWriter.close();
            if (deleteExtractedSIP) {
                Utils.deletePath(sip.getBasePath());
            }
        } finally {
            IOUtils.closeQuietly(outputStream);
        }
    } else {
        // no arguments provided
    }
}