In terms of performance, this is a big improvement in the API for I/O operations.īuffers, Selectors, and Channels are the three primary components of Java NIO, although in this article we'll focus strictly on using the NIO classes for interacting with files, and not necessarily the concepts behind the API.Īs this tutorial is about reading and writing files, we will discuss only the related classes in this short section: Java NIO is a non-blocking I/O API which was introduced back in Java 4 and can be found in the java.nio package. And again, as we saw in the character streams, this is a more efficient method than FileOutputStream thanks to the buffering. ByteOutputStream: This class is a wrapper over OutputStream to support buffering capabilities.FileOutputStream: A class to write raw bytes to the file.OutputStream: An abstract class to write byte streams.As we saw in the character streams, this is a more efficient method than FileInputStream.Īnd here are the classes used to write the byte data: BufferedInputStream: This is a wrapper over InputStream that supports buffering capabilities.FileInputStream: A class to simply read bytes from a file.InputStream: An abstract class to read the byte streams.Here are the classes used to read the byte data: All of these classes are defined under java.io package. In this section we'll take a look at the most commonly used classes. Here you work with raw bytes, which could be characters, image data, unicode data (which takes 2 bytes to represent a character), etc. This is different from before in the way they treat the data. And like the BufferedReader, this reduces the number of total I/O operations with file system.īyte Streams are used to read or write byte data with files. This is most preferable class to write data to a file since more data can be written to the file in one write() call. BufferedWriter: This is a wrapper over the Writer class, which also supports buffering capabilities.FileWriter: A class to actually write characters to the file.OutputStreamWriter: This class is used to write character streams and also convert them to byte streams.Writer: This is an abstract class to write the character streams.In many cases this is most preferable class to read data because more data can been read from the file in one read() call, reducing the number of actual I/O operations with file system.Īnd here are some classes you can use to write character data to a file: BufferedReader: This is a wrapper over the Reader class that supports buffering capabilities.FileReader: A class to read the characters from a file.InputStreamReader: Class used to read the byte stream and converts to character stream.Reader: An abstract class to read a character stream. ![]() Here are some classes you should know that can be used to read character data: Let's look at the most commonly used classes. Character StreamsĬharacter Streams are used to read or write the characters data type. There are two types of Streams you can use to interact with files:įor each of the above stream types, there are several supporting classes shipped with Java, which we'll take a quick look at below. In the following sections we'll provide a brief overview of the Java I/O classes and explain what they do, then we'll take a look at Java NIO Streams, and finally we'll show some examples of reading and writing data to files. With subsequent releases, Java I/O has been improved, simplified and enhanced to support new features.īefore we get in to some actual examples, it would help to understand the classes available to you that will handle the reading and writing of data to files. ![]() ![]() Java provides several API (also known as Java I/O) to read and write files since its initial releases. In this tutorial, we are going to show the most common ways you can read and write to files in Java. This data could be cache data, data you retrieved for a dataset, an image, or just about anything else you can think of. When programming, whether you're creating a mobile app, a web application, or just writing scripts, you often have the need to read or write data to a file. In this article, we'll be diving into Reading and Writing Files in Java.
0 Comments
Leave a Reply. |