1. 基本概念
IO:
Java对数据的操作是通过流的方式,IO流用来处理设备之间的数据传输,上传文件和下载文件,Java用于操作流的对象都在IO包中。
2. IO流的分类


3. 字节流
1. 字节流基类
- InputStream
InputStream:字节输入流基类,抽象类是表示字节输入流的所有类的超类。
| 常用方法: |
| |
| abstract int read() |
| |
| |
| int read(byte[] b) |
| |
| |
| int read(byte[] b, int off, int len) |
| |
| |
| long skip(long n) |
| |
| |
| void close() |
- OutputStream
OutputStream:字节输出流基类,抽象类是表示输出字节流的所有类的超类。
| 常用方法: |
| |
| void write(byte[] b) |
| |
| |
| void write(byte[] b, int off, int len) |
| |
| |
| abstract void write(int b) |
| |
| |
| void close() |
| |
| |
| void flush() |
2. 字节文件操作流
- FileInputStream
FileInputStream: 字节文件输入流,从文件系统中的某个文件中获得输入字节,用于读取注入图像数据之类的原始字节流。
| 构造方法: |
| |
| FileInputStream(File file) |
| |
| |
| FileInputStream(String name) |
| |
| 常用方法: 覆盖和重写了父类的常用方法 |
| import java.io.*; |
| |
| public class Java8Tester { |
| public static void main(String[] args) throws IOException { |
| |
| |
| |
| |
| InputStream inputStream = new FileInputStream(new File("G:\\test.txt")); |
| int i = 0; |
| |
| |
| while ((i = inputStream.read()) != -1) { |
| System.out.println(i); |
| System.out.println((char) i + " "); |
| } |
| |
| |
| inputStream.close(); |
| |
| |
| |
| InputStream inputStream2 = new FileInputStream(new File("G:\\test.txt")); |
| |
| |
| byte[] b = new byte[2]; |
| int i2 = 0; |
| |
| |
| while ((i2 = inputStream2.read(b)) != -1) { |
| System.out.println(new String(b, 0, i2) + " "); |
| } |
| |
| |
| inputStream2.close(); |
| } |
| } |
| 注: 一次读取一个字节数组,提高了操作效率,IO流使用完毕一定要关闭。 |
- FileOutputStream
FileOutputStream: 字节文件输出流是用于将数据写入到File, 从程序中写入到其他位置
| 构造方法: |
| |
| FileOutputStream(File file) |
| |
| |
| FileOutputStream(File file, boolean append) |
| |
| |
| FileOutputStream(String name) |
| |
| |
| FileOutputStream(String name, boolean append) |
| |
| 常用方法: 覆盖和重写了父类的常用方法 |
| import java.io.*; |
| |
| public class Java8Tester { |
| public static void main(String[] args) throws IOException { |
| OutputStream outputStream = new FileOutputStream(new File("test.txt")); |
| |
| outputStream.write("ABCD".getBytes()); |
| |
| |
| outputStream.close(); |
| |
| |
| OutputStream outputStream1 = new FileOutputStream("test.txt", true); |
| |
| outputStream1.write("\r\n".getBytes()); |
| |
| outputStream1.write("hello".getBytes()); |
| |
| outputStream1.close(); |
| |
| |
| |
| |
| |
| |
| } |
| } |
| |
| 注: |
| 输出的目的地文件不存在,则会自动创建,不指定盘符的话,默认创建在项目目录下; |
| 输出换行符是一定要写\r\n,不能只写\n,因为不同文本编辑器对换行符的识别存在差异性。 |
3. 字节缓冲流(高效流)
- BufferedInputStream
BufferedInputStream:字节缓冲输入流,提高了读取效率
| 构造方法: |
| |
| BufferedInputStream(InputStream in) |
| |
| |
| BufferedInputStream(InputStream in, int size) |
| |
| import java.io.*; |
| |
| public class Java8Tester { |
| public static void main(String[] args) throws IOException { |
| InputStream in = new FileInputStream("test.txt"); |
| |
| |
| BufferedInputStream bis = new BufferedInputStream(in); |
| byte[] bs = new byte[20]; |
| int len = 0; |
| while ((len = bis.read(bs)) != -1) { |
| System.out.println(new String(bs, 0, len)); |
| } |
| |
| bis.close(); |
| |
| |
| |
| |
| } |
| } |
- BufferedOutputStream
BufferedOutputStream:字节缓冲输出流,提高了写出效率
| 构造方法: |
| |
| BufferedOutputStream(OuputStream out) |
| |
| |
| BufferedOuputStrema(OutputStream out, int size) |
| |
| 常用方法: |
| |
| void write(byte[] b, int off, int len) |
| |
| |
| void write(int b) |
| |
| |
| void flush() |
| |
| |
| import java.io.*; |
| |
| public class Java8Tester { |
| public static void main(String[] args) throws IOException { |
| BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("test.txt")); |
| bos.write("from here".getBytes()); |
| |
| |
| bos.write("\r\n".getBytes()); |
| |
| |
| bos.write("Hello Android".getBytes()); |
| |
| |
| bos.flush(); |
| |
| |
| bos.close(); |
| |
| |
| |
| |
| |
| } |
| } |
4. 字符流
1. 字符流基类
| 常用方法: |
| |
| int read() |
| |
| |
| int read(char[] cbuf) |
| |
| |
| abstract int read(char[] cbuf, int off, int len) |
| |
| |
| long skip(long n) |
| |
| |
| abstract void close() |
| 常用方法: |
| |
| void write(char[] cbuf) |
| |
| |
| abstract void write(char[] cbuf, int off, int len) |
| |
| |
| void write(int c) |
| |
| |
| void write(String str) |
| |
| // 写入字符串的某一部分 |
| void write(String str, int off, int len) |
| |
| |
| Writer append(char c) |
| |
| |
| Writer append(CharSequence csq) |
| |
| |
| Writer append(CharSequence ceq, int start, int end) |
| |
| |
| abstract void close() |
| |
| |
| abstract void flush() |
2. 字符转换流
- InputStreamReader
InputStreamReader:字节流转字符流,它使用的字符集可以有名称指定或显示给定,否则将接收平台默认的字符集。
| 构造方法: |
| |
| InputStreamReader(InputStream in) |
| |
| |
| InputStreamReader(InputStream in, Charset cs) |
| |
| |
| InputStreamReader(InputStream in, CharsetDecoder dec) |
| |
| |
| InputStreamReader(InputStream in, String charsetName) |
| |
| 特有方法: |
| |
| String getEncoding() |
| |
| import java.io.*; |
| |
| public class Java8Tester { |
| public static void main(String[] args) throws Exception { |
| |
| InputStreamReader reader = new InputStreamReader(new FileInputStream("test.txt")); |
| |
| int len; |
| while ((len = reader.read()) != -1) { |
| System.out.println((char) len); |
| } |
| reader.close(); |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| InputStreamReader reader1 = new InputStreamReader(new FileInputStream("test.txt"), "utf-8"); |
| int len1; |
| while ((len1 = reader1.read()) != -1) { |
| System.out.println((char) len); |
| } |
| reader.close(); |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| } |
| } |
| 注:Idea默认使用GBK编码,test.txt文件所以是GBK编码,当指定utf-8编码时所以会乱码。 |
- OutputStreamWriter
OutputStreamWriter:字节流转字符流
| 构造方法: |
| |
| OutputStreamWriter(OutputStream out) |
| |
| |
| OutputStreamWriter(OutputStream out, Charset cs) |
| |
| |
| OutputStreamWriter(OutputStream out, CharsetEncoder enc) |
| |
| |
| OutputStreamWriter(OutputStream out, String charsetName) |
| |
| 特有方法: |
| |
| String getEncoding() |
3. 字符缓冲流(高效流)
- BufferedReader
BufferedReader:字符缓冲流,从字符输入流中读取文本,缓冲各个字符,从而实现字符、数组和行的高效读取
| 构造方法: |
| |
| BufferedReader(Reader in) |
| |
| |
| BufferedReader(Reader in, int sz) |
| |
| 特有方法: |
| |
| String readLine() |
| |
| |
| import java.io.*; |
| |
| public class Java8Tester { |
| public static void main(String[] args) throws Exception { |
| |
| BufferedReader reader = new BufferedReader( |
| new InputStreamReader( |
| new FileInputStream("test.txt") |
| ) |
| ); |
| String str; |
| |
| |
| while ((str = reader.readLine()) != null) { |
| System.out.println(str); |
| } |
| |
| |
| reader.close(); |
| } |
| } |
| |
| 结果: |
| 爱生活,爱拉芳 |
- BufferedWriter
BufferedWriter:字符缓冲流,将文本写入字符输出流,缓冲各个字符,从而提供单个字符、数组和字符串的高效写入。
| 构造方法: |
| |
| BufferedWriter(Writer out) |
| |
| |
| BufferedWriter(Writer out, int sz) |
| |
| 特有方法: |
| |
| void newLine() |
| FileReader: |
| InputStreamReader类的直接子类,用来读取字符文件的便捷类,使用默认字符编码。 |
| |
| FileWriter: |
| OutputStreamWriter类的直接子类,用来写入字符文件的便捷类,使用默认字符编码。 |
5. 高效流效率比对
读取C盘下的视频文件到项目中:文件大小为9.56MB
地址"C:\Users\19413\Pictures\Camera Roll\a.mp4"
读取方式一:
| import java.io.*; |
| |
| public class Java8Tester { |
| public static void main(String[] args) throws Exception { |
| FileInputStream inputStream = new FileInputStream("C:\\Users\\19413\\Pictures\\Camera Roll\\a.mp4"); |
| FileOutputStream outputStream = new FileOutputStream("a.mp4"); |
| int len; |
| |
| |
| long begin = System.currentTimeMillis(); |
| |
| |
| while ((len = inputStream.read()) != -1) { |
| outputStream.write(len); |
| } |
| |
| |
| System.out.println(System.currentTimeMillis() - begin); |
| |
| |
| inputStream.close(); |
| outputStream.close(); |
| } |
| } |
读取方式二:
| import java.io.*; |
| |
| public class Java8Tester { |
| public static void main(String[] args) throws Exception { |
| FileInputStream inputStream = new FileInputStream("C:\\Users\\19413\\Pictures\\Camera Roll\\a.mp4"); |
| FileOutputStream outputStream = new FileOutputStream("a.mp4"); |
| int len; |
| byte[] bs = new byte[1024]; |
| |
| |
| long begin = System.currentTimeMillis(); |
| |
| |
| while ((len = inputStream.read(bs)) != -1) { |
| outputStream.write(bs, 0, len); |
| } |
| |
| |
| System.out.println(System.currentTimeMillis() - begin); |
| |
| |
| inputStream.close(); |
| outputStream.close(); |
| } |
| } |
读取方式三:
| import java.io.*; |
| |
| public class JavaTest { |
| public static void main(String[] args) throws Exception { |
| FileInputStream inputStream = new FileInputStream("C:\\Users\\19413\\Pictures\\Camera Roll\\a.mp4"); |
| BufferedInputStream bis = new BufferedInputStream(inputStream); |
| FileOutputStream outputStream = new FileOutputStream("a.mp4"); |
| BufferedOutputStream bos = new BufferedOutputStream(outputStream); |
| |
| int len; |
| byte[] bs = new byte[1024]; |
| |
| |
| long begin = System.currentTimeMillis(); |
| |
| |
| while ((len = bis.read(bs)) != -1) { |
| bos.write(bs, 0, len); |
| } |
| |
| |
| System.out.println(System.currentTimeMillis() - begin); |
| |
| |
| bis.close(); |
| bos.close(); |
| } |
| } |
参考:https://www.cnblogs.com/shuaiguoguo/p/8883862.html
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 三行代码完成国际化适配,妙~啊~
· .NET Core 中如何实现缓存的预热?