概念:内存与存储设备之间传输数据的通道
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 blocke.printStackTrace();}finally {try {//关闭流fw.close();} catch (IOException e) {// TODO Auto-generated catch blocke.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 blocke.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}finally {try {fw.close();} catch (IOException e) {// TODO Auto-generated catch blocke.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 blocke.printStackTrace();} finally {try {bw.close();fw.close();br.close();fr.close();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}}
15、字节流的操作
录入文件:
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);//方法内部读完了返回-1while(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 blocke.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}finally {try {is.close();} catch (IOException e) {// TODO Auto-generated catch blocke.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);//方法内部读完了返回-1while(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 blocke.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}finally {try {is.close();} catch (IOException e) {// TODO Auto-generated catch blocke.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 blocke.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}finally {try {is.close();} catch (IOException e) {// TODO Auto-generated catch blocke.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;}@Overridepublic 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
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() {@Overridepublic 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 blocke.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch blocke.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、总结
流的概念:
内存与存储设备之间传输数据的通道
流的分类:
输入流、输出流、字节流、字符流、节点流(缓冲区)、过滤流(写);
序列化和反序列化:
将对象通过流写入到文件,或将对象通过流存储到内存,必须实现Serializable接口。
File对象:
代表物理盘符中的一个文件或者文件夹