3 ways to read files using Java NIO | How to do in JAVA
New I/O, usually called NIO, is a collection of APIs that offer additional capabilities for intensive I/O operations. It was introduced with the Java 1.4 release by Sun Microsystems to complement an existing standard I/O. The extended NIO that offers further new file system APIs, called NIO2, was released with Java SE 7 (“Dolphin”).
NIO related questions are very popular in java interviews now-a-days.
NIO2 provides two major methods of reading a file:
- Using buffer and channel classes
- Using Path and Files classes
In this post, I am showing a couple of ways to read a file from file system. So lets start them by first showing old famous approach first, so that we can see what really changed.
Old famous I/O way
This example shows how we have been reading a text file using old I/O library APIs. It uses a BufferedReader object for reading. Another way can be using InputStream implementation.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
|
package com.howtodoinjava.test.nio; import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; public class WithoutNIOExample { public static void main(String[] args) { BufferedReader br = null ; String sCurrentLine = null ; try { br = new BufferedReader( new FileReader( "test.txt" )); while ((sCurrentLine = br.readLine()) != null ) { System.out.println(sCurrentLine); } } catch (IOException e) { e.printStackTrace(); } finally { try { if (br != null ) br.close(); } catch (IOException ex) { ex.printStackTrace(); } } } } |
1) Read a small file in buffer of file size
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
|
package com.howtodoinjava.test.nio; import java.io.IOException; import java.io.RandomAccessFile; import java.nio.ByteBuffer; import java.nio.channels.FileChannel; public class ReadFileWithFileSizeBuffer { public static void main(String args[]) { try { RandomAccessFile aFile = new RandomAccessFile( "test.txt" , "r" ); FileChannel inChannel = aFile.getChannel(); long fileSize = inChannel.size(); ByteBuffer buffer = ByteBuffer.allocate(( int ) fileSize); inChannel.read(buffer); buffer.rewind(); buffer.flip(); for ( int i = 0 ; i < fileSize; i++) { System.out.print(( char ) buffer.get()); } inChannel.close(); aFile.close(); } catch (IOException exc) { System.out.println(exc); System.exit( 1 ); } } } |
2) Read a large file in chunks with fixed size buffer
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
|
package com.howtodoinjava.test.nio; import java.io.IOException; import java.io.RandomAccessFile; import java.nio.ByteBuffer; import java.nio.channels.FileChannel; public class ReadFileWithFixedSizeBuffer { public static void main(String[] args) throws IOException { RandomAccessFile aFile = new RandomAccessFile ( "test.txt" , "r" ); FileChannel inChannel = aFile.getChannel(); ByteBuffer buffer = ByteBuffer.allocate( 1024 ); while (inChannel.read(buffer) > 0 ) { buffer.flip(); for ( int i = 0 ; i < buffer.limit(); i++) { System.out.print(( char ) buffer.get()); } buffer.clear(); // do something with the data and clear/compact it. } inChannel.close(); aFile.close(); } } |
3) Faster file copy with mapped byte buffer
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
package com.howtodoinjava.test.nio; import java.io.IOException; import java.io.RandomAccessFile; import java.nio.MappedByteBuffer; import java.nio.channels.FileChannel; public class ReadFileWithMappedByteBuffer { public static void main(String[] args) throws IOException { RandomAccessFile aFile = new RandomAccessFile ( "test.txt" , "r" ); FileChannel inChannel = aFile.getChannel(); MappedByteBuffer buffer = inChannel.map(FileChannel.MapMode.READ_ONLY, 0 , inChannel.size()); buffer.load(); for ( int i = 0 ; i < buffer.limit(); i++) { System.out.print(( char ) buffer.get()); } buffer.clear(); // do something with the data and clear/compact it. inChannel.close(); aFile.close(); } } |
All above techniques will read the content of file and print it to console. You can do whatever you want once you have read it.