Introduction
In this article, we discuss I/O APIs in Java 7.
I/O APIs in Java
Java provides some libraries for input/output operations, in other words reading and writing to files or network sockets. A number of key classes and packages are provided as part of the JDK for dealing with I/O in Java.
Java Packages for dealing with I/O APIs:
- java.io
- java.net
- java.nio
1. Java.io package
In the beginning, Java had only java.io API working with input and output operations on files. This does data stream I/O in Java applications.
It contains classes like:
- FileInputStream
- FileReader
- FileOutputStream
- FileWriter
- BufferedInputStream
- BufferedOutputStream
- ByteArrayOutputStream
- ByteArrayInputStream
2. java.net package
It provides classes to implement network-related applications.
It contains several classes:
- ContentHandler
- DatagramPacket
- DatagramSocket
- DatagramSocketImpl
- HttpURLConnection
- InetAddress
- MulticastSocket
- Socket
- SocketImpl
- URLConnection
- URLEncoder
- URLStreamHandler
3. Java.nio package
New I/O, also called nio, is a Java package. It was released with J2SE 1.4 used to extend the features of the java.io package. It provides low-level I/O operations on modern operating systems.
It contains several classes:
- Buffer
- ByteBuffer
- ByteOrder
- CharBuffer
- DoubleBuffer
- FloatBuffer
- IntBuffer
- LongBuffer
- MappedByteBuffer
- ShortBuffer
Limitation
The following are limitations of the current I/O API:
- In the current I/O API there is no guarantee of proper deletion of a file, you must check again and again that the file was deleted.
- Some operations are not scalable on directories and run on the parent thread.
- When some changes happen in a file you need to do polling.
Then came Java 7 that provides a way to handle I/O operations on a file with respect to the underlying file system.
What's New
The following are what's new in the Java 7 I/O APIs:
- New File System API
- File Notifications
- Directory Operations
- Asynchronous I/0
Reasons
The following are the reasons behind the change in APIs:
1. In the previous versions, the java.io.File class is:
- inconsistent in handling files from various operating systems.
- also inefficient in catching file attributes.
- it has certain limits in accessing functionality from the filesystem.
- when something goes wrong, some of its methods don't throw informative errors.
2. The work is done in the new I/O API provides fast and scalable I/O.


Join the conversation! Your thoughts help the community grow.