Java InputStream read text file with nio

Description

Java InputStream read text file with nio

import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.InvalidPathException;
import java.nio.file.Paths;

public class Main {
   public static void main(String args[]) {
      int i;//from w  w  w . java 2  s.co m

      // Open the file and obtain a stream linked to it.
      try (InputStream fin = Files.newInputStream(Paths.get("Main.java"))) {
         do {
            i = fin.read();
            if (i != -1)
               System.out.print((char) i);
         } while (i != -1);

      } catch (InvalidPathException e) {
         System.out.println("Path Error " + e);
      } catch (IOException e) {
         System.out.println("I/O Error " + e);
      }
   }
}



PreviousNext

Related