This blog teaches you how to perform input/output operations in Java using streams, readers and writers. You will learn the basics of Java input/output, the differences between byte streams and character streams, buffered and unbuffered streams, standard streams and console, and how to use various readers and writers to read and write data from and to different sources and destinations. You will also learn how to use streams and filters to manipulate data in different formats and objects.
1. Introduction
In this tutorial, you will learn how to perform input/output operations in Java using streams, readers and writers. Input/output operations are essential for any program that needs to interact with external sources or destinations of data, such as files, network sockets, databases, or user input. Java provides a rich set of classes and interfaces to handle different types of input/output operations in a consistent and efficient way.
Some of the key concepts and topics that you will learn in this tutorial are:
- What are Java streams and how they enable input/output operations in Java.
- What are the differences between byte streams and character streams, and how to choose the appropriate one for your input/output needs.
- What are the advantages and disadvantages of using buffered and unbuffered streams, and how to use them effectively.
- How to use the standard streams and the console class to perform input/output operations with the standard input, output, and error devices.
- What are Java readers and writers, and how they provide a convenient and high-level way to read and write text data.
- How to use various readers and writers to read and write data from and to different sources and destinations, such as files, strings, arrays, or network sockets.
- What are Java streams and filters, and how they allow you to manipulate data in different formats and objects.
- How to use data input/output streams and object input/output streams to read and write primitive data types and objects.
- How to use print streams and print writers to format and print data in a human-readable way.
By the end of this tutorial, you will have a solid understanding of the Java input/output system and how to use it effectively in your programs. You will also be able to apply the skills and knowledge you learned to solve various input/output problems in Java.
Are you ready to dive into the world of Java input/output? Let’s get started!
2. Java Input/Output Basics
Before we dive into the details of Java streams, readers and writers, let’s first understand some basic concepts and terminology related to Java input/output. In this section, you will learn:
- What is a stream and how it enables input/output operations in Java.
- What are the differences between input streams and output streams, and how to use them to read and write data.
- What are the differences between byte streams and character streams, and how to choose the appropriate one for your input/output needs.
- What are the advantages and disadvantages of using buffered and unbuffered streams, and how to use them effectively.
- How to use the standard streams and the console class to perform input/output operations with the standard input, output, and error devices.
A stream is an abstract representation of a sequence of data that can be read from or written to a source or destination. A stream acts as a bridge between your program and the external world, allowing you to exchange data with different devices, files, network sockets, databases, or other programs. A stream can be either input or output, depending on the direction of data flow. An input stream allows you to read data from a source, while an output stream allows you to write data to a destination.
For example, suppose you want to read a text file from your disk and display its contents on the screen. You can use an input stream to read the data from the file, and an output stream to write the data to the standard output device (usually the console or the terminal).
Java provides a rich set of classes and interfaces to handle different types of streams. The most basic and common ones are the byte streams and the character streams. Byte streams deal with raw binary data, such as bytes, while character streams deal with text data, such as characters, strings, or Unicode. Byte streams are useful for reading and writing binary data, such as images, audio, or video files, while character streams are useful for reading and writing text data, such as text files, HTML, or XML documents.
Byte streams and character streams are organized in a hierarchy of classes and interfaces.
The root of the hierarchy is the java.io.InputStream
and java.io.OutputStream
classes, which are the abstract base classes for all byte input and output streams. The java.io.Reader
and java.io.Writer
classes are the abstract base classes for all character input and output streams. These classes provide the basic methods for reading and writing data, such as read()
, write()
, close()
, and flush()
.
The subclasses of these classes provide more specific functionality and features for different types of sources and destinations, such as files, arrays, strings, network sockets, or pipes. For example, the java.io.FileInputStream
and java.io.FileOutputStream
classes are byte streams that read and write data from and to files. The java.io.FileReader
and java.io.FileWriter
classes are character streams that read and write text data from and to files. The java.io.DataInputStream
and java.io.DataOutputStream
classes are byte streams that read and write primitive data types and strings in a binary format. The java.io.ObjectInputStream
and java.io.ObjectOutputStream
classes are byte streams that read and write objects in a serialized format. The java.io.PrintStream
and java.io.PrintWriter
classes are character streams that format and print data in a human-readable way.
One of the important features of Java streams is that they can be buffered or unbuffered. A buffered stream uses an internal buffer to store data temporarily, while an unbuffered stream does not. A buffered stream can improve the performance and efficiency of input/output operations, as it reduces the number of calls to the underlying source or destination. For example, a buffered input stream can read a large chunk of data from a file at once, and store it in the buffer, instead of reading one byte at a time. A buffered output stream can write a large chunk of data to a file at once, and flush it to the destination, instead of writing one byte at a time.
Java provides the java.io.BufferedInputStream
and java.io.BufferedOutputStream
classes for buffering byte streams, and the java.io.BufferedReader
and java.io.BufferedWriter
classes for buffering character streams. These classes are called wrapper or decorator classes, as they wrap around another stream and add buffering functionality to it. For example, you can create a buffered input stream by passing a file input stream to the constructor of the buffered input stream class, as shown below:
// create a file input stream to read data from a file FileInputStream fis = new FileInputStream("input.txt"); // create a buffered input stream to wrap around the file input stream BufferedInputStream bis = new BufferedInputStream(fis);
Similarly, you can create a buffered output stream by passing a file output stream to the constructor of the buffered output stream class, as shown below:
// create a file output stream to write data to a file FileOutputStream fos = new FileOutputStream("output.txt"); // create a buffered output stream to wrap around the file output stream BufferedOutputStream bos = new BufferedOutputStream(fos);
The advantage of using buffered streams is that they can improve the performance and efficiency of input/output operations, as they reduce the number of calls to the underlying source or destination. The disadvantage of using buffered streams is that they can introduce some overhead and complexity, as they require additional memory and management. Also, buffered streams may not be suitable for some scenarios, such as interactive or real-time input/output, where you need to read or write data immediately, without any delay or buffering.
Another important feature of Java input/output is the use of the standard streams and the console class. The standard streams are three predefined streams that are automatically available for your program to perform input/output operations with the standard input, output, and error devices. The standard input device is usually the keyboard, the standard output device is usually the screen, and the standard error device is usually the screen or a log file. The standard streams are:
System.in
: a byte input stream that reads data from the standard input device.System.out
: a byte output stream that writes data to the standard output device.System.err
: a byte output stream that writes data to the standard error device.
You can use the standard streams to perform input/output operations with the keyboard and the screen, such as reading user input or printing messages. For example, you can use the System.out
stream to print a message to the screen, as shown below:
// print a message to the standard output device System.out.println("Hello, world!");
You can also use the System.in
stream to read user input from the keyboard, but you need to wrap it with another stream, such as a buffered input stream or a scanner, to make it easier and more convenient. For example, you can use the Scanner
class to read user input from the System.in
stream, as shown below:
// create a scanner to read user input from the standard input device Scanner sc = new Scanner(System.in); // prompt the user to enter their name System.out.print("Enter your name: "); // read the user input as a string String name = sc.nextLine(); // print a greeting message to the user System.out.println("Hello, " + name + "!");
The Console
class is another way to perform input/output operations with the standard input and output devices. The Console
class provides methods for reading and writing text data, such as
2.1. Byte Streams and Character Streams
In this section, you will learn the differences between byte streams and character streams, and how to choose the appropriate one for your input/output needs. Byte streams and character streams are two types of streams that deal with different kinds of data: binary data and text data. Binary data is raw data that consists of bytes, such as images, audio, or video files. Text data is data that consists of characters, strings, or Unicode, such as text files, HTML, or XML documents.
Byte streams are streams that read and write data as bytes, one byte at a time. Byte streams are useful for reading and writing binary data, such as images, audio, or video files. Byte streams are also the lowest level of streams, meaning that they are the closest to the source or destination of data. Byte streams do not perform any encoding or decoding of data, meaning that they do not convert bytes to characters or vice versa. Byte streams are represented by the java.io.InputStream
and java.io.OutputStream
classes and their subclasses, such as java.io.FileInputStream
and java.io.FileOutputStream
.
Character streams are streams that read and write data as characters, one character at a time. Character streams are useful for reading and writing text data, such as text files, HTML, or XML documents. Character streams are also the highest level of streams, meaning that they are the furthest from the source or destination of data. Character streams perform encoding and decoding of data, meaning that they convert bytes to characters or vice versa, according to a specified character set. Character streams are represented by the java.io.Reader
and java.io.Writer
classes and their subclasses, such as java.io.FileReader
and java.io.FileWriter
.
The following table summarizes the main differences between byte streams and character streams:
Byte Streams | Character Streams |
---|---|
Read and write data as bytes | Read and write data as characters |
Useful for binary data | Useful for text data |
Lowest level of streams | Highest level of streams |
Do not perform encoding or decoding | Perform encoding and decoding |
Represented by InputStream and OutputStream classes | Represented by Reader and Writer classes |
So, how do you choose between byte streams and character streams for your input/output needs? The answer depends on the type of data that you want to read or write. If you want to read or write binary data, such as images, audio, or video files, you should use byte streams. If you want to read or write text data, such as text files, HTML, or XML documents, you should use character streams. However, there are some exceptions and special cases, such as when you want to read or write text data in a binary format, or when you want to read or write binary data in a text format. In those cases, you may need to use a combination of byte streams and character streams, or use some special classes or methods that can handle those scenarios.
For example, suppose you want to read or write a text file that contains numbers in a binary format, such as 00000001
for 1, 00000010
for 2, and so on. In this case, you cannot use a character stream, as it will read or write the numbers as characters, not as bytes. You also cannot use a byte stream, as it will read or write the numbers as bytes, not as characters. You need to use a special class or method that can convert the numbers from binary to decimal, or vice versa, such as the Integer.parseInt()
or Integer.toBinaryString()
methods. Alternatively, you can use a data input/output stream, which is a type of byte stream that can read and write primitive data types and strings in a binary format, as we will see in the next section.
Another example is when you want to read or write an image file that contains text data, such as a JPEG file that has some text embedded in it. In this case, you cannot use a byte stream, as it will read or write the image data as bytes, not as characters. You also cannot use a character stream, as it will read or write the text data as characters, not as bytes. You need to use a special class or method that can extract the text data from the image data, or vice versa, such as the javax.imageio.ImageIO
class, which can read and write images in various formats.
In summary, byte streams and character streams are two types of streams that deal with different kinds of data: binary data and text data. You should choose the appropriate one for your input/output needs, depending on the type of data that you want to read or write. However, there are some exceptions and special cases, where you may need to use a combination of byte streams and character streams, or use some special classes or methods that can handle those scenarios.
2.2. Buffered and Unbuffered Streams
In this section, you will learn the advantages and disadvantages of using buffered and unbuffered streams, and how to use them effectively. Buffered and unbuffered streams are two types of streams that differ in the way they store and transfer data. A buffered stream uses an internal buffer to store data temporarily, while an unbuffered stream does not. A buffered stream can improve the performance and efficiency of input/output operations, as it reduces the number of calls to the underlying source or destination. An unbuffered stream can provide more immediate and direct access to the data, without any delay or buffering.
Some of the key concepts and topics that you will learn in this section are:
- What is a buffer and how it works.
- What are the benefits and drawbacks of using buffered streams, and how to use them correctly.
- What are the benefits and drawbacks of using unbuffered streams, and how to use them correctly.
- How to choose between buffered and unbuffered streams, depending on your input/output needs and scenarios.
A buffer is a temporary storage area that holds data before it is transferred to or from a source or destination. A buffer can be either an array of bytes or an array of characters, depending on the type of stream. A buffer can have a fixed or variable size, depending on the implementation of the stream. A buffer can also be either full or empty, depending on the amount of data that it contains.
A buffered stream uses a buffer to store data temporarily, while an unbuffered stream does not. A buffered stream can read or write data in larger chunks, instead of one byte or character at a time. A buffered stream can also reduce the number of calls to the underlying source or destination, as it can read or write data from or to the buffer, instead of directly from or to the source or destination. A buffered stream can improve the performance and efficiency of input/output operations, as it can reduce the overhead and latency of accessing the source or destination.
However, a buffered stream also has some disadvantages and limitations. A buffered stream can introduce some overhead and complexity, as it requires additional memory and management. A buffered stream can also cause some inconsistency and confusion, as it can create a discrepancy between the data in the buffer and the data in the source or destination. A buffered stream can also cause some delay and buffering, as it can postpone the actual transfer of data until the buffer is full or flushed.
Java provides the java.io.BufferedInputStream
and java.io.BufferedOutputStream
classes for buffering byte streams, and the java.io.BufferedReader
and java.io.BufferedWriter
classes for buffering character streams. These classes are called wrapper or decorator classes, as they wrap around another stream and add buffering functionality to it. You can create a buffered stream by passing another stream to the constructor of the buffered stream class, as shown in the previous section.
When you use a buffered stream, you need to follow some rules and best practices to ensure the correct and efficient transfer of data. Some of these rules and best practices are:
- Always close the buffered stream when you are done with it, to release the resources and flush the buffer. You can use the
close()
method of the buffered stream class to close the stream and the underlying stream. - Always flush the buffered output stream before you read from the same source or destination, to avoid inconsistency and confusion. You can use the
flush()
method of the buffered output stream class to flush the buffer and write the data to the destination. - Always check the availability of data before you read from a buffered input stream, to avoid blocking and waiting. You can use the
available()
method of the buffered input stream class to check how many bytes are available in the buffer and the source. - Always use the appropriate buffer size for your input/output needs and scenarios, to optimize the performance and efficiency of input/output operations. You can use the constructor of the buffered stream class that takes an int parameter to specify the buffer size in bytes or characters. The default buffer size is usually 8192 bytes or characters, but you can adjust it according to your needs.
An unbuffered stream does not use a buffer to store data temporarily, but transfers data directly to or from the source or destination. An unbuffered stream can read or write data one byte or character at a time, without any delay or buffering. An unbuffered stream can provide more immediate and direct access to the data, without any overhead or complexity.
However, an unbuffered stream also has some disadvantages and limitations. An unbuffered stream can read or write data in smaller chunks, instead of larger chunks. An unbuffered stream can also increase the number of calls to the underlying source or destination, as it can read or write data directly from or to the source or destination. An unbuffered stream can reduce the performance and efficiency of input/output operations, as it can increase the overhead and latency of accessing the source or destination.
Java provides the java.io.InputStream
and java.io.OutputStream
classes and their subclasses for unbuffered byte streams, and the java.io.Reader
and java.io.Writer
classes and their subclasses for unbuffered character streams. These classes provide the basic methods for reading and writing data, such as read()
, write()
, close()
, and flush()
.
When you use an unbuffered stream, you need to follow some rules and best practices to ensure the correct and efficient transfer of data. Some of these rules and best practices are:
- Always close the unbuffered stream when you are done with it, to release the resources and flush the stream. You can use the
close()
method of the unbuffered stream class to close the stream. - Always flush the unbuffered output stream before you read from the same source or destination, to avoid inconsistency and confusion. You can use the
flush()
method of the unbuffered output stream class to flush the stream and write the data to the destination. - Always check the availability of data before you read from an unbuffered input stream, to avoid blocking and waiting. You can use the
available()
method of the unbuffered input stream class to check how many bytes are available in the source. - Always use the appropriate stream type for your input/output needs and scenarios, to optimize the performance and efficiency of input/output operations. You can use the subclasses of the unbuffered stream classes that provide more specific functionality and features for different types of sources and destinations, such as files, arrays, strings, network sockets, or pipes.
So, how do you choose between buffered and unbuffered streams for your input/output needs and scenarios? The answer depends on the trade-off between performance and immediacy. If you want to improve the performance and efficiency of input/output operations, you should use buffered streams. If you want to provide more immediate and direct access to the data, you should use unbuffered streams. However, there are some exceptions and special cases, such as when you want to perform interactive or real-time input/output, where you need to read or write data immediately, without any delay or buffering. In those cases, you may need to use unbuffered streams, or use some special classes or methods that can handle those scenarios.
In summary, buffered and unbuffered streams are two types of streams that differ in the way they store and transfer data. A buffered stream uses an internal buffer to store data temporarily, while an unbuffered stream does not. A buffered stream can improve the performance and efficiency of input/output operations, as it reduces the number of calls to the underlying source or destination. An unbuffered stream can provide more immediate and direct access to the data, without any overhead or complexity. You should choose the appropriate one for your input/output needs and scenarios, depending on the trade-off between performance and immediacy. However, there are some exceptions and special cases, where you may need to use a combination of buffered and unbuffered streams, or use some special classes or methods that can handle those scenarios.
2.3. Standard Streams and Console
In this section, you will learn how to use the standard streams and the console class to perform input/output operations with the standard input, output, and error devices. The standard streams are three predefined streams that are automatically available for your program to exchange data with the keyboard, the screen, or a log file. The console class is another way to interact with the keyboard and the screen, providing methods for reading and writing text data, such as passwords, formatted strings, or arrays of characters.
Some of the key concepts and topics that you will learn in this section are:
- What are the standard streams and how to use them to read and write data from and to the standard input, output, and error devices.
- How to use the System class to access and manipulate the standard streams, such as changing their source or destination, or flushing their buffer.
- How to use the Scanner class to read user input from the standard input stream, such as strings, numbers, or booleans.
- How to use the PrintStream and PrintWriter classes to write formatted data to the standard output and error streams, such as strings, numbers, or objects.
- What is the console class and how to use it to read and write text data from and to the standard input and output devices.
- How to use the readPassword method to read a password from the user without echoing it to the screen.
- How to use the format and printf methods to write formatted data to the console, such as strings, numbers, or objects.
The standard streams are three predefined streams that are automatically available for your program to perform input/output operations with the standard input, output, and error devices. The standard input device is usually the keyboard, the standard output device is usually the screen, and the standard error device is usually the screen or a log file. The standard streams are:
System.in
: a byte input stream that reads data from the standard input device.System.out
: a byte output stream that writes data to the standard output device.System.err
: a byte output stream that writes data to the standard error device.
You can use the standard streams to perform input/output operations with the keyboard and the screen, such as reading user input or printing messages. For example, you can use the System.out
stream to print a message to the screen, as shown below:
// print a message to the standard output device System.out.println("Hello, world!");
You can also use the System.in
stream to read user input from the keyboard, but you need to wrap it with another stream, such as a buffered input stream or a scanner, to make it easier and more convenient. For example, you can use the Scanner
class to read user input from the System.in
stream, as shown below:
// create a scanner to read user input from the standard input device Scanner sc = new Scanner(System.in); // prompt the user to enter their name System.out.print("Enter your name: "); // read the user input as a string String name = sc.nextLine(); // print a greeting message to the user System.out.println("Hello, " + name + "!");
The System
class provides methods to access and manipulate the standard streams, such as changing their source or destination, or flushing their buffer. For example, you can use the setIn
, setOut
, and setErr
methods to change the source or destination of the standard streams, such as redirecting them to a file or a network socket. You can also use the flush
method to flush the buffer of the standard output and error streams, ensuring that all the data is written to the destination.
The Scanner
class is a useful class to read user input from the standard input stream, or any other input stream. The Scanner
class provides methods to read different types of data, such as strings, numbers, booleans, or tokens. For example, you can use the nextInt
, nextDouble
, and nextBoolean
methods to read an integer, a double, and a boolean from the user input, respectively. You can also use the next
and nextLine
methods to read a single word or a whole line from the user input, respectively.
The PrintStream
and PrintWriter
classes are useful classes to write formatted data to the standard output and error streams, or any other output stream. The PrintStream
and PrintWriter
classes provide methods to write different types of data, such as strings, numbers, objects, or arrays. For example, you can use the print
and println
methods to write a single value or a whole line to the output stream, respectively. You can also use the printf
and format
methods to write formatted data to the output stream, using a format string and a list of arguments.
The Console
class is another way to perform input/output operations with the standard input and output devices. The Console
class provides methods for reading and writing text data, such as passwords, formatted strings, or arrays of characters. The Console
class is a singleton class, which means that there is only one instance of it in the program. You can obtain the instance of the Console
class by calling the System.console
method, as shown below:
// get the instance of the console class Console console = System.console();
However, the System.console
method may return null
if there is no console available for the program, such as when the program is run from an IDE or a GUI. Therefore, you should always check the return value of the System.console
method before using it, and handle the possible null
value accordingly.
One of the advantages of using the Console
class is that it provides a method to read a password from the user without echoing it to the screen. This method is called readPassword
, and it returns an array of characters that contains the password entered by the user. For example, you can use the readPassword
method to prompt the user to enter their password, as shown below:
// prompt the user to enter their password console.print("Enter your password: "); // read the password as an array of characters char[] password = console.readPassword(); // print a message to the user console.println("Your password is " + new String(password));
Another advantage of using the Console
class is that it provides methods to write formatted data to the console, using a format string and a list of arguments. These methods are called format
and printf
, and they are similar to the methods of the same name in the PrintStream
and PrintWriter
classes. For example, you can use the format
or printf
method to write a formatted message to the console, as shown below:
// write a formatted message to the console console.format("Hello, %s! You are %d years old.\n", name, age); // or console.printf("Hello, %s! You are %d years old.\n", name, age);
In this section, you learned how to use the standard streams and the console class to perform input/output operations with the standard input, output, and error devices. You learned how to use the System class to access and manipulate the standard streams, such as changing their source or destination, or flushing their buffer. You learned how to use the Scanner class to read user input from the standard input stream, such as strings, numbers, or booleans. You learned how to use the PrintStream and PrintWriter classes to write formatted data to the standard output and error streams, such as strings, numbers, or objects. You learned what is the console class and how to use it to read and write text data from and to the standard input and output devices. You learned how to use the readPassword method to read a password from the user without echoing it to the screen. You learned how to use the format and printf methods to write formatted data to the console, such as strings, numbers, or objects.
3. Java Readers and Writers
In this section, you will learn how to use the Java readers and writers to perform input/output operations with text data. Readers and writers are types of character streams that deal with text data, such as characters, strings, or Unicode. Readers and writers provide a convenient and high-level way to read and write text data from and to different sources and destinations, such as files, strings, arrays, or network sockets.
Some of the key concepts and topics that you will learn in this section are:
- What are the readers and writers and how they enable input/output operations with text data.
- How to use the
FileReader
andFileWriter
classes to read and write text data from and to files. - How to use the
BufferedReader
andBufferedWriter
classes to buffer the input/output operations with text data. - How to use the
InputStreamReader
andOutputStreamWriter
classes to convert byte streams to character streams, and vice versa.
A reader is a type of character input stream that reads text data from a source, one character at a time. A reader performs encoding of data, meaning that it converts bytes to characters, according to a specified character set. A reader is represented by the java.io.Reader
class, which is the abstract base class for all readers. The Reader
class provides the basic methods for reading text data, such as read()
, close()
, and skip()
.
A writer is a type of character output stream that writes text data to a destination, one character at a time. A writer performs decoding of data, meaning that it converts characters to bytes, according to a specified character set. A writer is represented by the java.io.Writer
class, which is the abstract base class for all writers. The Writer
class provides the basic methods for writing text data, such as write()
, close()
, and flush()
.
The subclasses of the Reader
and Writer
classes provide more specific functionality and features for different types of sources and destinations, such as files, strings, arrays, or network sockets. For example, the java.io.FileReader
and java.io.FileWriter
classes are readers and writers that read and write text data from and to files. The java.io.BufferedReader
and java.io.BufferedWriter
classes are readers and writers that buffer the input/output operations with text data. The java.io.InputStreamReader
and java.io.OutputStreamWriter
classes are readers and writers that convert byte streams to character streams, and vice versa.
One of the most common and useful classes for reading and writing text data from and to files are the FileReader
and FileWriter
classes. These classes are subclasses of the Reader
and Writer
classes, and they provide constructors and methods for accessing files as sources or destinations of text data. For example, you can create a file reader by passing a file name, a file object, or a file descriptor to the constructor of the file reader class, as shown below:
// create a file reader to read text data from a file FileReader fr = new FileReader("input.txt");
Similarly, you can create a file writer by passing a file name, a file object, a file descriptor, or a boolean flag to the constructor of the file writer class, as shown below:
// create a file writer to write text data to a file FileWriter fw = new FileWriter("output.txt");
The boolean flag indicates whether to append the data to the end of the file or overwrite the existing data. The default value is false, meaning that the data will overwrite the existing data.
Once you have created a file reader or a file writer, you can use the methods of the Reader
or Writer
class to read or write text data, such as read()
, write()
, close()
, and flush()
. For example, you can use the read()
method of the file reader class to read a single character from the file, as shown below:
// read a single character from the file int c = fr.read(); // check if the end of the file is reached if (c == -1) { // close the file reader fr.close(); } else { // print the character to the standard output device System.out.print((char) c); }
You can also use the write()
method of the file writer class to write a single character to the file, as shown below:
// write a single character to the file fw.write('A'); // flush the file writer fw.flush();
The flush()
method ensures that the data is written to the file, and not left in the buffer. You can also use the close()
method of the file writer class to close the file writer and flush the buffer, as shown below:
// close the file writer fw.close();
The FileReader
and FileWriter
classes are convenient and easy to use, but they also have some limitations and drawbacks. One of the limitations is that they do not allow you to specify the character set or encoding that the file uses, and they use the default character set or encoding of the platform. This can cause some problems or errors if the file uses a different character set or encoding than the default one. Another limitation is that they do not provide any buffering functionality, and they read or write data one character at a time. This can reduce the performance and efficiency of input/output operations, as it can increase the number of calls to the file system.
To overcome these limitations, you can use the BufferedReader
and BufferedWriter
classes, which are subclasses of the Reader
and Writer
classes, and they provide buffering functionality for the input/output operations with text data. You can also use the InputStreamReader
and OutputStreamWriter
classes, which are subclasses of the Reader
and Writer
classes, and they provide conversion functionality between byte streams and character streams. You can combine these classes with the FileReader
and FileWriter
classes, or with the FileInputStream
and FileOutputStream
classes, to create more flexible and efficient readers and writers for files.
For example, you can create a buffered reader by passing a file reader to the constructor of the buffered reader class, as shown below:
// create a file reader to read text data from a file FileReader fr = new FileReader("input.txt"); // create a buffered reader to wrap around the file reader BufferedReader br = new BufferedReader(fr);
Similarly, you can create a buffered writer by passing a file writer to the constructor of the buffered writer class, as shown below:
// create a file writer to write text data to a file FileWriter fw = new FileWriter("output.txt"); // create a buffered writer to wrap around the file writer BufferedWriter bw = new BufferedWriter(fw);
The advantage of using buffered readers and writers is that they can improve the performance and efficiency of input/output operations, as they reduce the number of calls to the file system. The buffered reader and writer classes also provide some additional methods for reading and writing text data, such as readLine()
, newLine()
, or read(char[])
. For example, you can use the readLine()
method of the buffered reader class to read a line of text from the file, as shown below:
// read a line of text from the file String line = br.readLine(); // check if the end of the file is reached if (line == null) { // close the buffered reader br.close(); } else { // print the line to the standard output device System.out.println(line); }
You can also use the <code
3.1. FileReader and FileWriter
In this section, you will learn how to use the FileReader and FileWriter classes to read and write text data from and to files. The FileReader and FileWriter classes are character streams that provide a convenient and high-level way to handle text files in Java. The FileReader and FileWriter classes are subclasses of the Reader and Writer classes, respectively, and they inherit the methods and features of their parent classes.
Some of the key concepts and topics that you will learn in this section are:
- How to create a FileReader object to read text data from a file, using a file name or a file object as a parameter.
- How to use the read methods of the FileReader class to read a single character, an array of characters, or a portion of an array of characters from the file.
- How to handle the IOException and the FileNotFoundException that may occur when reading from a file, using a try-catch-finally block or a try-with-resources statement.
- How to close the FileReader object after reading from the file, using the close method or the try-with-resources statement.
- How to create a FileWriter object to write text data to a file, using a file name or a file object as a parameter, and optionally specifying whether to append or overwrite the existing file.
- How to use the write methods of the FileWriter class to write a single character, an array of characters, a portion of an array of characters, or a string to the file.
- How to handle the IOException that may occur when writing to a file, using a try-catch-finally block or a try-with-resources statement.
- How to flush the FileWriter object to ensure that all the data is written to the file, using the flush method or the try-with-resources statement.
- How to close the FileWriter object after writing to the file, using the close method or the try-with-resources statement.
To create a FileReader object, you need to pass a file name or a file object as a parameter to the constructor of the FileReader class. For example, you can create a FileReader object to read from a file named “input.txt”, as shown below:
// create a FileReader object to read from a file named "input.txt" FileReader fr = new FileReader("input.txt");
Alternatively, you can create a File object to represent the file, and pass it to the constructor of the FileReader class, as shown below:
// create a File object to represent the file named "input.txt" File file = new File("input.txt"); // create a FileReader object to read from the file object FileReader fr = new FileReader(file);
To read data from the file, you can use the read methods of the FileReader class. The read methods return an int value that represents the character read, or -1 if the end of the file is reached. The read methods can read a single character, an array of characters, or a portion of an array of characters from the file. For example, you can use the read method to read a single character from the file, as shown below:
// read a single character from the file int c = fr.read(); // print the character to the standard output device System.out.print((char) c);
You can also use the read method to read an array of characters from the file, as shown below:
// create an array of characters to store the data read from the file char[] buffer = new char[1024]; // read an array of characters from the file int n = fr.read(buffer); // print the array of characters to the standard output device System.out.print(new String(buffer, 0, n));
You can also use the read method to read a portion of an array of characters from the file, by specifying the offset and the length of the portion, as shown below:
// create an array of characters to store the data read from the file char[] buffer = new char[1024]; // read a portion of an array of characters from the file, starting from the index 10 and reading 100 characters int n = fr.read(buffer, 10, 100); // print the portion of the array of characters to the standard output device System.out.print(new String(buffer, 10, n));
When reading from a file, you may encounter some exceptions, such as IOException or FileNotFoundException. An IOException is a general exception that occurs when an input/output operation fails or is interrupted. A FileNotFoundException is a specific exception that occurs when the file to be read does not exist or cannot be accessed. To handle these exceptions, you can use a try-catch-finally block or a try-with-resources statement. A try-catch-finally block allows you to execute some code in the try block, catch and handle the exceptions in the catch block, and perform some cleanup actions in the finally block. A try-with-resources statement allows you to declare one or more resources, such as a FileReader object, in the try block, and automatically close them at the end of the block. For example, you can use a try-catch-finally block to handle the exceptions when reading from a file, as shown below:
// declare a FileReader object FileReader fr = null; try { // create a FileReader object to read from a file named "input.txt" fr = new FileReader("input.txt"); // read and print the data from the file int c; while ((c = fr.read()) != -1) { System.out.print((char) c); } } catch (FileNotFoundException e) { // handle the FileNotFoundException System.out.println("The file does not exist or cannot be accessed."); } catch (IOException e) { // handle the IOException System.out.println("An error occurred while reading from the file."); } finally { // close the FileReader object if (fr != null) { try { fr.close(); } catch (IOException e) { // handle the IOException System.out.println("An error occurred while closing the file."); } } }
You can also use a try-with-resources statement to handle the exceptions when reading from a file, as shown below:
try ( // create and declare a FileReader object to read from a file named "input.txt" FileReader fr = new FileReader("input.txt"); ) { // read and print the data from the file int c; while ((c = fr.read()) != -1) { System.out.print((char) c); } } catch (FileNotFoundException e) { // handle the FileNotFoundException System.out.println("The file does not exist or cannot be accessed."); } catch (IOException e) { // handle the IOException System.out.println("An error occurred while reading from the file."); }
The advantage of using a try-with-resources statement is that it simplifies the code and ensures that the resources are closed automatically at the end of the block, regardless of whether an exception occurs or not.
To create a FileWriter object, you need to pass a file name or a file object as a parameter to the constructor of the FileWriter class. You can also optionally specify whether to append or overwrite the existing file, by passing a boolean value as a second parameter. If the boolean value is true, the data will be appended to the end of the file. If the boolean value is false, or omitted, the data will overwrite the existing file. For example, you can create a FileWriter object to write to a file named “output.txt”, and append the data to the end of the file, as shown below:
// create a FileWriter object to write to a file named "output.txt", and append the data to the end of the file FileWriter fw = new FileWriter("output.txt", true);
Alternatively, you can create a File object to represent the file, and pass it to the constructor of the FileWriter class, as shown below:
// create a File object to represent the file named "output.txt" File file = new File("output.txt"); // create a FileWriter object to write to the file object, and overwrite the existing file FileWriter fw = new FileWriter(file, false);
To write data to the file, you can use the write methods of the FileWriter class. The write methods do not return any value, but they may throw an IOException if an error occurs. The write methods can write a single character, an array of characters, a portion of an array of characters, or a string to the file. For example, you can use the write method to write a single character to the file, as shown below:
// write a single character to the file fw.write('A');
You can also use the write method to write an array of characters to the file, as shown below:
// create an array of characters to write
3.2. BufferedReader and BufferedWriter
In the previous section, you learned how to use the FileReader
and FileWriter
classes to read and write text data from and to files. However, these classes are unbuffered, which means they read and write data one character at a time. This can be inefficient and slow, especially if you are dealing with large files or frequent input/output operations. To improve the performance and efficiency of reading and writing text data, you can use the BufferedReader
and BufferedWriter
classes, which are buffered character streams.
The BufferedReader
and BufferedWriter
classes are wrapper classes that wrap around another reader or writer and add buffering functionality to it. A buffered reader uses an internal buffer to store data temporarily, and reads data from the underlying reader in large chunks. A buffered writer uses an internal buffer to store data temporarily, and writes data to the underlying writer in large chunks. By using buffered streams, you can reduce the number of calls to the underlying source or destination, and improve the input/output performance and efficiency.
To create a buffered reader or writer, you need to pass another reader or writer to the constructor of the buffered reader or writer class. For example, you can create a buffered reader by passing a file reader to the constructor of the buffered reader class, as shown below:
// create a file reader to read data from a file FileReader fr = new FileReader("input.txt"); // create a buffered reader to wrap around the file reader BufferedReader br = new BufferedReader(fr);
Similarly, you can create a buffered writer by passing a file writer to the constructor of the buffered writer class, as shown below:
// create a file writer to write data to a file FileWriter fw = new FileWriter("output.txt"); // create a buffered writer to wrap around the file writer BufferedWriter bw = new BufferedWriter(fw);
The advantage of using buffered readers and writers is that they provide more convenient and efficient methods for reading and writing text data, such as readLine()
and write(String)
. The readLine()
method reads a line of text from the buffered reader, and returns it as a string. The write(String)
method writes a string to the buffered writer, and does not add a new line character. To add a new line character, you can use the newLine()
method.
For example, suppose you want to copy the contents of a text file to another text file, using buffered readers and writers. You can use the following code to do so:
// create a buffered reader to read data from a file BufferedReader br = new BufferedReader(new FileReader("input.txt")); // create a buffered writer to write data to a file BufferedWriter bw = new BufferedWriter(new FileWriter("output.txt")); // declare a string variable to store each line of text String line; // read each line of text from the buffered reader until the end of the file while ((line = br.readLine()) != null) { // write the line of text to the buffered writer bw.write(line); // add a new line character to the buffered writer bw.newLine(); } // close the buffered reader and writer br.close(); bw.close();
The disadvantage of using buffered readers and writers is that they may not flush the data to the underlying source or destination immediately, as they use an internal buffer to store data temporarily. To flush the data to the destination, you need to call the flush()
method explicitly, or close the buffered writer. If you do not flush or close the buffered writer, you may lose some data that is still in the buffer.
In this section, you learned how to use the BufferedReader
and BufferedWriter
classes to read and write text data from and to files in a buffered and efficient way. You also learned the advantages and disadvantages of using buffered streams, and how to flush and close them properly. In the next section, you will learn how to use the InputStreamReader
and OutputStreamWriter
classes to convert byte streams to character streams, and vice versa.
3.3. InputStreamReader and OutputStreamWriter
In the previous sections, you learned how to use byte streams and character streams to perform input/output operations in Java. However, sometimes you may need to convert between byte streams and character streams, depending on the type of data and the encoding scheme. For example, suppose you want to read text data from a network socket, which is a byte stream, but you need to convert it to a character stream, so that you can use the methods of the Reader
class. Or, suppose you want to write text data to a file, which is a character stream, but you need to convert it to a byte stream, so that you can use the methods of the OutputStream
class. How can you do that?
The answer is to use the InputStreamReader
and OutputStreamWriter
classes, which are bridge classes that convert byte streams to character streams, and vice versa. The InputStreamReader
class is a character input stream that reads bytes from an underlying byte input stream, and converts them to characters using a specified encoding scheme. The OutputStreamWriter
class is a character output stream that writes characters to an underlying byte output stream, and converts them to bytes using a specified encoding scheme.
To create an input stream reader or an output stream writer, you need to pass a byte input stream or a byte output stream to the constructor of the input stream reader or output stream writer class. You can also optionally specify an encoding scheme, such as UTF-8, ISO-8859-1, or US-ASCII, to use for the conversion. If you do not specify an encoding scheme, the default encoding scheme of your platform will be used. For example, you can create an input stream reader by passing a socket input stream and an encoding scheme to the constructor of the input stream reader class, as shown below:
// create a socket input stream to read data from a network socket Socket socket = new Socket("example.com", 80); InputStream is = socket.getInputStream(); // create an input stream reader to wrap around the socket input stream and use UTF-8 encoding InputStreamReader isr = new InputStreamReader(is, "UTF-8");
Similarly, you can create an output stream writer by passing a file output stream and an encoding scheme to the constructor of the output stream writer class, as shown below:
// create a file output stream to write data to a file FileOutputStream fos = new FileOutputStream("output.txt"); // create an output stream writer to wrap around the file output stream and use UTF-8 encoding OutputStreamWriter osw = new OutputStreamWriter(fos, "UTF-8");
The advantage of using input stream readers and output stream writers is that they allow you to convert between byte streams and character streams, and use different encoding schemes for the conversion. This can be useful for reading and writing text data from and to different sources and destinations, such as network sockets, files, or databases, that may use different encoding schemes. The disadvantage of using input stream readers and output stream writers is that they may introduce some overhead and complexity, as they require additional processing and management for the conversion.
In this section, you learned how to use the InputStreamReader
and OutputStreamWriter
classes to convert byte streams to character streams, and vice versa, using different encoding schemes. You also learned the advantages and disadvantages of using these classes, and how to create and use them properly. In the next section, you will learn how to use the DataInputStream
and DataOutputStream
classes to read and write primitive data types and strings in a binary format.
4. Java Streams and Filters
In the previous sections, you learned how to use byte streams and character streams to perform input/output operations in Java. However, sometimes you may need to manipulate the data in different formats and objects, such as primitive data types, strings, or serializable objects. For example, suppose you want to read an integer value from a file, which is stored as a sequence of bytes, but you need to convert it to an int data type, so that you can use it in your program. Or, suppose you want to write an object to a file, which is an instance of a class that implements the Serializable
interface, but you need to convert it to a sequence of bytes, so that you can store it in the file. How can you do that?
The answer is to use the DataInputStream
and DataOutputStream
classes, which are filter classes that read and write primitive data types and strings in a binary format. The DataInputStream
class is a byte input stream that reads bytes from an underlying byte input stream, and converts them to primitive data types and strings using a specified format. The DataOutputStream
class is a byte output stream that writes primitive data types and strings to an underlying byte output stream, and converts them to bytes using a specified format.
To create a data input stream or a data output stream, you need to pass a byte input stream or a byte output stream to the constructor of the data input stream or data output stream class. For example, you can create a data input stream by passing a file input stream to the constructor of the data input stream class, as shown below:
// create a file input stream to read data from a file FileInputStream fis = new FileInputStream("input.dat"); // create a data input stream to wrap around the file input stream DataInputStream dis = new DataInputStream(fis);
Similarly, you can create a data output stream by passing a file output stream to the constructor of the data output stream class, as shown below:
// create a file output stream to write data to a file FileOutputStream fos = new FileOutputStream("output.dat"); // create a data output stream to wrap around the file output stream DataOutputStream dos = new DataOutputStream(fos);
The advantage of using data input streams and data output streams is that they provide convenient and efficient methods for reading and writing primitive data types and strings in a binary format, such as readInt()
, writeDouble()
, readUTF()
, and writeUTF()
. The readInt()
method reads four bytes from the data input stream, and returns an int value. The writeDouble()
method writes a double value to the data output stream, and converts it to eight bytes. The readUTF()
method reads a string from the data input stream, and returns it as a String
object. The writeUTF()
method writes a string to the data output stream, and converts it to a sequence of bytes.
For example, suppose you want to read an int value and a string from a file, using data input streams. You can use the following code to do so:
// create a data input stream to read data from a file DataInputStream dis = new DataInputStream(new FileInputStream("input.dat")); // read an int value from the data input stream int n = dis.readInt(); // read a string from the data input stream String s = dis.readUTF(); // print the int value and the string System.out.println("n = " + n); System.out.println("s = " + s); // close the data input stream dis.close();
The disadvantage of using data input streams and data output streams is that they may not be compatible with other platforms or programs, as they use a specific format for the conversion. For example, the writeUTF()
method writes a string to the data output stream using a modified UTF-8 encoding, which may not be recognized by other programs that use a different encoding scheme. Also, data input streams and data output streams may not be suitable for some scenarios, such as reading and writing complex objects, such as arrays, collections, or custom classes, that may not have a simple binary representation.
In this section, you learned how to use the DataInputStream
and DataOutputStream
classes to read and write primitive data types and strings in a binary format. You also learned the advantages and disadvantages of using these classes, and how to create and use them properly. In the next section, you will learn how to use the ObjectInputStream
and ObjectOutputStream
classes to read and write objects in a serialized format.
4.1. DataInputStream and DataOutputStream
In this section, you will learn how to use the DataInputStream
and DataOutputStream
classes to read and write primitive data types and strings in a binary format. These classes are useful for manipulating data in a platform-independent way, as they use a standard format to store and transfer data. You will also learn how to use the EOFException
class to handle the end-of-file condition when reading data from a stream.
The DataInputStream
class is a byte input stream that reads data in a binary format from another input stream. The DataOutputStream
class is a byte output stream that writes data in a binary format to another output stream. These classes provide methods for reading and writing all the primitive data types and strings, such as readInt()
, writeInt()
, readDouble()
, writeDouble()
, readUTF()
, and writeUTF()
. These methods use a standard format to store and transfer data, regardless of the platform or the encoding scheme. For example, the writeInt()
method writes an integer value as four bytes in big-endian order, and the readInt()
method reads four bytes in big-endian order and converts them to an integer value.
To create a data input stream, you need to pass another input stream to the constructor of the DataInputStream
class. Similarly, to create a data output stream, you need to pass another output stream to the constructor of the DataOutputStream
class. For example, you can create a data input stream and a data output stream by passing a file input stream and a file output stream, respectively, as shown below:
// create a file input stream to read data from a file FileInputStream fis = new FileInputStream("data.bin"); // create a data input stream to wrap around the file input stream DataInputStream dis = new DataInputStream(fis); // create a file output stream to write data to a file FileOutputStream fos = new FileOutputStream("data.bin"); // create a data output stream to wrap around the file output stream DataOutputStream dos = new DataOutputStream(fos);
Once you have created a data input stream and a data output stream, you can use their methods to read and write data in a binary format. For example, you can write an integer, a double, and a string to the data output stream, as shown below:
// write an integer value to the data output stream dos.writeInt(42); // write a double value to the data output stream dos.writeDouble(3.14); // write a string value to the data output stream dos.writeUTF("Hello, world!");
You can also read an integer, a double, and a string from the data input stream, as shown below:
// read an integer value from the data input stream int i = dis.readInt(); // read a double value from the data input stream double d = dis.readDouble(); // read a string value from the data input stream String s = dis.readUTF();
When you read data from a data input stream, you need to be careful about the end-of-file condition. The end-of-file condition occurs when there is no more data to read from the stream. If you try to read data from a stream that has reached the end-of-file, you will get an EOFException
. The EOFException
class is a subclass of the IOException
class that indicates that the end of the stream has been reached unexpectedly. You can use a try-catch
block to handle the EOFException
gracefully, as shown below:
// create a boolean variable to indicate the end-of-file condition boolean eof = false; // use a while loop to read data from the data input stream until the end-of-file is reached while (!eof) { try { // read an integer value from the data input stream int i = dis.readInt(); // print the integer value to the standard output device System.out.println(i); } catch (EOFException e) { // catch the EOFException and set the eof variable to true eof = true; } }
In this section, you learned how to use the DataInputStream
and DataOutputStream
classes to read and write primitive data types and strings in a binary format. You also learned how to use the EOFException
class to handle the end-of-file condition when reading data from a stream. These classes are useful for manipulating data in a platform-independent way, as they use a standard format to store and transfer data.
4.2. ObjectInputStream and ObjectOutputStream
In this section, you will learn how to use the ObjectInputStream
and ObjectOutputStream
classes to read and write objects in a serialized format. These classes are useful for storing and transferring objects that implement the Serializable
interface. You will also learn how to use the ClassNotFoundException
class to handle the exception that occurs when the class of a serialized object cannot be found.
The ObjectInputStream
class is a byte input stream that reads objects in a serialized format from another input stream. The ObjectOutputStream
class is a byte output stream that writes objects in a serialized format to another output stream. These classes provide methods for reading and writing objects, such as readObject()
and writeObject()
. These methods use a standard format to store and transfer objects, regardless of the platform or the implementation of the class. For example, the writeObject()
method writes an object to the output stream, along with its class name, fields, and values. The readObject()
method reads an object from the input stream, and creates an instance of the class with the same fields and values.
To create an object input stream, you need to pass another input stream to the constructor of the ObjectInputStream
class. Similarly, to create an object output stream, you need to pass another output stream to the constructor of the ObjectOutputStream
class. For example, you can create an object input stream and an object output stream by passing a file input stream and a file output stream, respectively, as shown below:
// create a file input stream to read data from a file FileInputStream fis = new FileInputStream("objects.bin"); // create an object input stream to wrap around the file input stream ObjectInputStream ois = new ObjectInputStream(fis); // create a file output stream to write data to a file FileOutputStream fos = new FileOutputStream("objects.bin"); // create an object output stream to wrap around the file output stream ObjectOutputStream oos = new ObjectOutputStream(fos);
Once you have created an object input stream and an object output stream, you can use their methods to read and write objects in a serialized format. For example, you can write a Person
object to the object output stream, as shown below:
// create a Person object with some fields and values Person p = new Person("Alice", 25, "alice@example.com"); // write the Person object to the object output stream oos.writeObject(p);
You can also read a Person
object from the object input stream, as shown below:
// read a Person object from the object input stream Person p = (Person) ois.readObject(); // print the fields and values of the Person object System.out.println(p.getName()); System.out.println(p.getAge()); System.out.println(p.getEmail());
When you read an object from an object input stream, you need to cast it to the appropriate class, as the readObject()
method returns an Object
reference. You also need to make sure that the class of the object implements the Serializable
interface, which is a marker interface that indicates that the class can be serialized and deserialized. The Serializable
interface does not have any methods or fields, but it is required for the object input and output streams to work properly. For example, the Person
class should implement the Serializable
interface, as shown below:
// a class that represents a person public class Person implements Serializable { // some fields and values private String name; private int age; private String email; // a constructor that initializes the fields public Person(String name, int age, String email) { this.name = name; this.age = age; this.email = email; } // some getters and setters for the fields public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } }
When you read an object from an object input stream, you also need to be careful about the ClassNotFoundException
. The ClassNotFoundException
class is a subclass of the Exception
class that indicates that the class of a serialized object cannot be found. This exception can occur when the class of the object is not available in the classpath, or when the class of the object has been changed or deleted. You can use a try-catch
block to handle the ClassNotFoundException
gracefully, as shown below:
// create a boolean variable to indicate the end-of-file condition boolean eof = false; // use a while loop to read objects from the object input stream until the end-of-file is reached while (!eof) { try { // read an object from the object input stream Object obj = ois.readObject(); // check if the object is an instance of the Person class if (obj instanceof Person) { // cast the object to the Person class Person p = (Person) obj; // print the fields and values of the Person object System.out.println(p.getName()); System.out.println(p.getAge()); System.out.println(p.getEmail()); } } catch (EOFException e) { // catch the EOFException and set the eof variable to true eof = true; } catch (ClassNotFoundException e) { // catch the ClassNotFoundException and print an error message System.err.println("Class not found: " + e.getMessage()); } }
In this section, you learned how to use the ObjectInputStream
and ObjectOutputStream
classes to read and write objects in a serialized format. You also learned how to use the ClassNotFoundException
class to handle the exception that occurs when the class of a serialized object cannot be found. These classes are useful for storing and transferring objects that implement the Serializable
interface.
4.3. PrintStream and PrintWriter
In this section, you will learn how to use the PrintStream
and PrintWriter
classes to format and print data in a human-readable way. These classes are useful for creating output that is easy to read and understand, such as messages, reports, or logs. You will also learn how to use the printf()
method to format data using placeholders and modifiers.
The PrintStream
class is a byte output stream that writes data to another output stream. The PrintWriter
class is a character output stream that writes data to another output stream. These classes provide methods for printing data, such as print()
, println()
, and printf()
. These methods write data to the output stream, and optionally append a newline character at the end. These methods also do not throw any IOException
, but instead set an internal error flag that can be checked using the checkError()
method.
To create a print stream, you need to pass another output stream to the constructor of the PrintStream
class. Similarly, to create a print writer, you need to pass another output stream to the constructor of the PrintWriter
class. For example, you can create a print stream and a print writer by passing the standard output stream, as shown below:
// create a print stream to write data to the standard output device PrintStream ps = new PrintStream(System.out); // create a print writer to write data to the standard output device PrintWriter pw = new PrintWriter(System.out);
Once you have created a print stream and a print writer, you can use their methods to format and print data in a human-readable way. For example, you can use the println()
method to print a message to the output stream, as shown below:
// print a message to the output stream using the print stream ps.println("This is a message from the print stream."); // print a message to the output stream using the print writer pw.println("This is a message from the print writer.");
You can also use the printf()
method to format data using placeholders and modifiers. The printf()
method takes a format string and a variable number of arguments, and writes a formatted string to the output stream. The format string contains plain text and placeholders, which are replaced by the corresponding arguments. The placeholders start with a percent sign (%) and have a specific syntax, as shown below:
%[flags][width][.precision]conversion
The flags are optional characters that modify the output, such as -
for left alignment, +
for always showing the sign, or 0
for padding with zeros. The width is an optional integer that specifies the minimum number of characters to be written. The precision is an optional integer that specifies the number of digits after the decimal point for floating-point values, or the maximum number of characters for strings. The conversion is a required character that indicates the type of the argument, such as d
for decimal integer, f
for floating-point, or s
for string.
For example, you can use the printf()
method to format and print some data, as shown below:
// print a formatted string to the output stream using the print stream ps.printf("The value of pi is %.2f.\n", Math.PI); // print a formatted string to the output stream using the print writer pw.printf("The name of the user is %s.\n", "Alice");
In this section, you learned how to use the PrintStream
and PrintWriter
classes to format and print data in a human-readable way. You also learned how to use the printf()
method to format data using placeholders and modifiers. These classes are useful for creating output that is easy to read and understand, such as messages, reports, or logs.
5. Conclusion
In this tutorial, you learned how to perform input/output operations in Java using streams, readers and writers. You learned the basic concepts and terminology related to Java input/output, such as streams, byte streams, character streams, buffered streams, unbuffered streams, standard streams, and console. You also learned how to use various classes and methods to read and write data from and to different sources and destinations, such as files, network sockets, arrays, strings, or objects. You also learned how to format and print data in a human-readable way, using placeholders and modifiers.
By following this tutorial, you gained a solid understanding of the Java input/output system and how to use it effectively in your programs. You also acquired the skills and knowledge to solve various input/output problems in Java. You can apply these skills and knowledge to create more complex and interactive programs that can exchange data with the external world.
We hope you enjoyed this tutorial and found it useful. If you have any questions or feedback, please feel free to leave a comment below. Thank you for reading and happy coding!