FileOutputDemo.java 729 B

1234567891011121314151617181920212223242526
  1. package cn.hhj.nio.channel;
  2. import java.io.*;
  3. import java.nio.*;
  4. import java.nio.channels.*;
  5. public class FileOutputDemo {
  6. static private final byte message[] = { 83, 111, 109, 101, 32, 98, 121, 116, 101, 115, 46 };
  7. static public void main( String args[] ) throws Exception {
  8. FileOutputStream fout = new FileOutputStream( "E://test.txt" );
  9. FileChannel fc = fout.getChannel();
  10. ByteBuffer buffer = ByteBuffer.allocate( 1024 );
  11. for (int i=0; i<message.length; ++i) {
  12. buffer.put( message[i] );
  13. }
  14. buffer.flip();
  15. fc.write( buffer );
  16. fout.close();
  17. }
  18. }