1. IO
1.1 概述
流是一组有顺序的,有起点和终点的字节集合,是对数据传输的总称或抽象。即数据在两设备间的传输称为流,流的本质是数据传输,根据数据传输特性将流抽象为各种类,方便更直观的进行数据操作。
I : input 输入
O : output 输出
输入 : 就是把数据输入到内存中
输出 : 就是把内存中的数据写出到外面
1.2 分类
按处理数据类型的不同,分为字节流和字符流
按数据流向的不同,分为输入流和输出流(相对于内存来讲)
按功能不用分为节点流和处理流
节点流:直接操作数据源
处理流:对其他流进行处理
1.3 四大抽象类
InputStream:字节输入
OutputStream:字节输出
Reader:字符输入
Writer:字符输出
1.4 InputStream
方法:
void close():关闭此输入流并释放与该流关联的所有系统资源
abstract int raed():从输入流读取下一个数据字节
int read(byte[] b):从输入流中读取一定数量的字节并将其存储在缓冲区数组b中
int read(byte[] b,int off, int len):将输入流中最多len个数据字节读入字节数组
1.4.1 read使用方法
read : 读取文件中的数据,一次读取一个字节,返回值是读取的字节的值(返回int类型),如果读取到文件末尾(读完了)返回-1
package com;
import java.io.FileInputStream;
import java.io.IOException;
/**
* FileInputStream是字节输入流,就是读取文件中的数据
* 在操作系统层面,一切皆是文件
* @author 势不可挡的内鬼
*
*/
public class IO_01_FileInputStream_01 {
public static void main(String[] args) throws IOException {
//绝对路径:能够找到这个文件的全路径
//相对路径:相对于当前文件的所在位置去找其他文件,./表示当前目录,../表示上级目录,../../表示上上级目录
// FileInputStream fis = new FileInputStream("C:/test/a.txt");
//eclipse中 ./ 找到的是当前项目
FileInputStream fis = new FileInputStream("./src/com/IO_01_FileInputStream_01.java");
//read:读取文件中的数据,一次读取一个字节,返回值是读取字节的返回值(返回int类型),如果读取到文末尾(读完了)返回-1
int i = fis.read();
System.out.println((char)i);
}
}
package com;
import java.io.FileInputStream;
/**
* 循环读取
*
* @author 势不可挡的内鬼
*
*/
public class IO_02_FileInputStream_02 {
public static void main(String[] args) {
try (FileInputStream fis = new FileInputStream("./src/com/IO_01_FileInputStream_01");) {
int temp = 0;
while ((temp = fis.read()) != -1) {
System.err.println((char) temp);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
1.4.2 read重载使用方式
read方法的重载 , 可以传递一个字节数组,为了提高读取效率
默认一次读取一个字节,如果传递了字节数组,则把数组一次性读满,再回来,然后下次去再读满,一直到读完
read(byte[]) : 一次读取一个数组,返回当前次读取到的个数,如果到达文件末尾,返回-
package com;
import java.io.FileInputStream;
/**
* read方法的重载,可以传递一个字节数组,为了提高读取效率
* 默认一次读取一个字节,如果传递了字节数组,则把数组一次性读满然后下次再去读满,一直读完
* read(byte[]):一次读取一个数组,返回当前次读取到的个数,如果到达文件末尾则返回-1
*
* @author 势不可挡的内鬼
*
*/
public class IO_03_FileInputStream_03 {
public static void main(String[] args) {
try(FileInputStream fis = new FileInputStream("./src/com/IO_01_FileInputStream_01.java");) {
//available:可以获取的字节数
System.out.println(fis.available());
// byte[] bytes = new byte[fis.available()];
byte[] bytes = new byte[1025];
int count = fis.read(bytes);
System.out.println(new String(bytes,0,count));
System.out.println(count);
count = fis.read(bytes);
// new String(bytes) 把自己数组中所有的数据转换为字符串,但是可能有冗余数据
// 所以推荐使用 new String(bytes,0,count) 只把本次读取的数据转换为字符串
System.out.println(new String(bytes, 0, count));
System.out.println(count);
}catch(Exception e){
e.printStackTrace();
}
}
}
1.5 Reader
方法:
abstract void close():关闭该流
int read():读取单个字符
int read(char[] qqq):将字符读入数组
abstract int read(char[] qweqwe,int off, int len):将字符读入数组的某一部分
/**
* FileReader:字符输入流,字符输入流是一次读取一个字符
* 并且java中汉字采用unicode编码,而unicode编码占用16位,就是一个字符
* 所以使用字符输入流可以解决汉字乱码的问题
* 字符流一般只用于纯文本文件,比如压缩包,图片,视频等还是需要字节流处理
* read():读取一个字符,返回该字符的int值,达到文件末尾返回-1
* read(char[]):读取一个char数组,返回当前读取的个数,到达末尾返回-1
*
*/
1.5.1 read使用方式
package com;
import java.io.FileReader;
public class IO_05_FileReader_01 {
public static void main(String[] args) {
try(FileReader fr = new FileReader("./src/com/IO_01_FileInputStream_01.java")){
int temp = 0;
while((temp = fr.read()) != -1) {
System.out.println((char)temp);
}
}catch(Exception e ) {
e.printStackTrace();
}
}
}
1.5.2 read重载使用方式
1.6 OutputStream
1.6.1 概述
输出流是吧内存中的数据写到硬盘中
如果该文件不存在,会自动创建,但是不会创建文件夹
如果对应的文件夹目录不存在,就报错
构造方法
FlieOutputStream(String path):向该文件中写出数据,并覆盖原有内容
FlieOutputStream(String path,boolean append):向该文件中写出数据,如果append为true就追加写出,如果为false就覆盖
write(int):写出一个数据
write(byte[] b):写出该数组中所有的内容
write(byte[] b,int off, int len):写出数组中的指定内容
flush():刷缓存,不调用也可以,关闭流时会自动调用,最好手动写上,以免报错
辅助方法:getBytes():String中的方法,用于获取字符串对应的字节数组
1.6.2 使用方式
public class IO_06_FileOutputStream_01 {
public static void main(String[] args) {
try(FileOutputStream fos = new FileOutputStream("D:/a.txt");
FileOutputStream fos1 = new FileOutputStream("D:/a.txt",true);){
fos1.write(2);
fos1.write(3);
fos1.write(4);
fos1.write(5);
fos1.write('\n');
String str = "hello";
byte[] bytes = str.getBytes();
fos1.write(bytes);
fos1.flush();
}catch(Exception e) {
e.printStackTrace();
}
}
}
1.7 Writer
1.7.1 概述
1.7.2 使用方式
package com;
import java.io.FileWriter;
public class IO_07_FileWriter {
public static void main(String[] args) {
try(FileWriter fw = new FileWriter("D:/a.txt");){
//写出字符串
fw.write("hello\n");
char[] chars = {'a','b','c','d'};
fw.write(chars);
fw.flush();
}catch(Exception e) {
e.printStackTrace();
}
}
}
1.8 缓冲流
1.8.1 概述
1.8.2 BufferedInputStream
package com;
import java.io.BufferedInputStream;
import java.io.FileInputStream;
//字节输入缓冲流
public class IO_08_BufferedInputStream {
public static void main(String[] args) {
// long startTime = System.currentTimeMillis();
// try(FileInputStream fis = new FileInputStream("D:/a.txt");){
// byte[] bytes = new byte[1024];
// int temp = 0;
// while((temp=fis.read(bytes)) != -1){
// }
// long endTime = System.currentTimeMillis();
// System.out.println("读取耗时:"+(endTime - startTime+"s"));
long startTime = System.currentTimeMillis();
try(FileInputStream fis = new FileInputStream("D:/a.txt");
BufferedInputStream bis = new BufferedInputStream(fis);){
byte[] bytes = new byte[1024];
int temp = 0;
while((temp=fis.read(bytes)) != -1){
}
long endTime = System.currentTimeMillis();
System.out.println("读取耗时:"+(endTime - startTime+"s"));
}catch(Exception e) {
e.printStackTrace();
}
}
}
1.8.3 BufferedOutputStream
package com;
import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
//字节输出缓冲流
public class IO_09_BufferedOutputStream {
public static void main(String[] args) {
try (FileOutputStream fos = new FileOutputStream("D:/a.txt");
BufferedOutputStream bos = new BufferedOutputStream(fos);
) {
bos.write(97);
bos.write(98);
bos.write(99);
bos.write(100);
bos.write('\n');
String str = "你好吗";
byte[] bytes = str.getBytes();
bos.write(bytes);
bos.flush();
} catch (Exception e) {
e.printStackTrace();
}
}
}
1.8.4 BufferedReader
package com;
import java.io.BufferedReader;
import java.io.FileReader;
/*
* 字符输入缓冲流
* 新增方法 String readLine() : 读取一行数据,返回值就是读到的数据,到达文件末尾返回 null
*/
public class IO_10_BufferedReader {
public static void main(String[] args) {
try(FileReader fr = new FileReader("D:/a.txt");
BufferedReader br = new BufferedReader(fr);) {
String temp = null;
// 读取一行
while ((temp = br.readLine()) != null) {
System.out.println(temp);
}
}catch(Exception e ) {
e.printStackTrace();
}
}
1.8.5 BufferedWriter
package com;
import java.io.BufferedWriter;
import java.io.FileWriter;
//字符输出缓冲流
public class IO_11_BufferedWriter {
public static void main(String[] args) {
BufferedWriter bw = null ;
try {
bw = new BufferedWriter(new FileWriter("D:/a.txt"));
bw.write("你好吗");
// 换行
bw.newLine();
bw.write("我很好");
bw.flush();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (bw != null) {
bw.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}