Un-buffered IO support in the Files class - Java File Path IO

Java examples for File Path IO:File Stream

Description

Un-buffered IO support in the Files class

Demo Code

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;

public class Main {
  public static void main(String[] args) throws IOException {
    try {//from w  w w.jav a2s .c o m
      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);
          data = in.read();
        }
      }

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

  }
}

Related Tutorials