⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 streamfile.java

📁 java nio 编程一个实例子.服务端程序
💻 JAVA
字号:
package com.ronsoft.books.nio.channels;import java.nio.ByteBuffer;import java.nio.channels.FileChannel;import java.io.FileInputStream;/** * Test behavior of FileChannel on stream devices. * Per MR at Sun, this is officially not supported. * FileChannel should not work with non-files, it's * a "hole in the spec" and will be closed in the * future. * * @author Ron Hitchens * @version $Id: StreamFile.java,v 1.2 2002/05/20 07:24:29 ron Exp $ */public class StreamFile{	public static void main (String [] argv)		throws Exception	{		String name = "/dev/tty";		if (argv.length > 0) {			name = argv [0];		}		FileInputStream fis = new FileInputStream (name);		FileChannel channel = fis.getChannel();		try {			System.out.println ("position=" + channel.position());			System.out.println ("Attempting seek 100");			channel.position (100);			System.out.println ("position=" + channel.position());			System.out.println ("Attempting seek 10");			channel.position (10);			System.out.println ("position=" + channel.position());		} catch (Exception e) {			System.out.println ("Caught: " + e);		}		try {			System.out.println ("Attempting truncate");			channel.truncate (100);		} catch (Exception e) {			System.out.println ("Caught: " + e);		}		try {			System.out.println ("Attempting force");			channel.force (true);		} catch (Exception e) {			System.out.println ("Caught: " + e);		}		try {			System.out.println ("Attempting size");			long size = channel.size();			System.out.println ("size=" + size);		} catch (Exception e) {			System.out.println ("Caught: " + e);		}		try {			ByteBuffer bb = ByteBuffer.allocate (10);			System.out.println ("Attempting rel read");			int bytes = channel.read (bb);			System.out.println ("read=" + bytes);			System.out.println ("Attempting abs read");			bb.clear();			bytes = channel.read (bb, 100);			System.out.println ("read=" + bytes);		} catch (Exception e) {			System.out.println ("Caught: " + e);		}	}}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -