IO/NIO学习笔记
来源:互联网 发布:东阿南湖行知学校 编辑:程序博客网 时间:2024/06/07 16:34
基于字节流的IO:
- File。
- InputStream,FileInputStream。
- OutputStream,FileOutputStream。
输入流使用示例:
import java.io.File;import java.io.FileInputStream;import java.io.IOException;import java.io.InputStream;public class Main { public static void main(String[] args) throws IOException { File file = new File("c:" + File.separator + "mylog.log"); InputStream input = new FileInputStream(file); byte[] arr = new byte[(int) file.length()]; int t = 0; int i = 0; while ((t = input.read()) != -1) arr[i++] = (byte) t; System.out.println(new String(arr)); input.close(); }}
输出流使用示例:
import java.io.File;import java.io.FileOutputStream;import java.io.IOException;import java.io.OutputStream;public class Main { public static void main(String[] args) throws IOException { File file = new File("c:" + File.separator + "mylog.log"); OutputStream output = new FileOutputStream(file); String s = "hello world!"; byte[] arr = s.getBytes(); output.write(arr); output.close(); }}
基于字符流的IO:
- File。
- Reader,FileReader,BufferedReader。
- Writer,FileWriter,BufferedWriter。
输入流使用示例:
import java.io.BufferedReader;import java.io.File;import java.io.FileReader;import java.io.IOException;public class Main { public static void main(String[] args) throws IOException { File file = new File("c:" + File.separator + "mylog.log"); BufferedReader in = new BufferedReader(new FileReader(file)); StringBuilder sb = new StringBuilder(); String s = ""; while ((s = in.readLine()) != null) sb.append(s + "\n"); System.out.println(sb.toString()); in.close(); }}
输出流使用示例:
import java.io.BufferedWriter;import java.io.File;import java.io.FileWriter;import java.io.IOException;public class Main { public static void main(String[] args) throws IOException { File file = new File("c:" + File.separator + "mylog.log"); BufferedWriter out = new BufferedWriter(new FileWriter(file)); String str = "hello world"; out.write(str); out.close(); }}
标准输入System.in是没有被包装,未经加工过的InputStream,因此在使用时需要对其包装。
public final static InputStream in = nullInputStream(); //System类中的定义Scanner in = new Scanner(System.in);
标准输出System.out和标准错误System.err都被包装成了PrintStream,可以直接使用。
public final static PrintStream out = nullPrintStream();public final static PrintStream err = nullPrintStream();
nio:为了提高效率引入nio,使得io操作更接近于操作系统的io方式。
- 通道(类似煤矿的矿藏),FileChannel。
- 缓冲器(类似运煤的煤车),ByteBuffer。
旧io中FileInputStream,FileOutputStream和RandomAccessFile被修改以支持创建FileChannel对象。
使用方式:
import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.IOException;import java.nio.ByteBuffer;import java.nio.channels.FileChannel;public class Main { private static final int BSIZE = 1024; public static void main(String args[]) throws IOException { FileChannel fc = new FileOutputStream("E:/channel.txt").getChannel(); fc.write(ByteBuffer.wrap("hello world".getBytes())); fc.close(); fc = new FileInputStream("E:/channel.txt").getChannel(); ByteBuffer buff = ByteBuffer.allocate(BSIZE); fc.read(buff); buff.flip(); while (buff.hasRemaining()) System.out.print((char)buff.get()); fc.close(); }}
io与nio区别:
- io是基于流的,nio是基于缓冲区。
- io中包含字节流和字符流,nio包含缓冲区、通道和选择器。
- io是阻塞型io,nio是非阻塞型io。
0 0
- IO/NIO学习笔记
- Java NIO学习笔记一(IO VS NIO)
- JDK学习-IO/NIO
- Java 笔记:NIO新IO
- ThingInJava-IO笔记(NIO)
- java io与nio学习
- NIO学习笔记(1)之组成、NIO与IO区别、NIO和IO如何影响应用程序的设计和通道
- Java Nio学习笔记
- NIO学习笔记
- NIO学习笔记(一)
- NIO学习笔记1
- Java NIO学习笔记
- [网络]NIO学习笔记
- java NIO 学习笔记
- NIO学习笔记一
- NIO学习笔记二
- NIO学习笔记三
- NIO学习笔记四
- SQL Server 2008 还原备份文件
- java.lang.IllegalAccessError
- MPAndroidChart开源图表实现饼状图
- 使用存储过程和反射获取从多个数据表所需信息
- Spring架构详解之Core组件详解
- IO/NIO学习笔记
- 静态内部类、Handlers涉及的内存泄露
- Java分布式应用技术架构介绍
- SQL FOREIGN KEY 约束
- API接口编程数据类型转化
- 搭建springMVC框架
- 解决问题redis问题:ERR Client sent AUTH, but no password is set
- Action的动态方法调用
- java冒泡程序