Java IO
2019-03-07 10:52:05 0 举报
AI智能生成
Java io和Nio,根据源码和相关博客总结
作者其他创作
大纲/内容
io
InputStream/OutputStream/Reader/Writer是阻塞式,底层的read()、writer()方法都用了synchronized
File类
File path = new File("文件路径");
File类实际是文件路径类
String[] path.list();
返回此抽象路径表示的目录中的<b><font color="#f15a23">目录和文件</font></b>
字节流
abstract class<br>InputStream
不包装InputStream
ByteArrayInputStream
构造器传入byte[]
字段
protected byte buf[];
protected int pos;
方法
int read()——返回读的下一个byte;流的末尾返回-1
FileInputStream
构造器传入File 文件或String 文件名
方法
int read()——返回读的下一个byte;文件末尾返回-1
int read(byte b[])——返回读入b[]中的byte总数;文件末尾返回-1
可包装InputStream<br>FilterInputStream<br>
格式化输入<br>DataInputStream
构造器传入InputStream
方法
int read(byte b[])——返回读入b[]中的byte总数
char readChar()——连读2byte,左移拼出char返回
int readInt()——连读4byte,左移拼出int返回
long readLong()——连读8byte,左移拼出long返回
缓存输入<br>BufferedInputStream
构造器传入InputStream,可指定buf数组大小或默认8192
思想:每次往buf数组装n个,count=pos+n,但逐字节返回;<br>每次多读一些到内存中,缓冲显著增加IO操作性能
字段
volatile byte buf[];
protected int pos;
protected int count;
static int DEFAULT_BUFFER_SIZE = 8192
static int MAX_BUFFER_SIZE=Integer.MAX_VALUE - 8
方法
synchronized int read()——返回当前pos位置处的byte,pos++
abstract class<br>OutputStream
不包装OutputStream
ByteArrayOutputStream
构造器,可指定buf数组大小或默认32
字段
protected byte buf[];
protected int count;
方法
void write(int b)——将b写进buf,count++
void write(byte b[], int off, int len)——将b写进buf,count+=len
byte[] toByteArray()——拿到写进byte[]数组的内容
String toString()——拿到写进byte[]数组的内容
FileOutputStream
构造器传入File 文件或String 文件名
方法
void write(int b)
void write(byte[] b)
可包装OutputStream<br>FilterOutputStream
DataOutputStream
BufferedOutputStream
PrintStream
字符流
abstract class<br>Reader
不包装Reader
CharArrayReader
字节流转为字符流<br>InputStreamReader
FileReader
构造器传入InputStream
构造器传入File 文件或String 文件名包装成FileInputStream
StringReader
构造器传入String
字段
private String str;
private int length;
方法
int read()——return str.charAt(next++);
可包装Reader
BufferedReader
构造器传入Reader,可指定cb数组的大小或默认8192
字段
private char cb[];
private static int defaultCharBufferSize = 8192;
方法
int read()——返回单个char(0~65535);流的末尾返回-1
String readLine()——返回一行(回车\n或换行\r表EOF)
abstract class<br>Writer
不包装Writer
CharArrayWriter
字节流转为字符流<br>OutputStreamWriter
FileWriter
构造器传入OutputStream
构造器传入File 文件或String 文件名包装成FileInputStream
StringWriter
构造器设定StringBuffer buf的size或默认
字段
private StringBuffer buf;
方法
void write(char cbuf[], int off, int len)——将内容append到buf
void write(String str)——将内容append到buf
StringBuffer getBuffer()——拿到写进StringBuffer的内容
String toString()————拿到写进StringBuffer的内容
可包装Writer
BufferedWriter
PrintWriter
nio
Channel
FileChannel<br>只有阻塞模式
创建:getChannel()方法
FileInputStream
读文件,构造器传入File 文件或String 文件名
FileOutputStream
写文件,构造器传入File或String文件名
RandomAccessFile
读写文件,构造器传入File或String文件名,String 读/写模式
方法
读文件,写入ByteBuffer
inchannel.read(buf)
读ByteBuffer,写入文件
outchannal.write(buf)
scatter,将读取的数据写入多个buffer
inchannel.read(ByteBuffer[] dests)
gather,将多个buffer的数据写入同一个Channel
outchannel.write(ByteBuffer[] dsts)
transferFrom(ReadableByteChannel src,
long position, long count)
long transferTo(long position, long count,
WritableByteChannel target)
SelectableChannel<br>有非阻塞模式
DatagramChannel
SocketChannel
ServerSocketChannel
abstract class Buffer
子类
abstract class ByteBuffer
创建
创建HeapByteBuffer对象
特点
这种缓冲区是分配在Java虚拟机的堆上,直接由JVM负责垃圾回收
heapbuffer对象写入channal,需中转到内核缓冲区(创建临时directbuffer)——速度慢
static ByteBuffer allocate(int capacity)
static ByteBuffer wrap(byte[] array)
创建DirectByteBuffer对象
特点
通过JNI在Java的虚拟机外的内存中分配了一块缓冲区
创建、释放代价高,但速度快
static ByteBuffer allocateDirect(int capacity)
读写
写Buffer
buf.put(127)
读Buffer
byte tmp = buf.get()
方法
Buffer compact()
未读完继续写:清除已经读过的数据,任何未读的数据都被移到<br>缓冲区的起始处,新写入的数据将放到缓冲区未读数据的后面。
CharBuffer
DoubleBuffer
...
字段
约束:mark<=position<=limit<=capacity
mark
-1,丢弃状态
Buffer mark()——将mark设置为pos的值
limit
表示之前写进了多少个byte、char...
方法
Buffer flip()
准备读:lim置为之前写到的pos处,pos置0,mark置-1
Buffer rewind()
重读:pos置0,mark置-1
Buffer clear()
准备写:清空缓冲区,pos置0,lim置cap,mark置-1
Buffer reset()
重置:pos置mark
Buffer mark()
标记:mark置pos
Selectors
意义:使用Selector能够处理多个通道
创建:Selector selector = Selector.open();
注册通道:SelectionKey key = channel.register(selector, 感兴趣的事件的int);
读就绪OP_READ=1<<0
写就绪OP_WRITE=1<<2
连接就绪OP_CONNECT=1<<3
接收就绪OP_ACCEPT=1<<4
就绪通道数:上次调用select()方法后变成就绪状态的通道数
int select()——阻塞到至少有一个通道在注册的事件上就绪
int select(long timeout)——最长会阻塞timeout毫秒
int selectNow()——不会阻塞,不管什么通道就绪都立刻返回
就绪通道相应的Selectionkey集合:Set<SelectionKey> selectedKeys()
wakeUp()、close()
SelectionKey
创建:SelectionKey key = channel.register(selector, 感兴趣的事件的int);
interest集合:感兴趣事件的集合,int interestSet = selectionKey.interestOps(); <br>
ready集合:就绪事件的集合,int readySet = selectionKey.readyOps();
Channel channel = selectionKey.channel(); <br>
Selector selector = selectionKey.selector();
附加对象
selectionKey.attach(theObject);
Object attachedObj = selectionKey.attachment(); <br>
0 条评论
下一页