1、什么是流
概念:内存与存储设备之间传输数据的通道
2、流的分类
按方向【重点】:
输入流:将<存储设备>中的内容读入到<内存>中。读数据
输出流:将<内存>中的内容写入到<存储设备>中。写数据
3、流的分类
按单位:
字节流:以字节为单位,可以读写所有数据
字符流:以字符为单位,只能读写文本数据
按功能:
节点流:具有实际传输数据的读写功能
过滤流:在节点流的基础之上增强功能
4、字节流
字节流的父类(抽象类):
5、字节节点流
FileOutputStream:
public void write(byte[] b) //一次写多个字节,将b数组中所有字节,写入输出流。
FileInputStream:
public int read(byte[] b) //从流中读取多个字节,将读到内容存入到b数组,返回实际读到的字 节数 ;如果达到文件的尾部,则返回-1。
6、字节过滤流
缓冲流:BufferedOutputStream / BufferedInputStream
提高IO效率,减少访问磁盘的次数
数据存储在缓冲区中,flush是将缓存区的内容写入文件中,也可以直接close。
对象流:ObjectOutputStream / ObjectInputStream
增强了缓冲区功能
增强了读写8种基本数据类型和字符串功能
增强了读写对象的功能 1)readObject() 从流中读取一个对象
2)writeObject(Object obj) 向流中写入一个对象
使用流传输对象的过程称为序列化、反序列化
7、对象序列化
对象序列化的细节
必须实现Serializable接口
必须保证其所有属性均可序列化
transient修饰为临时属性,不参与序列化
读取到文件尾部的标志:java.io.EOFException
8、字符编码
9、字符流
字符流的父类(抽象类):
10、字符节点流
FileWriter:
public void writer(String str) //一次写多个字符,将b数组中所有字符,写入输出流。
FileReader:
public int read(char[] c) //从流中读取多个字符,将读到内容存入c数组,返回实际读到的字符 数; 如果达到文件的尾部,则返回-1。
11、字符过滤流
缓冲流:BufferedWriter / BufferedReader
支持输入换行符。
可一次写一行、读一行。
PrintWriter:
封装了print() / println() 方法,支持写入后换行。
支持数据原样打印
12、字符节点流
桥转换流: InputStreamReader / OutputStreamWriter
可将字节流转换为字符流
可设置字符的编码设置
13、使用步骤
创建节点流
创建过滤流 设置字符编码集
封装过滤流
读写数据
关闭流
14、字符流的操作
一般适用于文本文件,日志等
录入文件:
FileWriter fw = null;
fw = new FileWriter("文件路径");
fw.write("要输入的内容");
fw.close();
import java.io.FileWriter;
import java.io.IOException;
public class Demo4 {
public static void main(String[] args) {
//写入字符流
FileWriter fw = null;
try {
fw = new FileWriter("文件路径");
fw.write("要输入的内容");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
try {
//关闭流
fw.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
读取文件:
FileReader fw = null;
fw = new FileReader("读取的文件路径");
char[] meg = new char[5];
int length = 0;
while((length = fw.read(meg)) != -1) {
System.out.println(new String(meg ,0 ,length));
}
fw.close();
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
public class Demo4 {
public static void main(String[] args) {
//读字符流
FileReader fw = null;
try {
fw = new FileReader("读取的文件路径");
char[] meg = new char[5];
int length = 0;
while((length = fw.read(meg)) != -1) {
System.out.println(new String(meg, 0, length));
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
try {
fw.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
-------------------------------------------> 综合 读写放一起
FileWriter fw = null;
BufferedWriter bw = null;
FileReader fr = null;
BufferedReader br = null;
fw = new FileWriter("写入文件路径" ,true);
bw = new BufferedWriter(fw);
bw.newLine();
bw.write("今天是2022年2月8日");
bw.newLine();
bw.write("明天是2022年2月9日");
bw.flush();
fr = new FileReader("读取文件路径");
br = new BufferedReader(fr);
String content = null;
while((content = br.readLine()) != null) {
System.out.println(content);
}
bw.close();
fw.close();
br.close();
fr.close();
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
//字符流
public class Demo3 {
public static void main(String[] args) {
//先写字符节点流
//写到程序中
FileWriter fw = null;
//写到缓存区
BufferedWriter bw = null;
//读文件
FileReader fr = null;
//读缓存区
BufferedReader br = null;
try {
//写文件
fw = new FileWriter("写入文件路径" ,true);
//写进缓存区
bw = new BufferedWriter(fw);
//就能空一行再写进文件里
bw.newLine();// 转行 相当于回车
//写文件
bw.write("今天是2022年2月8日");
bw.newLine();//转行
//再写文件
bw.write("明天是2022年2月9日");
bw.flush();
System.out.println("写入文件完毕, 开始读取文件......");
//读文件
fr = new FileReader("读取文件路径");
//读到缓存区
br = new BufferedReader(fr);
//整行读取
String content = null;
while((content = br.readLine()) != null) {
System.out.println(content);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
bw.close();
fw.close();
br.close();
fr.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
15、字节流的操作
/*// //读取文件内容
定义一个byte类型的数组数组的长度可以随便定义
//byte[] meg = new byte[1024];
//
一次性最多读取数组长度个字节
//is.read(meg);
//
定义一个String类型的变量 用来读取磁盘的文件
//String str = new String(meg);
//
//System.out.println("读取到的内容为:" + str + " ");
//以上会发生覆盖
//如果文件内容较多,一次性无法读取完成,需要循环读
//每次读取5个
byte[] meg = new byte[5];
//
方法内部为空的话就会返回-1
//while(is.read(meg) != -1) {
// System.out.println("读取的内容:" + new String(meg));
//}*/
录入文件:
String meg = "abcdefg";
OutputStream os = null;
os = new FileOutputStream("F:/jdk-8u121/jdk-8u121.day28", true);
os.write(meg.getBytes());
System.out.println("文件写入成功");
os.close();
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
public class Demo2 {
public static void main(String[] args) {
//读取文件 输入流
InputStream is= null;
//创建输入流
try {
is = new FileInputStream("写入文件的路径");
//定义一个byte类型的数组,5代表一次可以读取5个
byte[] meg = new byte[5];
//定义一个int类型的变量将读的东西传进去
int length = is.read(meg);
//方法内部读完了返回-1
while(length != -1) {
//System.out.println("读取到的个数:"+length);
//参数1:要转成字符串的字节数组
//参数2:从字节数组的哪个元素开始转换
//参数3:转换几个元素 length就是全转
System.out.println(new String(meg,0,length));
//读完了接着读
length = is.read(meg);
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
try {
is.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
读取文件:
InputStream is= null;
is = new FileInputStream("F:/jdk-8u121/jdk-8u121.day28");
byte[] meg = new byte[5];
int length = is.read(meg);
while(length != -1) {
System.out.println(new String(meg,0,length));
length = is.read(meg);
}
is.close();
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
public class Demo2 {
public static void main(String[] args) {
//读取文件 输入流
InputStream is= null;
//创建输入流
try {
is = new FileInputStream("读取文件的路径");
//定义一个byte类型的数组,5代表一次可以读取5个
byte[] meg = new byte[5];
//定义一个int类型的变量将读的东西传进去
int length = is.read(meg);
//方法内部读完了返回-1
while(length != -1) {
//System.out.println("读取到的个数:"+length);
//参数1:要转成字符串的字节数组
//参数2:从字节数组的哪个元素开始转换
//参数3:转换几个元素 length就是全转
System.out.println(new String(meg,0,length));
//读完了接着读
length = is.read(meg);
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
try {
is.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
-------------------------------------------> 综合 读写放一起
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
//字节流
public class Demo5 {
public static void main(String[] args) {
//创建输入流
OutputStream os = null;
//放进缓存区
BufferedOutputStream bos = null;
//创建输出流
InputStream is = null;
//放进缓存区
BufferedInputStream bis = null;
String meg = "abcdefg";
try {
//写文件
os = new FileOutputStream("录入文件路径",true);
//写进缓存区
bos = new BufferedOutputStream(os);
//传进缓冲区
bos.write(meg.getBytes());
bos.flush();
System.out.println("文件写入成功,开始读取文件......");
//读取
is = new FileInputStream("读取文件路径");
//读到缓存区
bis = new BufferedInputStream(is);
//整行读取
byte[] str = new byte[5];
int length = bis.read(str);
while(length != -1) {
System.out.println(new String(str ,0 ,length));
length = bis.read(str);
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
try {
is.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
16、序列化与反序列化
序列化文件不可以手动创建
import java.io.Serializable;
public class Student implements Serializable{
private String stuName;
private int age;
public Student() {
super();
}
public Student(String stuName, int age) {
super();
this.stuName = stuName;
this.age = age;
}
public String getStuName() {
return stuName;
}
public void setStuName(String stuName) {
this.stuName = stuName;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "Student [stuName=" + stuName + ", age=" + age + "]";
}
}
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.List;
public class Demo6 {
public static void main(String[] args) {
Student zhangsan = new Student("张三",25);
Student lisi = new Student("李四",40);
List<Student> stus = new ArrayList<Student>();
stus.add(zhangsan);
stus.add(lisi);
FileOutputStream fos = null;
ObjectOutputStream oos = null;
try {
fos = new FileOutputStream("F:/JIHExuliehua.txt");
oos = new ObjectOutputStream(fos);
oos.writeObject(stus);
System.out.println("序列化成功......");
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
try {
oos.close();
fos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.util.ArrayList;
public class Test601 {
public static void main(String[] args) {
FileInputStream fis = null;
ObjectInputStream ois = null;
try {
fis = new FileInputStream("F:/JIHExuliehua.txt");
ois = new ObjectInputStream(fis);
ArrayList<Student>list = (ArrayList<Student>)ois.readObject();
//遍历集合输出
for(Student stu : list) {
System.out.println(stu);
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
17、File类
概念:代表物理盘符中的一个文件或者文件夹
方法:
createNewFile() //创建一个新文件
mkdir() //创建一个新目录
delete() // 删除文件或空目录
exists() //判断File对象所代表的对象是否存在
getAbsolutePath() //获取文件的绝对路径
getName() //取得名字
getParent() //获取文件 / 目录所在的目录
isDirectory() //是否是目录(文件夹)
isFile() //是否是文件
length() //获得文件的长度
listFiles() //列出目录中的所有内容
renameTo() //修改文件名为
18、FileFilter接口
public interface FileFilter
boolean accept(File pathname)
当调用File类中的listFiles()方法时,支持传入FileFilter接口接口实现类,对获取文件进行过滤,只有满足条件的文件 才可出现在listFiles()的返回值中。
import java.io.File;
import java.io.FileFilter;
public class Demo2 {
public static void main(String[] args) {
File file = new File("F:/");
File[] fs = file.listFiles(new FileFilter() {
@Override
public boolean accept(File pathname) {
//进行判断该路径当中的文件有没有.log结尾的
if(pathname.getName().endsWith(".log")) {
return true;
}else {
return false;
}
}
});
System.out.println("获取文件的个数" + fs.length);
for(File f : fs) {
System.out.println("文件名:" + f.getName());
}
}
}
19、Properties
Properties:属性集合。
特点:
存储属性名和属性值
属性名和属性值都是字符串类型
没有泛型
和流有关
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties;
public class Demo3 {
public static void main(String[] args) {
Properties pro = new Properties();
//通过字节流来指定文件
FileInputStream fis = null;
//通过Properties把文件流加载进来
try {
fis = new FileInputStream("读取文件路径");
//通过Properties把文件流加载进来
pro.load(fis);
//读取配置文件的内容
System.out.println("班级名称:" + pro.getProperty("className"));
System.out.println("班级人数:" + pro.getProperty("stuNum"));
System.out.println("学科:" + pro.getProperty("type"));
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
20、File综合
import java.io.File;
public class Demo1 {
public static void main(String[] args) {
//输入文件路径
File file=new File("E:/");
//new对象
Demo1 d1 = new Demo1();
//运用自己写的递归方法,一个传入的是文件,一个传入的是- 来进行分割
d1.findFile(file, "-");
}
//写递归方法查询的所有文件夹和文件 传参一个用来传文件路径一个用-进行标识
public void findFile(File file, String str) {
//获取文件名和文件夹名
System.out.println(str + file.getName());
//判断查询到的是不是文件是文件的话就进去继续寻找
if(file.isDirectory()) {
//再继续获取
//找文件夹下边的所有文件和文件夹
File[] files = file.listFiles();
//查询到一会自加-
str+=str;
//增强for循环进行遍历
for(File f : files) {
//递归调用
findFile(f ,str);
}
}
}
}
21、复制图片
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class Demo4 {
public static void main(String[] args) {
//先写File字节流的输入和输出
FileInputStream fis = null;
FileOutputStream fos = null;
//定义一个数组
//用来确定一次查多少
byte[] readImg = new byte[1024];
//定义一个长度用来判断是否都查额完了
//全查完了后边没东西输出-1 还有没查完的输出0
//具体一次差多少数组长度说了算
int length = 0;
try {
//new字节输入和字节输出对象
fis = new FileInputStream("F:/1.jpg");
fos = new FileOutputStream("F:/JDK/1.jpg");
//用fis里边的read方法读取fis这个路径里边的东西,一次读1024个
//读完1024个后长度为1024 返回0 再读 假如这次读了15个 然后没了 长度变成了15 返回0
//再读 因为上次读15证明后边没东西了 这次再读发现没有返回-1
//证明没有读的了
//边写边读
while((length = fis.read(readImg)) != -1) {
//readImg这个数组就是搬运工 存里边一次用fos里边的write写一次 存一次写一次
//length代表的是读哪个数组
//length代表这一次循环里边存在多少个内容 0 ~ length 就是全读
//直到循环结束,循环结束返回-1就证明没有东西可以读了
fos.write(readImg, 0 ,length);
}
System.out.println("图片复制成功!");
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
22、总结
流的概念:
内存与存储设备之间传输数据的通道
流的分类:
输入流、输出流、字节流、字符流、节点流(缓冲区)、过滤流(写);
序列化和反序列化:
将对象通过流写入到文件,或将对象通过流存储到内存,必须实现Serializable接口。
File对象:
代表物理盘符中的一个文件或者文件夹