Java Streams

INPUT, OUTPUT AND ERROR STREAMS


INPUT

System.in is an InputStream which is typically connected to keyboard input of console programs. In other words, if you start a Java application from the command line, and you type something on the keyboard while the CLI console (or terminal) has focus, the keyboard input can typically be read via System.in from inside that Java application. However, it is only keyboard input directed to that Java application (the console / terminnal that started the application) which can be read via System.in. Keyboard input for other applications cannot be read via System.in .

System.in is not used as often since data is commonly passed to a command line Java application via command line arguments, files, or possibly via network connections if the application is designed for that. In applications with GUI the input to the application is given via the GUI. This is a separate input mechanism from System.in.


  
import java.io.*;
public class NewClass
{
    public static void main(String[] args) throws FileNotFoundException, IOException
    {
  
        // initializing FileInputStream
        FileInputStream geek = new FileInputStream("ABC.txt");
  
        // Initializing InputStreamReader object
        InputStreamReader in_strm = new InputStreamReader(geek);
  
        int t;
        String read_reslt="";
  
        // Use of read() method
        while((t = in_strm.read()) != -1)
        {
            read_reslt = read_reslt+(char)t;
        }
      
        // print the result read from the file
        System.out.println(read_reslt);
    }
}

             

                                                                OUTPUT

System.out is a PrintStream to which you can write characters. System.out normally outputs the data you write to it to the CLI console / terminal. System.out is often used from console-only programs like command line tools as a way to display the result of their execution to the user. This is also often used to print debug statements of from a program (though it may arguably not be the best way to get debug info out of a program).

import java.io.*;
//Java program to demonstrate OutputStream
class OutputStreamDemo
{
    public static void main(String args[])throws Exception
    {
        OutputStream os = new FileOutputStream("file.txt");
        byte b[] = {65, 66, 67, 68, 69, 70};
          
        //illustrating write(byte[] b) method
        os.write(b);
          
        //illustrating flush() method
        os.flush();
  
        //illustrating write(int b) method
        for (int i = 71; i <75 ; i++) 
        {
            os.write(i);
        }
          
        os.flush();
          
        //close the stream
        os.close();
    }
}



                                                                 ERROR
System.err is a PrintStreamSystem.err works like System.out except it is normally only used to output error texts. Some programs (like Eclipse) will show the output to System.err in red text, to make it more obvious that it is error text.

try {
  InputStream input = new FileInputStream("c:\\data\\...");
  System.out.println("File opened...");

} catch (IOException e){
  System.err.println("File opening failed:");
  e.printStackTrace();
}

Comments

Popular posts from this blog

Solve the Sudoku Python

Solve the Sudoku Java

Find Duplicates Java