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.
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).
OutputStream os = newFileOutputStream("file.txt");
byteb[] = {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(inti = 71; i <75; i++)
{
os.write(i);
}
os.flush();
//close the stream
os.close();
}
}
ERROR
System.err is a PrintStream. System.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.
PROGRAM TO FIND REPEATING NUMBERS IN AN ARRAY class MAIN { public static void main(String args[]) { int numRay[] = { 0 , 4 , 3 , 2 , 7 , 8 , 2 , 3 , 1 }; for ( int i = 0 ; i < numRay.length; i++) { numRay[numRay[i] % numRay.length] = numRay[numRay[i] % numRay.length] + numRay.length; } System.out.println( "The repeating elements are : " ); for ( int i = 0 ; i < numRay.length; i++) { if (numRay[i] >= numRay.length* 2 ) { ...
PROGRAM TO SOLVE SUDOKU class GFG { public static boolean isSafe( int [][] board, int row, int col, int num) { // Row has the unique (row-clash) for ( int d = 0 ; d < board.length; d++) { // Check if the number we are trying to ...
PROGRAM TO PRINT MAXIMUM NUMBER OF A'S USING GIVEN FOUR KEYS import java.io.*; class GFG { // A recursive function that returns // the optimal length string for N keystrokes static int findoptimal( int N) { // The optimal string length is N // when N is smaller than 7 if (N <= 6 ) return N; // Initialize result int max = 0 ; // TRY ALL POSSIBLE BREAK-POINTS // For any keystroke N, we need to ...
Comments
Post a Comment