
通过IO可以完成硬盘文件的读和写。
以内存作为参照物:
按照 字节 的方式读取数据,一次读取1个字节byte,
等同于一次读取8个二进制位。
这种流是万能的,什么类型的文件都可以读取。
包括:文本文件,图片,声音文件,视频文件 等eg. 假设文件file1.txt,采用字节流的话是这样读的: a中国bc张三fe 第一次读:一个字节,正好读到’a’ 第二次读:一个字节,正好读到’中’字符的一半。 第三次读:一个字节,正好读到’中’字符的另外一半。
这种流是为了方便读取 普通文本文件 而存在的,
这种流不能读取:图片、声音、视频等文件。只能读取 纯文本文件,
连word文件都无法读取。注意: 纯文本文件,不单单是.txt文件,还包括 .java、.ini、.py 。总之只要 能用记事本打开 的文件都是普通文本文件。
eg. 假设文件file1.txt,采用字符流的话是这样读的: a中国bc张三fe 第一次读:'a’字符('a’字符在windows系统中占用1个字节。) 第二次读:'中’字符('中’字符在windows系统中占用2个字节。)
输入流、输出流 字节流、字符流
java.io.InputStream 字节输入流 java.io.OutputStream 字节输出流
java.io.Reader 字符输入流 java.io.Writer 字符输出流
在java中只要“类名”以 Stream 结尾的都是字节流。以“ Reader/Writer ”结尾的都是字符流。
文件专属:
java.io.FileInputStream
java.io.FileOutputStream
java.io.FileReader
java.io.FileWriter转换流:(将字节流转换成字符流)
java.io.InputStreamReader
java.io.OutputStreamWriter缓冲流专属:
java.io.BufferedReader
java.io.BufferedWriter
java.io.BufferedInputStream
java.io.BufferedOutputStream数据流专属:
java.io.DataInputStream
java.io.DataOutputStream标准输出流:
java.io.PrintWriter
java.io.PrintStream(掌握)对象专属流:
java.io.ObjectInputStream(掌握)
java.io.ObjectOutputStream(掌握)File文件类
java.io.File补充:Windows/Linux小知识点
Windows:D:\Soft\QQ\Plugin
Linux: D:/Soft/QQ/Plugin
注意: Windows各个文件之间分隔符为:” \ “;
Linux各个文件之间分割符为:” / “文件字节输入流,万能的,任何类型的文件都可以采用这个流来读
构造方法
FileInputStream(String name) name为文件路径
FileInputStream(File file) public class FileInputStreamTest04 {
public static void main(String[] args) {
FileInputStream fis = null;
try {
fis = new FileInputStream("chapter23/src/tempfile3");
// 开始读,采用byte数组,一次读取多个字节。最多读取“数组.length”个字节。
byte[] bytes = new byte[4];// 准备一个4个长度的byte数组,一次最多读取4个字节。
int readCount = 0;
// 这个方法的返回值是:读取到的字节数量。(不是字节本身);1个字节都没有读取到返回-1(文件读到末尾)
while((readCount = fis.read(bytes)) != -1) {
// 不应该全部都转换,应该是读取了多少个字节,转换多少个。
System.out.print(new String(bytes, 0, readCount));
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
// 在finally语句块当中确保流一定关闭。
if (fis != null) {// 避免空指针异常!
// 关闭流的前提是:流不是空。流是null的时候没必要关闭。
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}我们读取 外部的TXT文件的时候,就可以使用这个,以字节的形式进行读取,一次读取一个字节;


public class FileOutputStreamTest01 {
public static void main(String[] args) {
FileOutputStream fos = null;
try {
// myfile文件不存在的时候会自动新建!
// 这种方式谨慎使用,这种方式会先将原文件清空,然后重新写入。
//fos = new FileOutputStream("myfile");
// 以追加的方式在文件末尾写入。不会清空原文件内容。
fos = new FileOutputStream("tempfile3", true);
// 开始写。
byte[] bytes = {97, 98, 99, 100};
// 将byte数组全部写出!
fos.write(bytes); // abcd
// 将byte数组的一部分写出!
fos.write(bytes, 0, 2); // 再写出ab
// 字符串
String s = "我是一个中国人,我骄傲!!!";
// 将字符串转换成byte数组。
byte[] bs = s.getBytes();
// 写
fos.write(bs);
// 写完之后,最后一定要刷新
fos.flush();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fos != null) {
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}FileReader 文件字符输入流,只能读取普通文本。读取文本内容时,比较方便,快捷。


public class FileReaderTest {
public static void main(String[] args) {
FileReader reader = null;
try {
// 创建文件字符输入流
reader = new FileReader("tempfile");
// 开始读
char[] chars = new char[4]; // 一次读取4个字符
int readCount = 0;
while((readCount = reader.read(chars)) != -1) {
System.out.print(new String(chars,0,readCount));
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}FileWriter文件字符输出流。写。只能输出普通文本。

public class FileWriterTest {
public static void main(String[] args) {
FileWriter out = null;
try {
// 创建文件字符输出流对象
//out = new FileWriter("file");
out = new FileWriter("file", true);
// 开始写。
char[] chars = {'我','是','中','国','人'};
out.write(chars);
out.write(chars, 2, 3);
out.write("我是一名java软件工程师!");
// 写出一个换行符。
out.write("\n");
out.write("hello world!");
// 刷新
out.flush();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (out != null) {
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}BufferedReader带有缓冲区的字符输入流。使用这个流的时候不需要自定义char数组,或者说不需要自定义byte数组。自带缓冲。
InputStreamReader字节输入流转字符输入流 我
们以FileInputStream 读取一个文件之后,就可以将读取的东西放到缓存里面;

public class BufferedReaderTest01 {
public static void main(String[] args) throws Exception{
FileReader reader = new FileReader("Copy02.java");
// 当一个流的构造方法中需要一个流的时候,这个被传进来的流叫做:节点流。
// 外部负责包装的这个流,叫做:包装流,还有一个名字叫做:处理流。
// 像当前这个程序来说:FileReader就是一个节点流。BufferedReader就是包装流/处理流。
BufferedReader br = new BufferedReader(reader);
// br.readLine()方法读取一个文本行,但不带换行符。
String s = null;
while((s = br.readLine()) != null){
System.out.print(s);
}
// 关闭流
// 对于包装流来说,只需要关闭最外层流就行,里面的节点流会自动关闭。(可以看源代码。)
br.close();
}
}这个将字节流 变为 字符流
public class BufferedReaderTest02 {
public static void main(String[] args) throws Exception{
/*// 字节流
FileInputStream in = new FileInputStream("Copy02.java");
// 通过转换流转换(InputStreamReader将字节流转换成字符流。)
// in是节点流。reader是包装流。
InputStreamReader reader = new InputStreamReader(in);
// 这个构造方法只能传一个字符流。不能传字节流。
// reader是节点流。br是包装流。
BufferedReader br = new BufferedReader(reader);*/
// 合并
BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream("Copy02.java")));
String line = null;
while((line = br.readLine()) != null){
System.out.println(line);
}
// 关闭最外层
br.close();
}
}BufferedWriter:带有缓冲的字符输出流。 OutputStreamWriter:字节输出流转字符输出流

public static void main(String[] args) throws Exception{
// 带有缓冲区的字符输出流
BufferedWriter out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("copy", true)));
// 开始写。
out.write("hello world!");
out.write("\n");
out.write("hello kitty!");
// 刷新
out.flush();
// 关闭最外层
out.close();
}
}File类和四大家族没有关系,所以File类不能完成文件的读和写。 File对象代表什么? 文件 和 目录路径名 的抽象表示形式。
C:\Drivers 这是一个File对象
C:\Drivers\Lan\Realtek\Readme.txt 也是File对象。
一个File对象有可能对应的是目录,也可能是文件。
File只是一个 路径名 的抽象表示形式。
我们以txt文件为例
# 这个filePath 就是txt文件的路径,以字节的形式进行读取
# 使用转换流 转为字符流
InputStreamReader sr = new InputStreamReader(new FileInputStream(filePath),
StandardCharsets.UTF_8);
# 将字符流 放到 缓存流里面
BufferedReader bufferedReader = new BufferedReader(isr);
String readLine; # 定义一个读取行
# 只要不为空 就一直读取
while ((readLine = bufferedReader.readLine()) != null) {
readLine 这个读取到的就是每一行数据
}