Thinking in Java第三版书中的源代码

源代码在线查看: getchannel.java

软件大小: 406 K
上传用户: zp380747972
关键词: Thinking Java in 源代码
下载地址: 免注册下载 普通下载 VIP

相关代码

				//: c12:GetChannel.java
				// Getting channels from streams
				// {Clean: data.txt}
				// From 'Thinking in Java, 3rd ed.' (c) Bruce Eckel 2002
				// www.BruceEckel.com. See copyright notice in CopyRight.txt.
				import java.io.*;
				import java.nio.*;
				import java.nio.channels.*;
				
				public class GetChannel {
				  private static final int BSIZE = 1024;
				  public static void main(String[] args) throws Exception {
				    // Write a file:
				    FileChannel fc =
				      new FileOutputStream("data.txt").getChannel();
				    fc.write(ByteBuffer.wrap("Some text ".getBytes()));
				    fc.close();
				    // Add to the end of the file:
				    fc =
				      new RandomAccessFile("data.txt", "rw").getChannel();
				    fc.position(fc.size()); // Move to the end
				    fc.write(ByteBuffer.wrap("Some more".getBytes()));
				    fc.close();
				    // Read the file:
				    fc = new FileInputStream("data.txt").getChannel();
				    ByteBuffer buff = ByteBuffer.allocate(BSIZE);
				    fc.read(buff);
				    buff.flip();
				    while(buff.hasRemaining())
				      System.out.print((char)buff.get());    
				  }
				} ///:~			

相关资源