Java的I/O操作
这是我参与新手入门的第二篇文章
本文将介绍Java中的I/O常用操作
File类
File
类的对象主要用来获取文件本身的一些信息,如文件所在的目录,文件的长度,文件的读写权限等主要介绍
File
类中常用方法:
构造方法:
File(String path) File(String parent, String child) File(File parent, String child) File(URI uri) 复制代码
判断函数
boolean exists() boolean isAbsolute() boolean isDirectory() boolean isFile() boolean isHidden() boolean canRead() boolean canWrite() boolean canExecute() 复制代码
创建和删除
boolean createNewFile() boolean mkdir() boolean mkdirs() boolean delete() 复制代码
获取文件属性
long file.length() long file.getFreeSpace() long file.getTotalSpace() long file.getUsableSpace() File getAbsoluteFile(); String getAbsolutePath(); String getParent(); File getParentFile(); String getName(); String getPath(); long lastModified(); 复制代码
获取目录中的文件名和文件夹
//返回字符串数组,数组中为当前抽象路径名下的文件名和目录名 String[] list() //返回File数组,数组中为当前抽象路径名下的文件和目录 File[] listFiles() //列出盘符 static File[] listRoots() 复制代码
IO流的分类
IO流共涉及40多个类,但都是从以下四个抽象类中派生出来的
- **InputStream/Reader:**所有输入流的父类,前者是字节输入流,后者是字符输入流
- **OutputStream/Writer:**所有输出流的父类,前者是字节输出流,后者是字符输出流
因为文件中有时会有中文汉字等字符存在,这些字符不同字符集处理会是不同字节,如果利用字节流进行读取会产生错误和不便,因此要利用此类字符类来专门读取字符文件
按操作方式分类如下图
按操作对象分类如下图
![]()
文件操作
java针对文件的读写操作设置了一系列的流,其中主要有
FileInputStream
,FileOutputStream
,FileReader
,FileWriter
四种常用流
FileInputStream
/** * 构造方法常用两种,参数为String类型的构造方法内部调用File类型的构造方法 */ public FileInputStream(File file) throws FileNotFoundException public FileInputStream(String name) throws FileNotFoundException /** * 常用API */ //从输入流中读取一个字节返回int型变量,若到达文件末尾,则返回-1 public int read() throws IOException //从输入流中读取最多b.length个字节到字节数组中,返回读入缓冲区的总字节数,若到达文件末尾,则返回-1 public int read(byte b[]) throws IOException //从输入流中读取最多len个字节到字节数组中,从off位置开始存储字节,返回读入缓冲区的总字节数,若到达文件末尾,则返回-1 public int read(byte b[], int off, int len) throws IOException //关闭流 public void close() throws IOException 复制代码
FileOutputStream
/** * 构造方法 * 文件存在但是是目录时 或者 文件不存在且无法创建时会抛出FileNotFoundException */ public FileOutputStream(File file) throws FileNotFoundException public FileOutputStream(String name) throws FileNotFoundException //参数append为真,则从文件末尾写入字节,否则覆盖内容。 public FileOutputStream(File file, boolean append) throws FileNotFoundException /** * 常用API */ public void write(int b) throws IOException public void write(byte b[]) throws IOException public void write(byte b[], int off, int len) throws IOException //关闭流 public void close() throws IOException 复制代码
FileReader
FileReader
继承自InputStreamReader
,InputStreamReader
中的方法基本都是调用StreamDecoder
类的方法
/** * 构造方法 * 本质都利用File对象创建FileInputStream流对象调用了父类构造方法 */ public FileReader(String fileName) throws FileNotFoundException public FileReader(File file) throws FileNotFoundException /** * 常用API * 调用InputStreamReader的方法 */ public int read() throws IOException //cbuf数组是目标数组 public int read(char[] cbuf) throws IOException public int read(char[] cbuf, int off, int len) throws IOException 复制代码
FileWriter
FileWriter
继承自OutputStreamReader
,OutputStreamReader
中的方法基本都是调用StreamEncoder
类的方法
/** * 构造方法 */ public FileWriter(File file) public FileWriter(String name) public FileWriter(File file, boolean append) public FileWriter(String name, boolean append) /** * 常用API */ public void write(String str) throws IOException //ASCII码 public void write(int c) throws IOException public void write(char[] cbuf) throws IOException public void write(String str, int off, int len) throws IOException public void write(char[] cbuf, int off, int len) throws IOException 复制代码
缓冲操作
缓冲流是对四个基本字节流的的增强,且都是在四个基本流基础上建立。
缓冲流的基本原理是在创建对象时,会创建一个内置默认大小的缓冲区数组,通过缓冲区读写,减少系统IO次数(访问硬盘资源),提高读写效率
字节缓冲流
BufferedInputStream
/** * 构造方法 */ public BufferedInputStream(InputStream in) public BufferedInputStream(InputStream in, int size) /** * 常用API,基本来自父类类 */ //从输入流中读取一个字节返回int型变量,若到达文件末尾,则返回-1 public int read() throws IOException //从输入流中读取最多b.length个字节到字节数组中,返回读入缓冲区的总字节数,若到达文件末尾,则返回-1 public int read(byte b[]) throws IOException //从输入流中读取最多len个字节到字节数组中,从off位置开始存储字节,返回读入缓冲区的总字节数,若到达文件末尾,则返回-1 public int read(byte b[], int off, int len) throws IOException 复制代码
BufferedOutputStream
/** * 构造方法 */ public BufferedOutputStream(OutputStream out) public BufferedOutputStream(OutputStream out, int size) /** * 常用API,基本来自父类 */ public void write(int b) throws IOException public void write(byte b[]) throws IOException public void write(byte b[], int off, int len) throws IOException //将缓冲区的数据强制写出 public void flush() throws IOException //关闭输出释放资源,并将缓冲区的数据写出 public void close() throws IOException 复制代码
字符缓冲流
BufferedWriter
/** * 构造方法 */ public BufferedWriter(Writer out) public BufferedWriter(Writer out, int size) /** * 常用API,基本来自父类 */ public void write(int c) throws IOException public void write(char[] cbuf) throws IOException public void write(String str, int off, int len) throws IOException public void write(char[] cbuf, int off, int len) throws IOException //写入行分隔符(window中为:\r\n) public void newLine() throws IOException 复制代码
BufferedReader
/** * 构造方法 */ public BufferedReader(Reader in) public BufferedReader(Reader in, int sz) /** * 常用API,基本来自父类 */ public int read() throws IOException public int read(char[] cbuf) throws IOException public int read(char[] cbuf, int off, int len) throws IOException //读入一行 public String readLine() throws IOException 复制代码
转换流
InputStreamReader
InputStreamReade
r是从字节流到字符流的桥:它读取字节,并使用指定的charset
将其解码为字符 。它使用的字符集可以由名称指定,也可以被明确指定,或者可以接受平台的默认字符集。每个调用
InputStreamReader
的read()
方法之一可能会导致从底层字节输入流读取一个或多个字节。 为了使字节有效地转换为字符,可以从底层流读取比满足当前读取操作所需的更多字节。
/** * InputStreamReader设置字符集 */ public InputStreamReader(InputStream in, String charsetName) 复制代码
OutputStreamWriter
OutputStreamWriter是字符的桥梁流以字节流:向其写入的字符编码成使用指定的字节
charset
。它使用的字符集可以由名称指定,也可以被明确指定,或者可以接受平台的默认字符集。每次调用
write()
方法都会使编码转换器在给定字符上被调用。 所得到的字节在写入底层输出流之前累积在缓冲区中。 可以指定此缓冲区的大小,但是默认情况下它大部分用于大多数目的。 请注意,传递给write()
方法的字符不会缓冲。
/** * OutputStreamWriter设置字符集 */ public OutputStreamWriter(OutputStream out, String charsetName) 复制代码
序列化流
序列化和反序列化
- 序列化:把Java对象转换为字节序列的过程。
- 反序列化:把字节序列恢复为Java对象的过程。
在Java中,如果一个对象想要序列化,需要实现下面两个接口之一:
Serializable
接口Externalizable
接口
对象的序列化主要有两种用途:
- 把对象的字节序列永久地保存到硬盘上,通常存放在一个文件中;(持久化对象)
- 在网络上传送对象的字节序列。(网络传输对象)
序列化
ObjectOutputStream
/** * 构造方法 */ public ObjectOutputStream(OutputStream out) /** * 常用API */ //将指定对象写入ObjectOutputStream public void writeObject(Object ob) 复制代码
反序列化
ObjectInputStream
/** * 构造方法 */ public ObjectInputStream() 复制代码
打印流
PrintStream
为另一个输出流添加了功能,即能够方便地打印各种数据值的表示,且不会抛出IOException
异常
PrintStream
/** * 构造方法 */ //打印目的地为文件file public PrintStream(File file) public PrintStream(String fileName) //打印目的地为输出流out public PrintStream(OutputStream out) /** * 常用API * 注:PrintStream类继承自FileOutputStream,也可以使用来自父类的方法比如write() * 但是write方法打印时会按编码表打印 */ public void print(); public void println(); 复制代码