Java I/O, which stands for Input and Output, is a fundamental part of Java programming used for handling data input and producing output in various applications.
Java leverages the concept of streams to optimize I/O operations, and the java.io package encompasses all the necessary classes for handling input and output tasks efficiently.
File handling in Java can be achieved through the Java I/O API.
In Java, a stream is a sequence of elements that you can process in a functional and declarative manner. Java Streams were introduced in Java 8 and provide a powerful way to work with collections, arrays, and other data sources. Streams allow you to perform operations on the elements of a sequence, such as filtering, mapping, and reducing, without the need for explicit loops.
In Java, three streams are automatically created for us, all of which are associated with the console:
1) `System.in`:This represents the standard input stream, which serves the purpose of reading characters from the keyboard or any other standard input source.
2)`System.out`: This refers to the standard output stream, utilized for displaying the program's results on an output device, such as the computer screen. Here is a list of various print functions used for outputting statements
'print()': In Java, this method is employed to exhibit text on the console. The text is supplied as a parameter in the form of a string. When invoked, this method displays the text on the console while keeping the cursor at the end of the printed text. Subsequent printing operations will begin from this point.
Syntax:
System.out.print(parameter);
// Java code to illustrate print()
import java.io.*;
class Demo {
public static void main(String[] args)
{
// using print()
// all are printed in the
// same line
System.out.print("CTI WORLD ");
System.out.print("CTI WORLD ");
System.out.print("CTI WORLD ");
}
}
Output:
CTI WORLD CTI WORLD CTI WORLD
println(): In Java, this method serves the purpose of showcasing text on the console. It outputs the text to the console and positions the cursor at the beginning of the next line. Subsequent printing operations will begin from the next line
Syntex:-
System.out.println(parameter);
// Java code to illustrate println()
import java.io.*;
class Demo {
public static void main(String[] args)
{
// using println()
// all are printed in the
// different line
System.out.println("CTI WORLD ");
System.out.println("CTI WORLD ");
System.out.println("CTI WORLD ");
}
}
Output:
CTI WORLD
CTI WORLD
CTI WORLD
printf(): This method in Java is reminiscent of the printf function in C. It is worth noting that while System.out.print() and System.out.println() accept a single argument, printf() can accept multiple arguments. Its primary purpose is to format the output in Java.
public class PrintfExample {
public static void main(String[] args) {
String name = "Abhay";
int age = 24;
double salary = 50000.75;
// Using printf to format output
System.out.printf("Name: %s%n", name);
System.out.printf("Age: %d%n", age);
System.out.printf("Salary: $%.2f%n", salary);
}
}
Output:-
Name: Abhay
Age: 34
tSalary: 50000.75
In this example:
%s is a placeholder for a string (name).
%d is a placeholder for an integer (age).
%.2f is a placeholder for a floating-point number (salary) with 2 decimal places.
The %n is used to insert a platform-independent line separator.
Types of Streams :-
Streams can be categorized into two main classes based on the type of operations they are used for
1.Input Stream: These streams are employed to retrieve data as input from various sources such as arrays, files, or peripheral devices. Examples include FileInputStream, BufferedInputStream, and ByteArrayInputStream
2.Output Stream: These streams are utilized to write data as output to various destinations such as arrays, files, or output peripheral devices. Examples include FileOutputStream, BufferedOutputStream, and ByteArrayOutputStream