import java.nio.*; import java.nio.channels.*; import java.io.*; public class ChannelTest4 { private String[] texts = {"ABCDEFGHIJKLMNOPQRSTUVWXYZ", "abcdefghijklmnopqrstuvwxyz", "0123456789", "あいうえお"}; public ChannelTest4(String filename) { write(filename); read(filename); } public void write(String filename){ try{ FileOutputStream stream = new FileOutputStream(filename); FileChannel channel = stream.getChannel(); ByteBuffer[] buffers = new ByteBuffer[texts.length]; for(int i = 0 ; i < texts.length ; i++){ buffers[i] = ByteBuffer.wrap(texts[i].getBytes()); } channel.write(buffers, 1, 2); channel.close(); }catch(FileNotFoundException ex){ ex.printStackTrace(); }catch(IOException ex){ ex.printStackTrace(); } } public void read(String filename){ try{ FileInputStream stream = new FileInputStream(filename); FileChannel channel = stream.getChannel(); ByteBuffer[] buffers = new ByteBuffer[10]; for (int i = 0 ; i < 10 ; i++){ buffers[i] = ByteBuffer.allocate(10); } channel.read(buffers, 2, 3); for (int i = 0 ; i < 10 ; i++) { buffers[i].clear(); byte[] bytes = new byte[buffers[i].capacity()]; buffers[i].get(bytes); System.out.println("buf[" + i + "]: " + new String(bytes)); } channel.close(); }catch(FileNotFoundException ex){ ex.printStackTrace(); }catch(IOException ex){ ex.printStackTrace(); } } public static void main(String[] args){ new ChannelTest4(args[0]); } }