int read() 从此输入流中读取一个字节的数据。 返回值: 数据的下一个字节,如果到达文件的末尾, -1 。 每次读取一个字节的数据,当数据个数非常多, 重复读取多次-->循环简化
public class IODemo02 {
public static void main(String[] args) throws IOException {
//1.创建流
InputStream is = new FileInputStream("D:/heihei.txt");
//2.读入
//int read() 从此输入流中读取一个字节的数据。
int result = -1; //result中存储读入到的数据(数据的字节|-1没读到)
//重复读取
while((result = is.read())!=-1){
//3.处理数据
System.out.println((char)result);
}
//4.关闭
is.close();
}
}
每次只读一个字节,读取次数过多,过于浪费,每次读取一个字节数组的数据,循环读取 int read(byte[] b) 从此输入流 b.length最多 b.length字节的数据读 b.length字节数组。
public class IODemo03 {
public static void main(String[] args) throws IOException {
//1.创建流
InputStream is = new FileInputStream("D:/heihei.txt");
//2.读入
//字节数组 卡车
byte[] car = new byte[1024]; //卡车
int len = -1; //存储读入到数组中数据的个数
//重复读取
while((len=is.read(car))!=-1){
System.out.println(new String(car,0,len));
}
//4.关闭
is.close();
}
}
OutputStream 字节输出流: 此抽象类是表示输出字节流的所有类的超类。 ByteArrayOutputStream 字节数组输出流 从程序写出内容到字节数组 FileOutputStream 文件输出流 从程序写出内容到文件 注意: 只要输出流就要提供关闭之前的刷出 目的地文件不存在会自动创建,但是如果目的地目录不存在,需要手动创建 写出目的地源文件中的内容默认覆盖不是追加,如果想要追加,需要在构造器中添加一个参数boolean append,true为追加,默认false覆盖
public class IODemo04 {
public static void main(String[] args) throws IOException {
//1.创建流 输出流管道
//FileOutputStream(String name)
//OutputStream os = new FileOutputStream("D:/xixi.txt");
//FileOutputStream(File file)
File dest = new File("D:/xixi.txt");
OutputStream os = new FileOutputStream(dest,true); //追加
//2.写出
//void write(int b) 将指定的字节写入此文件输出流。
os.write(97);
//3.刷出
os.flush();
//4.关闭
os.close();
}
}
操作单元:字符流 流向:输入流 功能:节点流 字符流: 只能操作与 纯文本文件 Reader 用于读取字符流的抽象类。 FileReader 使用默认缓冲区大小从字符文件中读取文本
public class CharIO07 {
public static void main(String[] args) throws IOException {
//1.创建流
//FileReader(String fileName) 使用平台 default charset创建一个新的 FileReader ,给定要读取的文件的 名称 。
//FileReader(File file) 使用平台 FileReader ,在 File读取时创建一个新的 FileReader 。
Reader rd = new FileReader("D:/haha.txt");
//2.读入
//read() 一个字符
//read(char[]) 字符串组单位读取
//System.out.println( (char)(rd.read()));
//汽车
char[] car = new char[1024];
int len = -1; //读入到数组中数据的个数
while((len = rd.read(car))!=-1){
System.out.println(new String(car,0,len));
}
//3.关闭
rd.close();
}
}
功能流 : 用来扩展节点流的功能性能 使用: 功能流(字节流) 缓冲流: Buffered : 加快节点流的读写效率 字节缓冲流 : BufferedInputStream 字节输入缓冲流 BufferedOutputStream 字节输出缓冲流 --BufferedInputStream(InputStream in) 参数为操作的节点流,增强参数节点的读写速度 注意: 没有新增方法,可以发生多态 使用没有影响 字符缓冲流
public class BufferedInputStream10 {
public static void main(String[] args) throws IOException {
//1.流
InputStream is = new BufferedInputStream(new FileInputStream("D:/haha.txt"));
OutputStream os = new BufferedOutputStream(new FileOutputStream("D:/DDD.txt",true));
//2.读入,写出
byte[] car = new byte[1024];
int len= -1;
//循环读写
while((len=is.read(car))!=-1){
//读入到字节数组中多少个len个数据,写出多少个
os.write(car,0,len);
}
//3.刷出
os.flush();
//4.关闭 (后打开的先关闭)
os.close();
is.close();
}
}
字符流缓冲流 字符输入缓冲流 BufferedReader 新增方法: String readLine() 读一行文字。 字符输出缓冲流 BufferedWriter 新增方法: void newLine() 写一个行分隔符。 注意:不能发生多态
public class BufferedReader11 {
public static void main(String[] args) throws IOException {
//1.流
BufferedReader br = new BufferedReader(new FileReader("D://haha.txt"));
BufferedWriter bw = new BufferedWriter(new FileWriter("D://EEE.txt"));
//2.读写
String msg = null; //存储每次读入的数据 如果达到文件末尾,返回null
while((msg = br.readLine())!=null){
bw.write(msg);
bw.newLine(); //换行符
}
//3.刷出
bw.flush();
//4.关闭
bw.close();
br.close();
}
}
Data流 (基本数据类型流) 字节流的功能流: 读写基本数据类型的数据+数据类型+String字符串数据 DataInputStream 新增方法: readXxx() DataOutputStream 新增方法: WriteXxx(数据) 注意: 什么顺序写出,就要使用什么 顺序读入 读入的文件必须为源文件否则抛出异常
public class DataDemo12 {
public static void main(String[] args) throws IOException {
readFromFile("D:/FFF2.txt");
}
//从文件中读入数据(数据+数据类型)
public static void readFromFile(String src) throws IOException {
//1.构建流
DataInputStream is = new DataInputStream(new FileInputStream(src));
//2.读入
int i = is.readInt();
//3.处理数据
System.out.println(i);
System.out.println(is.readBoolean());
System.out.println(is.readChar());
System.out.println(is.readUTF());
//4.关闭
is.close();
}
//写出到文件(数据+数据类型)
public static void writToFile(String dest) throws IOException {
//1.构建流
DataOutputStream out = new DataOutputStream(new FileOutputStream(dest));
//2.准备数据
int i =100;
boolean flag = false;
char ch = 'a';
String str = "哈哈";
//3.写出
out.writeInt(i);
out.writeBoolean(flag);
out.writeChar(ch);
out.writeUTF(str);
//4.刷出
out.flush();
//5.关闭
out.close();
}
}
字节流的功能流 *** Object流|对象流|序列化流 让节点流具有读写对象数据+数据类型的能力 序列化: 把对象数据的状态转为可存储或者可传输的状态的过程 反序列化: ObjectOutputStream : 对象字节输出流 | 序列化输出流 新增方法: writeObject(Object obj) 写出对象数据 ObjectInputStream : 对象字节输入流 | 反序列化输入流 新增方法: Object readObject() 从ObjectInputStream中读取一个对象。 注意: 1.先序列化后反序列化 2.如果没有想要读写对象数据需求,不要使用对象流 3.不是所有的类都能够序列化,必须要求实现java.io.Serializable接口 4.读入数据的顺序要与写出的顺序保持一致 5.不是所有的属性都需要序列化,transient关键字修饰的属性就不会序列化,是默认值 6.静态的属性不会序列化 7.如果父类实现序列化,子类没有实现,子类可以序列化所有的内容 8.如果父类没有实现序列化,子类实现了序列化,子类只能序列化子类独有的内容,父类内容不可以序列化 9.通过序列号控制版本不一致|兼容问题
public class ObjectDemo13 {
public static void main(String[] args) throws Exception {
readObjFromFile("D:/obj.txt");
}
//读取对象数据从文件中
public static void readObjFromFile(String src) throws Exception {
//1.构建流
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(new FileInputStream(src)));
//2.读入
Object obj = in.readObject();
int[] obj2 = (int[]) in.readObject();
//3.处理数据
System.out.println(obj);
System.out.println(Arrays.toString(obj2));
//4.关闭
in.close();
}
//写出到文件对象数据
public static void wirteObjToFile(String dest) throws IOException {
//1.构建流
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(new FileOutputStream(dest)));
//2.准备数据
Person p = new Person("张三",18,180);
//3.写出数据
out.writeObject(p);
out.writeObject(new int[]{1,2,3,4});
//4.刷出
out.flush();
//5.关闭
out.close();
//修改p对象的值
//p.setAge(20);
}
}
class Person implements Serializable{
//选中类名->alt+enter生成序列化
private static final long serialVersionUID = -5670927438744993208L;
private String name;
private int age;
private transient int height; //不会序列化的属性
static int a =200;
String str = "";
String str2 = "";
public Person() {
}
public Person(String name, int age, int height) {
this.name = name;
this.age = age;
this.height = height;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public int getHeight() {
return height;
}
public void setHeight(int height) {
this.height = height;
}
@Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", age=" + age +
", height=" + height +
", a="+a+
'}';
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Person person = (Person) o;
return age == person.age &&
height == person.height &&
Objects.equals(name, person.name);
}
}
Commons-IO 第三方的开源组件,对工具类的封装 1.下载Commons-IO的代码jar包 2.拿到当前使用的项目下 项目下右键新建一个目录,用来存放这些资源jar包 3.对资源做整合(让项目可以到jar中拿第三提供的class文件) 选中jar包右键add as lib
public class CommonsIODemo14 {
public static void main(String[] args) throws IOException {
//构建一个File对象
//new File()
File src = FileUtils.getFile("D://haha.txt");
//文件拷贝
FileUtils.copyFile(src,new File("D:/c.txt"));
//文件夹拷贝
FileUtils.copyDirectory(FileUtils.getFile("D:/AAA"),FileUtils.getFile("D:/CCC/AAA"));
}
}