Java从零开始 基础(四)IO编程
流的概念和分类
什么是流:内存与存储设备(硬盘)之间传输数据的通道
-
按流向分类:
- 输入流:将硬盘内容读到内存
- 输出流:将内存内容读到硬盘
-
按单位分类:
- 字节流:以字节为单位,可以读写所有数据
- 字符流:以字符为单位,只能读写文本数据
-
按功能分类:
- 节点流:具有实际传输数据的读写功能
- 过滤流:在节点流的基础之上增强功能
字节流
把数据拆成1个字节来传输,所以不能传中文(UTF-8的中文占3个字节)
抽象类
InputStream:字节输入流
public abstract int read() throws IOException
// 从输入流中读取数据的下一个字节。返回 0 到 255 范围内的 int 字节值。如果因为已经到达流末尾而没有可用的字节,则返回值 -1。在输入数据可用、检测到流末尾或者抛出异常前,此方法一直阻塞。子类必须提供此方法的一个实现。
// 返回:下一个数据字节;如果到达流的末尾,则返回 -1。
public int read(byte[] b) throws IOException
// 从输入流中读取一定数量的字节,并将其存储在缓冲区数组 b 中。以整数形式返回实际读取的字节数。在输入数据可用、检测到文件末尾或者抛出异常前,此方法一直阻塞。
// 如果 b 的长度为 0,则不读取任何字节并返回 0;否则,尝试读取至少一个字节。如果因为流位于文件末尾而没有可用的字节,则返回值 -1;否则,至少读取一个字节并将其存储在 b 中。
// 将读取的第一个字节存储在元素 b[0] 中,下一个存储在 b[1] 中,依次类推。读取的字节数最多等于 b 的长度。设 k 为实际读取的字节数;这些字节将存储在 b[0] 到 b[k-1] 的元素中,不影响 b[k] 到 b[b.length-1] 的元素。
// 类 InputStream 的 read(b) 方法的效果等同于: read(b, 0, b.length)
// 参数:b - 存储读入数据的缓冲区。
// 返回:读入缓冲区的总字节数;如果因为已经到达流末尾而不再有数据可用,则返回 -1。
public int read(byte[] b, int off, int len) throws IOException
// 将输入流中最多 len 个数据字节读入 byte 数组。尝试读取 len 个字节,但读取的字节也可能小于该值。以整数形式返回实际读取的字节数。在输入数据可用、检测到流末尾或者抛出异常前,此方法一直阻塞。
// 如果 len 为 0,则不读取任何字节并返回 0;否则,尝试读取至少一个字节。如果因为流位于文件末尾而没有可用的字节,则返回值 -1;否则,至少读取一个字节并将其存储在 b 中。
// 将读取的第一个字节存储在元素 b[off] 中,下一个存储在 b[off+1] 中,依次类推。读取的字节数最多等于 len。设 k 为实际读取的字节数;这些字节将存储在 b[off] 到 b[off+k-1] 的元素中,不影响 b[off+k] 到 b[off+len-1] 的元素。
// 在任何情况下,b[0] 到 b[off] 的元素以及 b[off+len] 到 b[b.length-1] 的元素都不会受到影响。
// 类 InputStream 的 read(b, off, len) 方法重复调用方法 read()。如果第一次这样的调用导致 IOException,则从对 read(b, off, len) 方法的调用中返回该异常。如果对 read() 的任何后续调用导致 IOException,则捕获该异常并将其视为到达文件末尾;到达该点时读取的字节存储在 b 中,并返回发生异常之前读取的字节数。在已读取输入数据 len 的请求数量、检测到文件结束标记、抛出异常前,此方法的默认实现将一直阻塞。建议子类提供此方法更为有效的实现。
// 参数:
// b - 读入数据的缓冲区。off - 数组 b 中将写入数据的初始偏移量。len - 要读取的最大字节数。
// 返回:读入缓冲区的总字节数;如果因为已到达流末尾而不再有数据可用,则返回 -1。
OutputStream:字节输出流
public abstract void write(int b) throws IOException
// 将指定的字节写入此输出流。 write 的常规协定是:向输出流写入一个字节。要写入的字节是参数 b 的八个低位。 b 的 24 个高位将被忽略。
// OutputStream 的子类必须提供此方法的实现。
// 参数:b - 字节
public void write(byte[] b) throws IOException
// 将 b.length 个字节从指定的 byte 数组写入此输出流。 write(b) 的常规协定是:应该与调用 write(b, 0, b.length) 的效果完全相同。
// 参数:b - 数据。
public void write(byte[] b, int off, int len) throws IOException
// 将指定 byte 数组中从偏移量 off 开始的 len 个字节写入此输出流。 write(b, off, len) 的常规协定是:将数组 b 中的某些字节按顺序写入输出流;元素 b[off] 是此操作写入的第一个字节, b[off+len-1] 是此操作写入的最后一个字节。
// OutputStream 的 write 方法对每个要写出的字节调用一个参数的 write 方法。建议子类重写此方法并提供更有效的实现。
// 如果 b 为 null,则抛出 NullPointerException。
// 如果 off 为负,或 len 为负,或者 off+len 大于数组 b 的长度,则抛出 IndexOutOfBoundsException。
// 参数:b - 数据。off - 数据中的初始偏移量。len - 要写入的字节数。
文件字节流
输入流 FileInputStream
继承自:InputStream
package io;
import java.io.FileInputStream;
import java.io.IOException;
public class TestStream {
public static void main(String[] args) throws IOException {
// 创建文件字节输出流,并指定文件路径
FileInputStream file = new FileInputStream("./aaa.txt");
// oneByte(file);
manyByte(file);
file.close();
}
private static void oneByte(FileInputStream file) throws IOException {
// 读取文件 一次读一个字节
int data = 0; // 创建一个int变量,来存文件内容的ASCII码,然后转为char输出
while ((data = file.read()) != -1) {
System.out.println(data + " | " + (char) data); // 打印出的是ASCII码, 强制转换为char
}
System.out.println("===============================");
}
private static void manyByte(FileInputStream file) throws IOException {
// 读取文件 一次读多个字节
byte[] buf = new byte[5]; // 创建一个数组,来存文件内容,然后输出
int count = 0;
while ((count = file.read(buf)) != -1) {
System.out.println(new String(buf, 0, count));
}
}
}
输出流 FileOutputStream
继承自:OutputStream
package io;
import java.io.FileOutputStream;
import java.io.IOException;
public class TestStream {
public static void main(String[] args) throws IOException {
// 创建文件字节输入流,并指定文件路径
FileOutputStream file = new FileOutputStream("./aaa.txt",true); // 加上true 写入不覆盖,文件不存在时会自动创建
// oneByte(file);
manyByte(file);
file.close();
}
private static void oneByte(FileOutputStream file) throws IOException {
// 写入,一次写一个字节
file.write(97); // 参数为ASCII码或char
System.out.println("===============================");
}
private static void manyByte(FileOutputStream file) throws IOException {
// 写入,一次写多个字节
String str = "床前明月光,疑是地上霜";
file.write(str.getBytes()); // getBytes() 获取字节码
}
}
复制文件
package io;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class CopyFile {
public static void main(String[] args) throws IOException {
// 创建文件输入流
FileInputStream file_i = new FileInputStream("1.png");
// 创建文件输出流
FileOutputStream file_o = new FileOutputStream("2.png");
// 一边读 一边写
byte[] buf = new byte[1024 * 4];
int count = 0;
while ((count = file_i.read(buf)) != -1) {
file_o.write(buf, 0, count);
}
// 关闭
file_i.close();
file_o.close();
}
}
字节缓冲流
- 缓冲流:BufferedInputStream / BufferedOutputStream;
- 提高I0效率,减少访问磁盘的次数;
- 数据存储在缓冲区中,flush是将缓存区的内容写入文件中,也可以直按close
读: BufferedInputStream
package io;
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.IOException;
public class BufferedStream {
public static void main(String[] args) throws IOException {
// 创建BufferedInputStream
FileInputStream fis = new FileInputStream("aaa.txt");
BufferedInputStream bis = new BufferedInputStream(fis); // 缓冲流 接收一个输入流对象
// 读取
int count = 0;
byte[] buf = new byte[10];
while ((count = bis.read(buf)) != -1) {
System.out.println(new String(buf, 0, count));
}
bis.close();
}
}
写: BufferedOutputStream
package io;
import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class BufferedStream {
public static void main(String[] args) throws IOException {
// 创建BufferedOutputStream
FileOutputStream fos = new FileOutputStream("bbb.txt", true);
BufferedOutputStream bos = new BufferedOutputStream(fos); // 缓冲流 接收一个输入流对象
// 读取
for (int i = 0; i < 10; i++) {
bos.write("hello world ".getBytes()); // 写入8K缓冲区
bos.flush(); // 刷新到硬盘
}
bos.close(); // 内部刷新(执行flush)
}
}
对象流
对象流:ObjectOutputStream/ObjectInputStream
- 增强了缓冲区功能
- 增强了读写8种基本数据类型和字符串功能
- 增强了读写对象的功能:
- readObject () 从流中读取一个对象
- write0b ject (Object obj) 向流中写入一个对象
- 使用流传输对象的过程称为序列化、反序列化
序列化
能被序列化的类,必须实现Serializable接口
先实现一个类
package io;
import java.io.Serializable;
public class Student implements Serializable { // 能被序列化的类,必须实现Serializable接口
private final static long serialVersionUID = -4185518774749092049L; // 序列化版本号id,作用是保证序列化的类和反序列化的类是同一个类
private String name;
private int age;
private transient int score; // 使用transient修饰的属性不会被序列化
public Student(String name, int age, int score) {
this.name = name;
this.age = age;
this.score = score;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
public int getScore() {
return score;
}
public void setName(String name) {
this.name = name;
}
public void setAge(int age) {
this.age = age;
}
public void setScore(int score) {
this.score = score;
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", age=" + age +
", score=" + score +
'}';
}
}
再将其序列化
package io;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
public class ObjectStream {
public static void main(String[] args) throws IOException {
Student tom = new Student("Tom", 18); // 实例化对象
// 创建对象流
FileOutputStream fos = new FileOutputStream("stu.bin"); // .bin 表示2进制文件
ObjectOutputStream oos = new ObjectOutputStream(fos);
// 序列化(写入操作)
oos.writeObject(tom);
// 关闭
oos.close();
}
}
反序列化
package io;
import java.io.*;
public class ObjectStream {
public static void main(String[] args) throws IOException, ClassNotFoundException {
// 创建对象流
FileInputStream fis = new FileInputStream("stu.bin"); // .bin 表示2进制文件
ObjectInputStream ois = new ObjectInputStream(fis);
// 反序列化(读取操作) 将序列化对象转为正常对象
Student tom = (Student)ois.readObject();
System.out.println(tom.toString());
// 关闭
ois.close();
}
}
注意事项
- 能被序列化的类,必须实现Serializable接口(对象类型的属性,所属类亦然)
- private final static long serialVersionUID = -4185518774749092049L; // 序列化版本号id,作用是保证序列化的类和反序列化的类是同一个类
- 使用transient修饰的属性,不会被序列化
- 静态属性不能被序列化
- 序列化多个对象,可借助集合(ArrayList)来实现
编码方式
- ISO-8859-1:收录除ASCII外,还包括西欧、希腊语、泰语、阿拉伯语、希伯来语对应的文字符号
- UTF-8:针对Unicode码表的可变长度字符编码(包含所有国家的常见字符)
- GB2312:简体中文
- GBK:简体中文、扩充
- BIG5:台湾,繁体中文
编码方式和解码方式不一致时,会出现乱码
字符流
抽象类
Reader:字符输入流
public int read() throws IOException
// 读取单个字符。在字符可用、发生 I/O 错误或者已到达流的末尾前,此方法一直阻塞。
// 用于支持高效的单字符输入的子类应重写此方法。
// 返回:作为整数读取的字符,范围在 0 到 65535 之间 ( 0x00-0xffff),如果已到达流的末尾,则返回 -1
public int read(char[] cbuf) throws IOException
// 将字符读入数组。在某个输入可用、发生 I/O 错误或者已到达流的末尾前,此方法一直阻塞。
// 参数:cbuf - 目标缓冲区
// 返回:读取的字符数,如果已到达流的末尾,则返回 -1
public abstract int read(char[] cbuf, int off, int len) throws IOException
// 将字符读入数组的某一部分。在某个输入可用、发生 I/O 错误或者到达流的末尾前,此方法一直阻塞。
// 参数:cbuf - 目标缓冲区、off - 开始存储字符处的偏移量、len - 要读取的最多字符数
// 返回:读取的字符数,如果已到达流的末尾,则返回 -1
Writer:字符输出流
public void write(int c) throws IOException
// 写入单个字符。要写入的字符包含在给定整数值的 16 个低位中,16 高位被忽略
// 用于支持高效单字符输出的子类应重写此方法
// 参数:c - 指定要写入字符的 int。
public void write(String str) throws IOException
// 写入字符串
// 参数:str - 要写入的字符串
public void write(String str, int off, int len) throws IOException
// 写入字符串的某一部分。
// 参数:str - 字符串、off - 相对初始写入字符的偏移量、len - 要写入的字符数
文件字符流
输入流 FileReader
public int read (char [] c) 从流中读取多个字符,将读到内容存入c数组,返回实际读到的字符数;如果达到文件的尾部,则返回 -1
package io.str;
import java.io.FileReader;
import java.io.IOException;
public class StrStream {
public static void main(String[] args) throws IOException {
// 创建FileReader 文件字符输入流
FileReader fr = new FileReader("aaa.txt");
// 读取
char[] buf = new char[1024];
int count = 0;
while ((count = fr.read(buf)) != -1) {
System.out.println(new String(buf, 0, count));
}
}
}
输出流 Filewriter
publie void write (String str) 一次写多个字符,将b数组中所有字符,写入输出流。
package io.str;
import java.io.FileWriter;
import java.io.IOException;
public class StrStream {
public static void main(String[] args) throws IOException {
// 创建FileWriter 文件字符输出流
FileWriter fw = new FileWriter("bbb.txt");
// 写入
for (int i = 0; i < 10; i++) {
String str = "你好吗兄弟\n";
fw.write(str);
fw.flush();
}
fw.close();
}
}
复制文件
只能复制文本文件,不能复制二进制文件(图、视频、录音、可执行文件等)
package io.str;
import java.io.*;
public class CopyFile {
public static void main(String[] args) throws IOException {
// 创建 FileReader FileWriter , 将bbb.txt复制为ccc.txt
FileReader fr = new FileReader("bbb.txt");
FileWriter fw = new FileWriter("ccc.txt");
// 一边读 一边写
char[] buf = new char[1024 * 4];
int count = 0;
while ((count = fr.read(buf)) != -1) {
fw.write(buf, 0, count);
}
// 关闭
fr.close();
fw.close();
}
}
字符缓冲流
类:BufferedReader / Bufferedwriter
- 高效读写
- 支持输入换行符
- 可一次写一行、读一行
读:BufferedReader
package io.str;
import java.io.*;
public class BufferedRead {
public static void main(String[] args) throws IOException {
// 创建BufferedReader
FileReader fr = new FileReader("bbb.txt");
BufferedReader br = new BufferedReader(fr); // 缓冲流 接收一个输入流对象
// 读取 第一种方式 读指定字节
// int count = 0;
// char[] buf = new char[1024];
// while ((count = br.read(buf)) != -1) {
// System.out.println(new String(buf, 0, count));
// }
// 读取 第二种方式,按行读
String line = null;
while ((line=br.readLine())!=null){
System.out.println(line);
}
br.close();
}
}
写:Bufferedwriter
package io.str;
import java.io.*;
public class BufferedWrite {
public static void main(String[] args) throws IOException {
// 创建BufferedWriter
FileWriter fw = new FileWriter("bbb.txt");
BufferedWriter bw = new BufferedWriter(fw); // 缓冲流 接收一个输入流对象
// 写入
String str = "你好你好你好";
for (int i = 0; i < 10; i++) {
bw.write(str);
bw.newLine(); // 写入一个换行符
bw.flush();
}
bw.close();
}
}
打印流 PrintWrite
- 继承自Writer类
- 将字符写入到文件
- 封装了print()、println() 方法,支持写入后换行
- 支持数据原样打印
package io.str;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
public class PrintStream {
public static void main(String[] args) throws FileNotFoundException {
PrintWriter pw = new PrintWriter("ccc.txt");
// 覆盖写入
pw.println(78);
pw.println("你好");
pw.println(3.14);
pw.close();
}
}
转换流
也叫桥转换流:InputStreamReader / OutputStreamWriter
- 可将字节流转换为字符流
- 可设置字符的编码方式。
读:
package io.turn;
import java.io.*;
public class InputStream {
public static void main(String[] args) throws IOException {
// 创建字节流 写
FileInputStream fis = new FileInputStream("bbb.txt");
// 创建InputStreamReader对象
InputStreamReader isr = new InputStreamReader(fis, "utf-8");
int count = 0;
char[] buf = new char[1024];
while ((count = isr.read(buf)) != -1) {
System.out.println(new String(buf, 0, count));
}
isr.close()
}
}
写:
package io.turn;
import java.io.*;
public class OutputStream {
public static void main(String[] args) throws IOException {
FileOutputStream fos = new FileOutputStream("bbb.txt");
OutputStreamWriter osw = new OutputStreamWriter(fos, "UTF-8");
for (int i = 0; i < 10; i++) {
osw.write("大家好");
osw.flush();
}
osw.close();
}
}
File 类
概念:代表物理盘符中的一个文件或者文件夹。
- 方法:
- createNewFile() // 创建一个新文件
- mkdir() // 创建一个新目录
- delete() // 删除文件或空目录
- exists() // 判断Pile对象所对象所代表的对象是否存在
- getAbsolutePath() // 获取文件的绝对路径
- getName() // 取得名字
- getParent() // 获取文件/目录所在的目录
- isDirectory() // 是否是目录
- isFile() // 是否是文件
- length() // 获得文件的长度
- listFiles() // 列出目录中的所有内容
- renameTo() // 修改文件名为
文件操作
package io.file;
import java.io.File;
import java.io.IOException;
import java.util.Date;
public class FileTest {
public static void main(String[] args) throws IOException {
// separator();
fileOpen();
}
// 分隔符
public static void separator() {
String pathSeparator = File.pathSeparator; // 路径分隔符
String separator = File.separator; // 名称分隔符
}
// 文件操作
public static void fileOpen() throws IOException {
File file = new File("file.txt");
// 创建文件
if (!file.exists()) { // 如果文件不存在
boolean c = file.createNewFile(); //创建文件,返回值为bool,true为创建成功
}
// 删除文件,2种方式
// boolean d = file.delete(); // 1、直接删除,返回值为bool,true为创建成功
file.deleteOnExit(); // 2、jvm退出时删除文件
// 获取文件信息
String absolute_path = file.getAbsolutePath(); // 获取绝对路径
String path = file.getPath(); // 获取路径
String name = file.getName(); // 获取文件名
String father_path = file.getParent(); // 获取父目录
long size = file.length(); // 获取文件大小 字节
long date = file.lastModified(); // 获取创建时间 时间戳
// 判断
boolean is_write = file.canWrite(); // 判断文件是否可写
boolean is_read = file.canRead(); // 判断文件是否可读
boolean is_file = file.isFile(); // 判断是否是文件
boolean is_hide = file.isHidden(); // 判断文件是否隐藏
}
}
目录操作
package io.file;
import java.io.File;
public class DirTest {
public static void main(String[] args) {
dirOpe();
}
public static void dirOpe() {
File dir = new File("../../test");
// 创建文件夹
if (!dir.exists()) {
// dir.mkdir(); // 只能创建单级目录
boolean m = dir.mkdirs(); // 可创建多级目录
}
// 删除文件夹,2种方法
// dir.delete(); // 直接删除,只能删除空目录
dir.deleteOnExit(); // jvm删除,程序结束时删除
// 获取文件夹信息
String absolute_path = dir.getAbsolutePath(); // 获取绝对路径
String path = dir.getPath(); // 获取路径
String name = dir.getName(); // 获取名称
String father_path = dir.getParent(); // 获取父目录
long date = dir.lastModified(); // 获取创建时间 时间戳
// 判断
boolean is_dir = dir.isDirectory(); // 判断是否是目录
boolean is_hide = dir.isHidden(); // 判断文件是否隐藏
// 遍历文件夹
File dir2 = new File("./");
String[] files = dir2.list();
for (String file : files) {
System.out.println(file);
}
}
}
其他常用操作
FileFilter 接口
public interface FileFilter
- boolean accept (File pathname)
- 当调用File类中的ListFiles()方法时:文持传入FileFilter接口实现类,对获取文件进行过滤,只有满足条件的文件的才可出现在ListFiles()的返回值中
package io.file;
import java.io.File;
import java.io.FileFilter;
public class Other {
public static void main(String[] args) {
File dir = new File("./");
File[] files = dir.listFiles(
new FileFilter() {
@Override
public boolean accept(File pathname) {
if (pathname.getName().endsWith(".txt")) { // 判断后缀是否是txt
return true;
} else {
return false;
}
}
}
);
for (File f : files) {
System.out.println(f);
}
}
}
递归文件夹
package io.file;
import java.io.File;
import java.util.ArrayList;
public class Other {
public static void main(String[] args) {
ArrayList<File> list = new ArrayList<File>();
String path = "./";
getFiles(list, path);
for (File file : list) {
System.out.println(file);
}
}
private static void getFiles(ArrayList<File> fileList, String path) {
File[] allFiles = new File(path).listFiles();
for (int i = 0; i < allFiles.length; i++) {
File file = allFiles[i];
if (file.isFile()) {
fileList.add(file);
} else {
getFiles(fileList, file.getAbsolutePath());
}
}
}
}

浙公网安备 33010602011771号