Day_19_IO(输入流,输出流,缓存流,打印流,转换流)
流是一组有顺序的,有起点有重点的字节集合,是对数据传输的总称或抽象
- 简单来说 就是设备之间的数据传递称为流
- 流的本质 就是数据的传输,根据不同的数据传输特性,抽象出各种类,方便我们直接操作数据
- 流中的操作对象是指内存中,当前操作的程序而言
-
输入 : 指往内存中导入数据
-
输出 : 指从内存中写出数据
- I : input 输入
- O : output 输出
- 原始文件流 : 用于打开链接,操作数据
-
1 FileInputStream 字节输入
-
2 FileOutputStream 字节输出
-
3 FileReader 字符输入
-
4 FileWriter 字符输出
public class IO_01 {
public static void main(String[] args) throws IOException {
// System.in 就是InputStream (控制台)
InputStream in = System.in;
System.out.println("请输入 : ");
int i = in.read();
// System.out是标准输出(控制台)
PrintStream out = System.out;
out.println(i);
}
}
java.io.InputStream
-
java.io.FileInputStream; 按照自己的方式在原始文件中读取数据
- 想要读取一个文件,必须要找到它
- 定位到某个文件 有两种方式, 1 绝对路径 2 相对路径
- 相对路径 : 相对当前文件,如何找到 ./ 当前目录 , …/ 上级目录, …/…/…/ 上上上级目录 , / 下级
- 绝对路径 : 以系统跟目录为准,如何找到这个文件
public class IO_02_FileInputStream {
public static void main(String[] args) {
// 在Eclipse中 相对的是当前项目 D:\workspace\java\D
FileInputStream fis = null ;
try {
// 创建对象 使用相对路径找这个文件
fis = new FileInputStream("./src/io/IO_02_FileInputStream.java");
// 绝对路径
// FileInputStream fis = new FileInputStream("D:/xxx/xx/xx.txt");
// 读取数据
// 读取一个字节,返回下一个字节的值,因为开始光标在顶端,如果到达文件末尾 就返回-1
int i1 = fis.read();
int i2 = fis.read();
System.out.println((char)i1);
System.out.println((char)i2);
} catch (Exception e) {
e.printStackTrace();
}finally{
try {
if (fis != null) {
fis.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
循环输出,打印文件
import java.io.FileInputStream;
// 使用 read 循环读取
public class IO_03_FileInputStream {
public static void main(String[] args) {
// 自动关闭资源
try (FileInputStream fis = new FileInputStream(
"./src/io/IO_02_FileInputStream.java");) {
// while (true) {
// int temp = fis.read();
// // 到达文件末尾 返回-1
// if (temp == -1) {
// break;
// } else {
// System.out.print((char) temp);
// }
// }
int temp = 0;
while( (temp = fis.read()) != -1 ){
System.out.print((char) temp);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
read 方法重载
*
-
可以无参,可以传入一个byte数组,
-
read(byte[] b) : 使用字节数组来存储读取的数据,并一次返回(要么数组装满,要么到达文件末尾),可以提高读取效率
-
返回值 是本次读取到的字节个数,如果到达文件末尾 返回-1
public class IO_04_FileInputStream {
public static void main(String[] args) {
// 自动关闭
try (FileInputStream fis = new FileInputStream(
"./src/io/IO_02_FileInputStream.java");) {
// 创建数组
byte[] bytes = new byte[1000];
// 返回本次读取的字节个数,
int i1 = fis.read(bytes);
System.out.println(i1);
// 转换 string
System.out.println(new String(bytes));
// 在读取一次
// 再次使用该数组读取的时候,数组内元素不会清空,而是会用新读取的数据替换原来的
// int i2 = fis.read(bytes);
// System.out.println(i2);
// 转换 为string
// 如果我们直接把字节数组转换为String的话,有可能产生冗余数据(上次读取的)
// 所以 为了保证数据不冗余,我们可以利用read的返回值,本次读取的字节数
// 我们只把本次读取的字节 转换为String 即可
// 字节数组,从哪开始(包含),个数
//System.out.println(new String(bytes,0,i2));
} catch (Exception e) {
e.printStackTrace();
}
}
}
// 循环读取
public class IO_05_FileInputStream {
public static void main(String[] args) {
// 自动关闭资源
try (FileInputStream fis = new FileInputStream(
"./src/io/IO_02_FileInputStream.java");) {
// 准备数组
byte[] bytes = new byte[1024];
int temp = 0;
while((temp = fis.read(bytes)) != -1){
System.out.println( new String(bytes,0,temp));
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
// 常用方法
public class IO_06_FileInputStream {
public static void main(String[] args) throws IOException {
// 创建流对象
FileInputStream fis = new FileInputStream(
"./src/io/IO_02_FileInputStream.java");
// 可获得字节数
System.out.println(fis.available());
// 读取字节数组
byte[] bytes = new byte[5];
fis.read(bytes);
System.out.println(new String(bytes));
System.out.println(fis.available());
// 跳过不读
fis.skip(20);
System.out.println(fis.available());
// 读取一个
System.out.println(fis.read());
// 关闭资源
fis.close();
}
}
FileRead : 一次读一个字符,也就是2字节,而 unicode编码也是2字节
*
- 所以 字符串输入流 一般用于读取纯文本文件,像压缩包,图片等还是使用字节流
- read一样有方法重载,
-
read() : 读取一个字符,返回下一个的字符数据,到达文件末尾返回-1
-
read(char[] c) : 读取一个字符数组,返回读取到的字符数,到达文件末尾返回-1
public class IO_07_FileReader {
public static void main(String[] args) {
try(
FileReader fr = new FileReader("D:/test.java");
) {
// 字符数组
char[] chars = new char[512];
int tmp = 0;
while((tmp = fr.read(chars)) != -1){
System.out.print(new String(chars,0,tmp));
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
java.io.OutputStream
-
java.io.FileOutputStream
-
将程序中内容 写出到硬盘中
- 输入流 找不到指定文件 会报错 , 但是输出流不会报错,会自动创建该文件,但是不会创建文件夹(目录)
- 构造方法 :
-
FileOutputStream(String) : 把内容输出到指定文件中,并会覆盖原文件中内容
-
FileOutputStream(String,boolean) : 如果第二个参数是true,把内容输出到指定文件中,并在原文件中追加数据
- 成员方法 :
-
write(int i) : 写出整型
-
write(byte[] b) : 写出字节数组,想要输出字符串可以利用字符串中的getBytes()方法,把字符串转换为字节数组
-
flush() : 刷缓存,强制把缓冲区写出,避免造成数据遗漏
public class IO_08_FileOutputStream {
public static void main(String[] args) throws IOException {
// 创建输出流对象,如果没有传递boolean值,默认是false,会覆盖原文件中内容
// FileOutputStream fos = new FileOutputStream("D:/123.txt");
// 追加式写入
FileOutputStream fos = new FileOutputStream("D:/test.txt", true);
for (int i = 0; i < 26; i++) {
fos.write(i + 97);
}
// 写出字符串
String str = "Hello world";
byte[] bytes = str.getBytes();
fos.write(bytes);
// 刷缓存
fos.flush();
// 关闭流
fos.close();
}
}
java.io.Writer
*
-
java.io.File.Writer 字符输出流
- 和字节输出流基本一致,新增字符串写出
public class IO_09_FileWriter {
public static void main(String[] args) {
try (
// 默认是覆盖写入
FileWriter fw = new FileWriter("D:/test.txt");
){
// \n是换行
fw.write("你好吗\n");
char[] chars = {'a','b','c','d'};
fw.write(chars);
//刷缓存
fw.flush();
} catch (Exception e) {
e.printStackTrace();
}
}
}
缓冲流 是包装流,创建对象的时候,传入的参数不再是文件地址,而是文件流对象
*
- 作用 : 把每次读入的数据存入到一个缓冲区,然后一次写入
-
把输出的数据存入一个缓冲区,然后一次写出
- 如果缓存流关闭,那么传入的对应的文件流对象也会被关闭
-
BufferedInputStream,BufferedOutputStream,BufferedReader,BufferedWriter
-
针对字符操作,提供了两个新方法
-
readLine() : 读一行,返回值就是读到的这一行的数据,到达文件末尾,返回null
-
newLine() : 换行,就等于\n
public class IO_10_BufferedInputStream {
public static void main(String[] args) {
try(
// 创建字节输入流
FileInputStream fis = new FileInputStream("./src/io/IO_02_FileInputStream.java");
// 创建字节输入缓冲流
BufferedInputStream bis = new BufferedInputStream(fis);
) {
// 读取下一个字节,到达末尾返回 -1
System.out.println((char)bis.read());
// 读取一个数组,返回读取到的长度,到达末尾返回-1
byte[] bytes = new byte[10];
int temp =bis.read(bytes);
System.out.println(new String(bytes,0,temp));
} catch (Exception e) {
e.printStackTrace();
}
}
}
转换流 是字节流和字符流之间转换 的桥梁,可以通过转换 流把字节流转换为字符流
-
并且 还可以再转换过程中 指定字符编码
public class IO_14_InputStreamReader {
public static void main(String[] args) {
try (
// 创建一个字节输入流对象
FileInputStream fis = new FileInputStream("./src/io/IO_02_FileInputStream.java");
// 创建转换流对象 并把字节输入流传入,转换为字符流
InputStreamReader isr = new InputStreamReader(fis);
// 创建字符输入缓冲流,把转换流传入
BufferedReader br = new BufferedReader(isr);
){
String tmp = br.readLine();
System.out.println(tmp);
} catch (Exception e) {
e.printStackTrace();
}
}
}