Java 7新I/O特性解析
Java 7提供了一个新API访问文件系统,但除此之外,JSR 203(NIO.2)还包含其它很多新特性,这个新版本的确新增了很多改善I/O编程的类,本文将会介绍下面的新特性:
· SeekableByteChannel:随机访问通道;
· MulticastChannel:允许IP多播的通道;
· NetworkChannel:新的网络通道超级接口;
· 异步I/O API:新的API使I/O操作可以异步进行。
SeekableByteChannel
首先,Java 7包括新的ByteChannel – SeekableByteChannel,这个通道维护当前的位置,你可以读写该位置,并允许随机访问。使用这个类型的通道,你可以添加多个线程读/写在 不同位置相同的线程。
SeekableByteChannel channel1 = Paths.get("Path to file").newByteChannel();//Simply READ
SeekableByteChannel channel2 = Paths.get("Path to file").newByteChannel(StandardOpenOption.READ, StandardOpenOption.WRITE);//READ and WRITE
你可 以使用下面这些方法操作位置和通道的大小。
· long position():返回目前的位置;
· long size():返回通道连接实体的当前大小,如通道连接的文件大小;
· position(long newPosition):移动当前位置到某个地方;
· truncate(long size):根据给定大小截断实体。
position()和truncate()方法简单地返回当前通道,允许链式调用。
现在FileChannel实现了新的接口,使 用所有FileChannel你都可以实现随机访问,当然你可以用它读取一个文件:
SeekableByteChannel channel = null;
try {
channel = Paths.get("Path to file").newByteChannel(StandardOpenOption.READ);
ByteBuffer buf = ByteBuffer.allocate(4096);
System.out.println("File size: " + channel.size());
String encoding = System.getProperty("file.encoding");
while (channel.read(buf) > 0) {
buf.rewind();
byte[] bytearr = new byte[bytebuff.remaining()];
buf.get(bytearray);
System.out.print(new String(bytearray));
buf.flip();
System.out.println("Current position : " + channel.position());
}
} catch (IOException e) {
System.out.println("Expection when reading : " + e.g
相关文档:
在网上看到很多人都在为Delphi是否支持C++/Java中的静态变量及静态方法的功能而困扰,其实这在Delphi中是很简单的.
静态方法就是使用Class Function. 静态变量则使用单元局部变量来实现. 代码如下:
//=========================================
// StaticVarTestU - 定义了可以计数的类,通过类方法即可访问此类的对象数 ......
1、Java修饰符 Java 访问权限\
类成员变量、函数修饰符 本类 无继承关系
同一包 无继承关系
不同包 继承关系(子类)
同一包 继 ......
package com.tienway.util;
import java.util.ArrayList;
public class StringUtil
{
public static String ContextType1ToType2(String Type1Context)
{
if (Type1Context== null || Type1Context.length() == 0)
{
return "";
}
char[] c = Type1Con ......
以往要使用Java对时间日期进行操作,可能会用到以下的一些类:
Date and its subclasses :
java.util.Date
java.sql.Date
java.sql.Timestamp
The calendar and time zone classes :
java.util.Calendar
java.util.GregorianCalendar
java.util.TimeZone
java.util.SimpleTimeZone
(for use with the Gregorian ......
package test;
import java.util.ArrayList;
import java.util.List;
import org.nuxeo.common.xmap.annotation.XNode;
import org.nuxeo.common.xmap.annotation.XNodeList;
import org.nuxeo.common.xmap.annotation.XObject;
/**
* Book 实体对象,此处用XMap注解
* @author Administra ......