0% found this document useful (0 votes)
6 views4 pages

Java I

Java I/O is a set of classes in the java.io package for reading and writing data from various sources and destinations. It includes default streams like System.in for input, System.out for output, and System.err for error messages. The document also describes methods associated with these streams, such as reading bytes from System.in and printing to the console using System.out methods like print() and println().

Uploaded by

doscse
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views4 pages

Java I

Java I/O is a set of classes in the java.io package for reading and writing data from various sources and destinations. It includes default streams like System.in for input, System.out for output, and System.err for error messages. The document also describes methods associated with these streams, such as reading bytes from System.in and printing to the console using System.out methods like print() and println().

Uploaded by

doscse
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Java I/O (Input/Output) is a collection of classes and streams in the java.

io
package that handle reading data from sources (like files, keyboard, or
network) and writing data to destinations (like files, console or sockets). It
provides both byte and character streams to support all types of data.

Default/Standard Streams in Java


Before exploring various input and output streams, let's look at 3 most
commonly used default streams that Java has provided:

 [Link]: This is the standard input stream that is used to read


characters from the keyboard or any other standard input device.
 [Link]: This is the standard output stream that is used to produce the
result of a program on an output device like the computer screen.
 [Link]: This is the standard error stream that is used to display error
messages separately from normal output.

Functions used with Default Streams:

1. [Link]

[Link] is an InputStream, so it provides methods from the InputStream


class. Commonly used ones are:
 int read(): reads one byte of data.
 int read(byte[] b): reads bytes into an array.
 int read(byte[] b, int off, int len): reads bytes into part of an array.
 void close(): closes the input stream.
 int available(): returns the number of bytes that can be read without
blocking.
Usually, [Link] is wrapped with classes like Scanner or BufferedReader for
easier input handling.
import [Link];

public class SystemInExample {


public static void main(String[] args) throws IOException {
[Link]("Enter a character:");

// Reads a single byte from [Link]


int data = [Link]();

// Print the character and its ASCII value


[Link]("You entered: " + (char) data);
[Link]("ASCII Value: " + data);
}
}
Output:
Enter a character:
a
You entered: a
ASCII Value: 97

=== Code Execution Successful ===

2. [Link]

print(): This Java method displays text on the console, keeping the cursor at
the end of the printed text so the next output continues from the same line.
Syntax:
[Link](parameter);
Example:
import [Link].*;

class Geeks {
public static void main(String[] args)
{
// using print() all are printed in the same line
[Link]("GfG! ");
[Link]("GfG! ");
[Link]("GfG! ");
}
}

GfG! GfG! GfG!


=== Code Execution Successful ===
println(): This method in Java is also used to display a text on the console. It
prints the text on the console and the cursor moves to the start of the next line
at the console. The next printing takes place from the next line.
Syntax:
[Link](parameter);
Example:
import [Link].*;

class Test {
public static void main(String[] args)
{
// using println() all are printed in the different line
[Link]( "Welcome ");
[Link]("to ");
[Link]("First Year ");
}
}

You might also like