Java OutputStream write to text file using nio

Description

Java OutputStream write to text file using nio

import java.io.BufferedOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.file.Files;
import java.nio.file.InvalidPathException;
import java.nio.file.Paths;

public class Main {
   public static void main(String args[]) {
      // Open the file and obtain a stream linked to it.
      try (OutputStream fout = new BufferedOutputStream(Files.newOutputStream(Paths.get("test.txt")))) {
         // Write some bytes to the stream.
         for (int i = 0; i < 26; i++)
            fout.write((byte) ('A' + i));

      } catch (InvalidPathException e) {
         System.out.println("Path Error " + e);
      } catch (IOException e) {
         System.out.println("I/O Error: " + e);
      }//from   w w w  . j  a  v a 2  s .  c  o  m
   }
}



PreviousNext

Related