I/O解决的是人和机器交互、机器间交互的问题输入流:InpudStream 和 Reader输出流;OutputStream 和 Writer 字节流 字符流字节流处理文件、图片、视频等二进制数据字符流处理文本数据
File
File file = new File("abc.txt");//获取文件boolean file.canExecute();//判断是否可以运行boolean file.canRead();//判断文件是否可以读boolean file.canWrite();//判断文件是否可以写boolean file.isHidden();//判断文件是否隐藏boolean file.isFile();//判断文件是否是文件boolean file.Directory();//判断文件是否是文件夹long file.length();//获取文件的大小long file.lastModefide();//获取文件最后的修改时间--要用SimpleDateFormat进行格式化long file.setLastModefide(time);//设置文件的最后修改时间 参数long类型===========================================boolean file.exists();//判断文件是否存在String file.getAbsolutePate();//获取文件的绝对路径String file.getName();//获取文件的名字boolean file.createNewFile();//创建文件boolean file.mkdir();//创建文件夹--外层文件夹必须存在boolean file.mkdirs();//创建文件夹--外层文件夹如果没有也会创建String file.getParent();//获取当前文件上层文件的名字File file.getParentFile();//获取文件上层文件的对象String[] file.list()//获取当前文件下所有的文件的名字File[] file.listFile();//获取当前文件下所有文件的对象
BIO
字节流
//读取文件FileInputStream inputStream = new FileInputStream(file);//创建输入流对象--参数file对象byte[] bytes = new byte[1024];//创建一个字节数组int read = inputStream.read(bytes);//把读到的内容装进数组 返回值为读取到的个数//注意:read方法也有一个带偏移的重载方法read(bytes,开始位置,结束位置)while (read!=-1){//-1是代表没有了//String的重载方法 第一个数组,第二个是从什么位置开始 第三个是到哪 String value = new String(bytes,0,read);//可以用String对象吧字节数组的内容输出出来 System.out.print(value); read = inputStream.read(bytes);}inputStream.available();//;流管道中有多少缓存信息inputStream.skip(long);//跳过几个字节读取
//写文件File file = new File("aaa.txt"); FileOutputStream outputStream = new FileOutputStream(file); FileOutputStream output = new FileOutputStream(file,true);//第二个参数设置是否覆盖前面的内容默认flase outputStream.write(97);//将ASCALL码对应的字符写入文件 byte[] bytes = {'a','c','d'};//创建byte数组 outputStream.write(bytes);//把数组中的内容写入文件中 String s = "abcdefg"; byte[] bytes1 = s.getBytes();//还可以通过定义字符串通过是getBytes()转成字节 outputStream.write(bytes1);//把数组中的内容写入文件中 outputStream.write(bytes,0,1);//可以添加偏移量 outputStream.flush();//将管道里面的数据推到文件中去
字符流
//读文件
File file = new File("abc.txt");
FileReader reader = new FileReader(file);
char[] chars = new char[1024];//字符型与字节型不同的地方是构建字符型数组
int reads = reader.read(chars);
while (reads!=-1){
String s = new String(chars,0,reads);
System.out.println(s);
reads=reader.read(chars);
}
//写文件
File file = new File("abc.txt");
FileWriter writer = new FileWriter(file);//也可以添加true参数设置文件可追加
writer.write("abcd哈哈");//可以直接写字符串
String s = "abc哈哈哈";
char[] chars = s.toCharArray();//可以写入char类型数组和可以把字符串转出chars数组写入
writer.write(chars);//可以带偏移量
writer.flush();
缓冲字节流字符流
//读文件
File file = new File("abc.txt");
FileInputStream inputStream = new FileInputStream(file);
BufferedInputStream bufferedStream = new BufferedInputStream(inputStream);
//缓冲的字节流方法和普通的方法是一样的。但是缓冲流的参数是普通的流
//写文件
FileOutputStream outputStream = new FileOutputStream(file);
BufferedOutputStream bufferedStream=new BufferedOutputStream(outputStream);
//BufferedReader/BufferedWriter同
BufferedReader多了个可以读一行的方法readLine()返回值是String 判断读到没有用是否为NULL;
PrintWriter
PrintWriter也是高级流 但是参数可以是 字节流 也可以是 字符流 BufferWriter参数只可以是字符流
Socket编程中,尽量用PrintWriter取代BufferedWriter,下面是PrintWriter的优点:
1. PrintWriter的print、println方法可以接受任意类型的参数,而BufferedWriter的write方法只能接受字符、字符数组和字符串;
2. PrintWriter的println方法自动添加换行,BufferedWriter需要显示调用newLine方法;
3. PrintWriter的方法不会抛异常,若关心异常,需要调用checkError方法看是否有异常发生;
4. PrintWriter构造方法可指定参数,实现自动刷新缓存(autoflush);
5. PrintWriter的构造方法更广。
在使用BufferedReader中的readLine方法接收BufferedWriter中的字符流时,由于readLine是在读取到换行符的时候才将整行字符返回,所以BufferedWriter方法在录入一段字符后要使用newLine方法进行一次换行操作,然后再把字符流刷出去。而PrintWriter由于可以开启自动刷新,并且其中的println方法自带换行操作。所以代码实现起来要比BufferedWriter简单一些。
NIO
BIO:同步阻塞
NIO:同步非阻塞
AIO:异步非阻塞
同步和异步:关注的是消息通知的机制
阻塞和非阻塞:关注的是等待消息过程中的状态
NIO的三大核心要素
通道(Channel):从节点读取数据的通道类比Stream但是是双向的即可输入也可输出
缓冲区(Buffer):读取和存储数据的byte数组,在通道直接传输数据使用
选择器(Selector):检测多个通道上是否有IO事件发生,如果没有继续检测
BIO和NIO对比
BIO是面向流的
NIO是面向块的(缓冲区)的
NIO将文件或文件中的一段区域映射到内存中可以向访问内存一样访问文件
Channel
文件管道的数据 FileChannel
线程间通信的管道Pipe.SinkChannel
Pipe.SourceChannel
TCP网络通信的管道 ServerSocketChannel
SocketChannel
UDP网络通信的管道:DatagramChannel
Buffer
底层是数组结构,可以进行get和set
Buffer获取必须通过allocate(int capacity)方法进行分配 创建容量为n的对象
capacity 总体容量
limit 存储容量大小 是可读写和不可读写的界限
position 已读容量的大小 是已读和未读区域的界限
使用原理:
1.初始化 给定总容量 position=0 limit=capacity
2.当使用put方法存入数据时,通过position来记录存储容量的变化,position不断后移,直到存储完成(写完成)
3.写完成后需要通过filp方法刷新 刷新的是limit和position的值 limit=position position=0
4.读数据,通过position来记录已读容量的变化直到读完成,通过clear方法position=0 limit=capacity
Selector
三要素:Selector 选择器
SelectableChannel 可选择的通道
SelectionKey 选择键
选择器和通道的关系 是将通道注册进选择器中,前提是通道可以选择,注册关系对应的是选择键SelectionKey所代表的具体操作(连接 接收 读/写)
本质上selector是监听器,监听的是通道是否有我们关系的事件,使用SelectionKey代表具体的事件,在确保可选择的情况下,将通道注册进选择器中,此时Selector维护是的通道和事件的关联关系
Selector 被注册的通道的集合以及他们的状态
SelectableChannel 是一个抽象类,提供了通道可被选择需要实现的api
FileChannel 不可被选择 Socket相关的通道都是可被选择的
SelectionKey 封装了要监听的事件 连接 接收 读 写
Selector关心通道要处理哪些事件
当事件触发时通道要处理哪些事件
NIO的文件写操作
FileOutputStream os = new FileOutputStream(file);
//通过Stream获取通道
FileChannel channel = os.getChannel();
//获取缓存区:通过ByBuffer类的allocate分配一个缓冲区
ByteBuffer byteBuffer = ByteBuffer.allocate(1024);
String str = "aaaabbbcccdddeefff";
byteBuffer.put(str.getBytes());
//写完成 通过filp刷新
byteBuffer.flip();
//写入文件
channel.write(byteBuffer);
channel.close();
os.close();
NIO的文件读操作
FileInputStream is = new FileInputStream(file);
FileChannel fileChannel = is.getChannel();
ByteBuffer allocate = ByteBuffer.allocate(1024);
int read = fileChannel.read(allocate);
while (read != -1) {
System.out.println(new String(allocate.array(),0,read));//allocate.array()转成byte数组
read = fileChannel.read(allocate);
}
allocate.clear();
fileChannel.close();
is.close();