How to write the contents of a file to standard output?

This tutorial describes how to write the contents of a file to standard output.

Example for Write a File Contents To Standard Output:

package com.nourit.javaIO;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

public class WriteFileContentsToStandardOutputExample {

  public static void main(String[] args) {
    File file = new File("D:/tmp/nourit.txt");
    FileInputStream fileInputStream;
    try {
      fileInputStream = new FileInputStream(file);

      int oneByte;
      while ((oneByte = fileInputStream.read()) != -1) {
        System.out.print((char)oneByte); 
        // could also do this
        //System.out.write(oneByte);
      }
      System.out.flush();
    } catch (IOException e) {
      e.printStackTrace();
    }
  }
}

Output:

This is the content of nourit-test to write into file

 

 

NOUR-IT

IT Training Center for Java and Web technology. If you enjoy and like our tutorials and believe in our mission, Please Subscribe us on Youtube and follow us on Twitter, or befriend us on Facebook or Google Plus.

Schreibe einen Kommentar

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert.

*