Home | | Web Programming | Use NIO for Stream-Based I/O

Chapter: Java The Complete Reference : The Java Library : Input/Output: Exploring java.io

Use NIO for Stream-Based I/O

Beginning with NIO.2, you can use NIO to open an I/O stream. Once you have a Path, open a file by calling newInputStream( ) or newOutputStream( ), which are static methods defined by Files.

Use NIO for Stream-Based I/O

 

Beginning with NIO.2, you can use NIO to open an I/O stream. Once you have a Path, open a file by calling newInputStream( ) or newOutputStream( ), which are static methods defined by Files. These methods return a stream connected to the specified file. In either case, the stream can then be operated on in the way described in Chapter 20, and the same techniques apply. The advantage of using Path to open a file is that all of the features defined by NIO are available for your use.

 

To open a file for stream-based input, use Files.newInputStream( ). It is shown here:

 

static InputStream newInputStream(Path path, OpenOption ... how) throws IOException

 

Here, path specifies the file to open and how specifies how the file will be opened. It must be one or more of the values defined by StandardOpenOption, described earlier. (Of course, only those options that relate to an input stream will apply.) If no options are specified, then the file is opened as if StandardOpenOption.READ were passed.

 

Once opened, you can use any of the methods defined by InputStream. For example, you can use read( ) to read bytes from the file.

The following program demonstrates the use of NIO-based stream I/O. It reworks the ShowFile program from Chapter 13 so that it uses NIO features to open the file and obtain a stream. As you can see, it is very similar to the original, except for the use of Path and newInputStream( ).

 

 

/*       Display a text file using stream-based, NIO code. Requires JDK 7 or later.

 

To use this program, specify the name of the file that you want to see.

 

For example, to see a file called TEST.TXT, use the following command line.

 

java ShowFile TEST.TXT

 

*/

 

import java.io.*;

import java.nio.file.*;

 

class ShowFile {

 

public static void main(String args[])

 

{

 

int i;

 

    //First, confirm that a filename has been specified.

    if(args.length != 1) {

 

System.out.println("Usage: ShowFile filename"); return;

 

}

 

    //Open the file and obtain a stream linked to it.

 

try (

InputStream fin = Files.newInputStream(Paths.get(args[0])) )

 

{

 

do {

 

i = fin.read();

 

if(i != -1) System.out.print((char) i);

 

} while(i != -1);

 

} catch(InvalidPathException e) { System.out.println("Path Error " + e);

} catch(IOException e) {

System.out.println("I/O Error "   + e);

 

}

 

}

 

}

 

Because the stream returned by newInputStream( ) is a normal stream, it can be used like any other stream. For example, you can wrap the stream inside a buffered stream, such as a BufferedInputStream, to provide buffering, as shown here:

 

new BufferedInputStream(Files.newInputStream(Paths.get(args[0])))

 

Now, all reads will be automatically buffered.

 

To open a file for output, use Files.newOutputStream( ). It is shown here:

 

static OutputStream newOutputStream(Path path, OpenOption ... how) throws IOException

 

Here, path specifies the file to open and how specifies how the file will be opened. It must be one or more of the values defined by StandardOpenOption, described earlier. (Of course, only those options that relate to an output stream will apply.) If no options are specified, then the file is opened as if StandardOpenOption.WRITE, StandardOpenOption.CREATE, and StandardOpenOption.TRUNCATE_EXISTING were passed.

 

The methodology for using newOutputStream( ) is similar to that shown previously for newInputStream( ). Once opened, you can use any of the methods defined by OutputStream. For example, you can use write( ) to write bytes to the file. You can also wrap the stream inside a BufferedOutputStream to buffer the stream.

The following program shows newOutputStream( ) in action. It writes the alphabet to a file called test.txt. Notice the use of buffered I/O.

 

// Demonstrate NIO-based, stream output. Requires JDK 7 or later.

 

import java.io.*; import java.nio.file.*;

 

class NIOStreamWrite {

 

public static void main(String args[])

 

{

 

// Open the file and obtain a stream linked to it.

try ( OutputStream fout =

 

new BufferedOutputStream( Files.newOutputStream(Paths.get("test.txt"))) )

 

{

 

// Write some bytes to the stream.

for(int i=0; i < 26; i++)

 

fout.write((byte)('A' + i));

 

} catch(InvalidPathException e) { System.out.println("Path Error " + e);

 

} catch(IOException e) { System.out.println("I/O Error: " + e);

}

 

}


Study Material, Lecturing Notes, Assignment, Reference, Wiki description explanation, brief detail
Java The Complete Reference : The Java Library : Input/Output: Exploring java.io : Use NIO for Stream-Based I/O |


Privacy Policy, Terms and Conditions, DMCA Policy and Compliant

Copyright © 2018-2023 BrainKart.com; All Rights Reserved. Developed by Therithal info, Chennai.