|
Channels in Java
An Introduction |
|
Prof. David Bernstein |
| Computer Science Department |
| bernstdh@jmu.edu |
import java.io.*;
import java.nio.*;
import java.nio.channels.*;
import java.nio.charset.*;
import java.nio.file.*;
/**
* An application that uses a SeekableByteChannel to read some files
* and display them on the console.
*
* @author Prof. David Bernstein, James Madison University
* @version 1.0
*/
public class ShowWithChannel
{
/**
* The entry point of the application.
*
* @param args The command line arguments (containing the file names)
*/
public static void main(String[] args)
{
for (int i=0; i<args.length; i++)
{
Path path = Paths.get(args[i]);
// Use a try-with-resources statement to ensure that the Channel
// is closed regardless of what exceptions are thrown
try (SeekableByteChannel channel = Files.newByteChannel(path))
{
String encoding = System.getProperty("file.encoding");
Charset charset = Charset.forName(encoding);
// Create a ByteBuffer (with capacity 10)
ByteBuffer buffer = ByteBuffer.allocate(10);
// Iteratively attempt to read bytes into the buffer
// (which changes position)
while (channel.read(buffer) > 0)
{
// Reset the position to 0 and discard the mark
// (so that it can be decoded)
buffer.rewind();
// Convert the ByteBuffer to a CharBuffer
// (which changes position to limit)
CharBuffer chars = charset.decode(buffer);
// Print the CharBuffer
System.out.print(chars);
// Set limit to position and position to 0
// (so that the buffer can be used for input again)
buffer.flip();
}
}
catch (IOException ioe)
{
System.out.printf("Couldn't read %s\n", args[i]);
}
}
}
}
import java.nio.*;
import java.nio.channels.*;
import java.nio.file.*;
import java.io.*;
import java.util.*;
/**
* An application that appends the command line arguments to
* a "history" file.
*
* @author Prof. David Bernstein, James Madison University
* @version 1.0
*/
public class AppendToHistory
{
/**
* The entry point of the application.
*
* @param args The command line arguments to record
*/
public static void main(String[] args)
{
Set<OpenOption> options = new HashSet<OpenOption>();
options.add(java.nio.file.StandardOpenOption.APPEND);
options.add(java.nio.file.StandardOpenOption.CREATE);
Path path = Paths.get("./history.txt");
// Use a try-with-resources statement to ensure that the Channel
// is closed regardless of what exceptions are thrown
try (SeekableByteChannel channel = Files.newByteChannel(path, options))
{
for (int i=0; i<args.length; i++)
{
byte[] data = args[i].getBytes();
ByteBuffer buffer = ByteBuffer.wrap(data);
channel.write(buffer);
}
}
catch (IOException ioe)
{
System.out.printf("Unable to open history.txt\n");
}
}
}
Files.readAllBytes(Path) returns a
byte[]
Files.readAllLines(Path) returns a
List<String>
Files.write(Path, byte[])
import java.io.*;
import java.nio.file.*;
import java.util.*;
/**
* An application that uses convenience methods in Java's "New I/O"
* package to read some files and display them on the console.
*
* @author Prof. David Bernstein, James Madison University
* @version 1.0
*/
public class ShowWithConvenienceMethods
{
/**
* The entry point of the application.
*
* @param args The command line arguments (containing the file names)
*/
public static void main(String[] args)
{
for (int i=0; i<args.length; i++)
{
try
{
List<String> lines = Files.readAllLines(Paths.get(args[i]));
for (String line: lines)
{
System.out.println(line);
}
}
catch (IOException ioe)
{
System.out.printf("Couldn't read %s\n", args[i]);
}
}
}
}