1、什么是流
流是一连串流动的字符, 是一组有序的数据序列,是以先进先出方式发送信息的通道,将数据从一个地方带到另一个地方,在 java 中所有数据都是使用流读写的。同时可以通过流进行文件的读写操作。
2、流的分类
按照流向,可以分为输入流和输出流两大类
简单来说,可以理解为将磁盘中的数据读入到内存中即为输入流;相反,将内存中的数据存入磁盘中即为输出流
按处理数据的单元,可以将流分为字节流和字符流两大类
字节流是 8 位通用字节流,字符流是 16 位 Unicode 字符流
3、文件读写操作
文件分为文本文件和二进制文件,对不同类型的文件,我们需要的操作也不尽相同
FileInputStream类常用方法
子类FileInputStream常用的构造方法
FileInputStream读取文本文件操作步骤:
-
创建文件文件输入流对象
FileInputStream fis= new FileInputStream(File类或String类文件路径);
-
读取文本文件的数据
fis.read();
-
关闭文件流
fis.read();
参考代码:
public class TestFileInputStream {
public static void main(String[] args) {
String path = "D:\\aa.txt";
File file = new File(path);
// 需先判断文件是否存在,若不存在,创建
if (!file.exists()) {
try {
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
FileInputStream fileInputStream = null;
try {
// 创建输入流对象(一般参数使用File类型,也可以跟路径path)
fileInputStream = new FileInputStream(file);
// 进行文件的操作,若读到文件末尾,将返回一个-1
int read = -1;
while (true) {
// fileInputStream读取到的是字节,并非字符型
// 需要经过强转(char)才能显示字符
read = fileInputStream.read();
if (read == -1) {
break;
}
System.out.print((char) read);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
// 释放资源,释放前,需要先确认文件是否存在
try {
if (fileInputStream != null) {
fileInputStream.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
在以上代码中,我们会存在两个问题:一是手动关闭资源比较麻烦,二是一个字节一个字节的读取不够高效,对此,我们进行优化
参考代码:
/**
* @author Nil
* @create 2021-11-21-14:32
* @description 如果觉得手动关闭资源比较麻烦,可以使用jdk1.7引入的自动关闭资源的写法
*/
public class TestFileInputStream2 {
public static void main(String[] args) {
String path = "D:\\aa.txt";
// 在try中创建流后会自动释放资源:fileInputStream.close()
try(FileInputStream fileInputStream = new FileInputStream(path)) {
while (true) {
// 创建缓冲区,用字符数组读取内容
byte[] datas = new byte[3];
int read = fileInputStream.read(datas);
System.out.println(read);
// 获取数据
for (int i = 0; i < datas.length; i++) {
System.out.print((char)datas[i]+"");
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
FileOutputStream类常用方法
子类FileOutputStream常用的构造方法
-
FileOutputStream (File file)
-
FileOutputStream(String name)
-
FileOutputStream(String name,boolean append)
FileOutputStream写入文本文件操作步骤:
-
创建文件文件输出流对象
OutputStream os = new FileOutputStream("D:\\hello.txt");
-
把数据写入文本文件中
String str ="content";
byte[] words = str.getBytes();
os.write(words, 0, words.length);
-
关闭文件流
os.close();
参考代码:
public class TestFileOutputStream {
public static void main(String[] args) {
String path = "d:\\info.txt";
OutputStream outputStream = null;
try {
// 1.先判断是否存在该文件
File file = new File(path);
boolean ret = false;
if (!file.exists()) {
// 2.创建文件
ret = file.createNewFile();
}
// 有文件
if (ret) {
// 3、创建输出流
outputStream = new FileOutputStream(file);
// 4、进行写入操作,写入字符
outputStream.write('a');
System.out.println("写入成功!");
} else {
System.out.println("文件不存在!");
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (outputStream != null) {
try {
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
上述代码存在几个问题:一个是该写入方式只会覆盖,而不是追加;二是存在编码混乱的问题,对此,我们进行优化
public class TestFileOutputStream2 {
public static void main(String[] args) {
String path = "d:\\info.txt";
OutputStream outputStream = null;
try {
// 1.先判断是否存在该文件
File file = new File(path);
if (!file.exists()) {
// 2.创建文件
if (!file.createNewFile()) {
System.out.println("文件创建失败!");
return;
}
}
// 3、指定编码
String word = "888";
byte[] bytes = word.getBytes(StandardCharsets.UTF_8);
// 4、创建输出流,追加方式
outputStream = new FileOutputStream(file,true);
// 5、进行写入操作
outputStream.write(bytes);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (outputStream != null) {
try {
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}