IO框架
流的概念
什么是流?
- 概念:内存与储存设备之间传输数据的通道
流的分类
-
按流向【重点】
- 输入流:将 <存储设备> 中的内容读入到 <内存> 中 input
- 输出流:将 <内存> 中的内容写入到 <存储设备> 中 output
-
按单位
- 字节流:以字节为单位,可以读写所有数据
- 字符流:以字符为单位,只能读写文本数据
-
按功能:
- 节点流:具备实际传输数据的读写功能
- 过滤流:在节点流的基础之上增强功能
字节流
- 字节流的父类(抽象类):
- InputStream:字节输入流
- public int read()
- public int read(byte[] b)
- public int read(byte[] b,int off,int len)
- OutputStream:字节输出流
- public void write(int n)
- public void write(byte[] b)
- public void write(byte[] b,int off,int len)
- InputStream:字节输入流
文件字节流的两个子类
-
FileInputStream:
- public int read(byte [] b) 从流中读取多个字节,将读到的内容存入b数组,并返回实际读到的字节数;如果达到文件的尾部,则返回-1
-
FileOutputStream:
- public void write(byte [] b) 一次写多个字节,将b数组中所有字节,写入输入流
FileInputStream使用示例
/*
* 演示FileInputStream的使用
* 文件字节输入流
* */
public class Demo01 {
public static void main(String[] args) throws IOException {
//创建FileInputStream,并指定文件路径
FileInputStream fis = new FileInputStream("C:\\Xiang\\IOTest\\aaa.txt");
//读取文件
//方法1 fis.read() 单个字节读取
/*int data=0;
while((data=fis.read())!=-1){
System.out.print((char)data);
}
//============================================================
*/
//方法2 fis.read(byte[] b) 一次读取多个字节
/*
byte [] buf=new byte[3];
int count =fis.read(buf);
System.out.println(new String(buf,0,count));
System.out.println(count);
System.out.println("=====");
int count2=fis.read(buf);
System.out.println(new String(buf,0,count2));
System.out.println(count2);
System.out.println("=====");
int count3=fis.read(buf);
System.out.println(new String(buf,0,count3));
System.out.println(count3);
//=============================================================
*/
byte [] buf=new byte[3];
int count=0;
while((count=fis.read(buf))!=-1){
System.out.println(new String(buf,0,count));
}
//关闭
fis.close(); //注意关闭资源
System.out.println("执行完毕");
}
}
FileOutputStream使用示例
/*
* 演示文件字节输出流的使用
* FileOutputStream
* */
public class Demo02 {
public static void main(String[] args) throws Exception {
//创建一个文件字节输出流对象
FileOutputStream fos = new FileOutputStream("C:\\Xiang\\IOTest\\bbb.txt",true);
//创建对象时,在此处目标文件名后面加上参数true,每次输出的内容会加载后面,而不会取代前面的内容
//写入文件
//fos.write(97);
//fos.write('7');
//fos.write('b');
//fos.write('c');
String str="hello world ";
fos.write(str.getBytes()); //getBytes():将一个字符串转化为一个字节数组byte[]的方法
fos.close(); //注意关闭资源
System.out.println("执行完毕");
}
}
使用字节输入输出流实现文件的复制
/*
* 使用文件字节流实现文件的复制
* */
public class Demo03 {
public static void main(String[] args) throws IOException {
//1.创建流
//1.1文件字节输入流
FileInputStream fis = new FileInputStream("C:\\Xiang\\IOTest\\003.jpg");
//1.2文件字节输出流
FileOutputStream fos = new FileOutputStream("C:\\Xiang\\IOTest\\004.jpg");
//2.边读边写
byte [] buf=new byte[1024*1024];
int count=0;
while((count=fis.read(buf))!=-1){
fos.write(buf,0,count);
}
//3.关闭流
fis.close();
fos.close();
System.out.println("复制完毕");
}
}
字节缓冲流
- 缓冲流:BufferedInputStream/BufferedOutputStream
- 提高IO效率,减少访问磁盘的次数
- 数据出处在缓冲区中,flush()是将缓冲区的内容写入文件中,也可以直接close()
/*
* 使用字节缓冲流读取文件
* BufferedInputStream
* */
public class Demo04 {
public static void main(String[] args) throws IOException {
//1.创建BufferedInputStream
FileInputStream fis= new FileInputStream("C:\\Xiang\\IOTest\\aaa.txt");
BufferedInputStream bis = new BufferedInputStream(fis);
//2.读取
int data=0;
while((data=bis.read())!=-1){
System.out.print((char)data);
}
//fis.close(); 此处关闭字节输出流可以省略,因为在关闭缓冲流时,内部会将字节流也关掉
bis.close();
}
}
/*
* 使用字节缓冲流写入文件
* BufferedOutputStream
* */
public class Demo05 {
public static void main(String[] args) throws IOException {
//1.创建字节输出缓冲流
FileOutputStream fos = new FileOutputStream("C:/Xiang/IOTest/ccc.txt",true);
BufferedOutputStream bos = new BufferedOutputStream(fos);
//2.写入文件
for (int i = 0; i <100 ; i++) {
bos.write("hello world \r\n".getBytes());
}
//bos.flush(); //如果没有flush()的话,字节会保存在缓冲六中,不会写入到目标文件
bos.close(); //调用close()方法会自动flush()
}
}
对象流
- 对象流:ObjectOutputStream/ObjecttInputStream
- 增强了缓冲区功能
- 增强了读写8种基本数据类型和字符串功能
- 增强了读写对象的功能
- readObject() 从流中读取一个对象
- writeObject(Object obj) 向流中写入一个对象
使用流传输对象的过程称为序列化,反序列化
-
- 序列化:将对象存储到文件中 ObjectOutputStream
- 反序列化:从文件中读取对象 ObjecttInputStream
Serializable接口:是一个标记接口,作用就是标记这个类可以序列化,其中没有任何方法
每个要被序列化的类,都必须先实现Serializable接口
注意事项:
1.序列化必须实现Serializable接口
2.序列化类中,如果有对象属性,对象属性也要实现Serializable接口
3.序列化版本号ID,用来保证序列化的类和反序列化的类是同一个类
4.使用transient(瞬间的)修饰的属性,不能被序列化
5.静态属性不能序列化
6.序列化多个对象,可以借助集合实现
序列化
/*
* 使用ObjectOutputStream实现对象的序列化(把对象写入到文件)
* 要求:序列化的类,必须实现Serializable接口
* */
//实体类Student 必须实现Serializable接口
public class Student implements Serializable{
private String name;
private int age;
public Student() {
}
public Student(String name, int age) {
this.name = name;
this.age = age;
}
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;
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
//=================================================
public class Demo06 {
public static void main(String[] args) throws Exception {
//创建对象流
FileOutputStream fos = new FileOutputStream("c:/Xiang/IOTest/stu.bin");
ObjectOutputStream oos = new ObjectOutputStream(fos);
//序列化(写入操作)
Student s1 = new Student("果果", 3);
oos.writeObject(s1);
oos.close();
System.out.println("序列化完成");
/*
* Exception in thread "main" java.io.NotSerializableException: IO.Student
* 类不能序列化,需要将实体类实现java.io.Serializable接口
* Serializable接口是一个标记接口,作用就是标记这个类可以序列化,其中没有任何方法
* */
}
}
反序列化
/*
* 使用ObjectInputStream实现反序列化(读取重构文件)
* */
public class Demo07 {
public static void main(String[] args) throws Exception {
//创建对象流
FileInputStream fis = new FileInputStream("c:/Xiang/IOTest/stu.bin");
ObjectInputStream ois = new ObjectInputStream(fis);
//反序列化
//Object o = ois.readObject();
Student stu = (Student) ois.readObject();
System.out.println(stu);
ois.close();
System.out.println("反序列化完成");
}
}
编码方式
字符编码
- ISO-8859-1 1个字节,最多256个字符;收录出ASCII之外,还包括修、希腊语、泰语、阿拉伯语、希伯来语对应的文字符号
- UTF-8 国际字符集;针对Unicode码表的可变成都字符编码
- GB2312 简体中文
- GBK 简体中文、扩充
- BIG5 台湾,繁体中文
当编码方法上和解码方式不一致时,会出现乱码
字符流
- 字符流的父类(抽象类):
- Reader:字符输入流
- public int read()
- public int read(char [] c)
- public int read(char [] b,int off,int len )
- Writer:字符输出流
- public void writer(int n)
- public void writer(String str)
- public void writer(char [] c)
- Reader:字符输入流
文件字符流的两个子类
- FileReader:读取字符文件的类
- public int read(char [] c) 从流中读取多个字符,将独到的内容存入c数组,返回实际独到的字符数;如果达到文件底部,返回-1
- FileWriter:
- public void writer(String str) 一次写多个字符,将b数组中所有字符,写入输出流
FileReader读取文件
/*
* 使用FileReader读取文件
* */
public class Demo12 {
public static void main(String[] args) throws Exception {
//1.创建FileReader 文件字符输入流
FileReader fr = new FileReader("c:/Xiang/IOTest/hello.txt");
//idea默认UTF-8,电脑系统默认ANSI,将文档另存为是,在底下更改编码表为UTF-8,否则会使用FileReader读取会乱码
//2.读取
//单个字符读取
/*int data=0;
while((data=fr.read())!=-1){
System.out.print((char)data); //果果是条调皮的京巴狗
}*/
//创建缓冲区,一次读取多个
char [] buf=new char[4]; //为了演示,创建一个容量为4的缓冲区,要分3次读取 果果是 条调皮的 京巴狗
int count=0;
while ((count=fr.read(buf))!=-1){
System.out.println(new String(buf,0,count));
}
fr.close();
}
}
FileWriter写入文件
/*
* 使用FileWriter写入文件
* */
public class Demo13 {
public static void main(String[] args) throws IOException {
//创建FileWriter对象
FileWriter fw = new FileWriter("c:/Xiang/IOTest/writer.txt",true);
//写入
for (int i = 0; i <10 ; i++) {
fw.write("java学起来好累\r\n");
}
fw.flush();
fw.close();
System.out.println("写入完成");
}
}
复制文件(仅限文本文件)
/*
*使用FileReader和FileWriter复制文件
* 仅限文本文件
* */
public class Demo14 {
public static void main(String[] args) throws Exception {
//创建FileReader,FileWriter
FileReader fr = new FileReader("c:/Xiang/IOTest/bbb.txt");
FileWriter fw = new FileWriter("c:/Xiang/IOTest/fff.txt");
//读写
int data=0;
while((data=fr.read())!=-1){
fw.write(data);
}
//关闭流
fr.close();
fw.close();
System.out.println("复制完毕");
}
}
字符缓冲流
- 缓冲流:BufferedReader/BufferedWriter
- 高效读写
- 支持输入换行符
- 可一次写一行、读一行 写入时, newLine() 方法可以自动换行
- 缓冲区默认大小8K
/*
* 使用字符缓冲流读取文件
* BufferedReader
* */
public class Demo15 {
public static void main(String[] args) throws Exception {
//创建缓冲流
FileReader fr = new FileReader("c:/Xiang/IOTest/writer.txt");
BufferedReader br = new BufferedReader(fr);
//读取
//1.方式1
/*char[] buf=new char[1024];
int count=0;
while ((count=br.read(buf))!=-1){
System.out.print(new String(buf,0,count));
}*/
//关闭
//方式2,一次读一行 readLine()
String line=null;
while((line=br.readLine())!=null){
System.out.println(line);
}
br.close();
}
}
/*
* 演示BufferedWriter的使用
* */
public class Demo16 {
public static void main(String[] args) throws IOException {
//创建缓冲流
FileWriter fw = new FileWriter("c:/Xiang/IOTest/ggg.txt");
BufferedWriter bw = new BufferedWriter(fw);
//写入
for (int i = 0; i <10 ; i++) {
bw.write("努力挣钱,养家糊口"); //写入后的结果没有换行
bw.newLine(); //newLine() 方法可以自动换行
bw.flush();
}
bw.close();
System.out.println("执行完毕");
}
}
打印流
- PrintWriter:(PrintWriter和PrintStream方法完全一样,只是PrintStream支持字节流)
- 封装了print()/println()方法,支持写入后换行
- 支持数据原样打印
/*
* PrintWriter的使用
* */
public class Demo17 {
public static void main(String[] args) throws Exception {
//创建打印流
PrintWriter pw = new PrintWriter("c:/Xiang/IOTest/print.txt");
//打印
pw.println(97);
pw.println(true);
pw.println(3.14);
pw.println('c');
pw.println("CD");
pw.close();
System.out.println("执行完毕");
}
}
转换流
- 桥转换流:InputStreamReader/OutputStreamWriter
- InputStreamReader可将字节流转换成字符流/ OutputStreamWriter字符流–>字节流
- 可设置字符的编码方式
/*
* 使用InputStreamReader读取文件,指定使用的编码
* */
public class Demo18 {
public static void main(String[] args) throws Exception {
//1.创建InputStreamReader对象
FileInputStream fis = new FileInputStream("c:/Xiang/IOTest/writer.txt"); //writer.txt 编码是utf-8的,下面指定UTF-8可以正常读
//FileInputStream fis = new FileInputStream("c:/Xiang/IOTest/new.txt");
//new.txt 编码是ANSI的,在中国也就是GBK,下面指定UTF-8出来乱码
InputStreamReader isr = new InputStreamReader(fis, "GBK"); //ASCII也无法解析汉字;GBK解析utf-8会全部乱码
//2.读取文件
int data=0;
while ((data=isr.read())!=-1){
System.out.print((char)data);
}
isr.close();
}
}
/*
* 使用OutputStreamWriter写入文件,指定使用的编码
* */
public class Demo19 {
public static void main(String[] args) throws Exception {
//1.创建OutputStreamWriter
FileOutputStream fos = new FileOutputStream("c:/Xiang/IOTest/hhh.txt");
OutputStreamWriter osw = new OutputStreamWriter(fos,"GBK"); //此处指定GBK,在文件另存为是可以看到是ANSI
//2.写入数据
for (int i = 0; i <10 ; i++) {
osw.write("在湖北快冻死了");
osw.flush();
}
//3.关闭流
osw.close();
}
}
File类
- 概念:代表物理盘符中的一个文件或者文件夹
- 方法
- createNewFile() 创建一个新文件
- mkdir() 创建一个新目录、文件夹
- delete() 删除文件或空目录
- exists() 判断File对象是否存在
- getAbsolutePath() 获取文件的绝对路径
- getName() 获取名字
- getParent() 获取文件、目录所在的目录
- isDirectory() 判断是否是目录
- isFiled() 判断是否是文件
- length() 获取文件的长度
- listFiles() 列出目录中的所有内容
- renameTo() 修改文件名为
/*
* File类的使用
* 1.分隔符
* 2.文件操作
* 3.文件夹操作
* */
public class Demo21 {
public static void main(String[] args) throws IOException {
//separator();
fileOpe();
}
//1.分隔符
public static void separator(){
System.out.println("路径分隔符"+ File.pathSeparator); //路径分隔符;
System.out.println("名称分隔符"+File.separator); //名称分隔符\
}
//2.文件操作
public static void fileOpe() throws IOException {
//1.文件操作
File file=new File("c:/Xiang/IOTest/文件测试.txt"); //创建文件对象
//System.out.println(file.toString()); //电脑上实际不存在这个文件c:/Xiang/IOTest/文件测试.txt
if(!file.exists()) {
boolean b = file.createNewFile(); //调用createNewFile()方法后再真实地在电脑中创建这个文件
System.out.println(b); //true
}
//2.删除文件
//2.1直接删除
//System.out.println("删除结果"+file.delete());
//2.2使用jvm退出时删除,自动删除
file.deleteOnExit();
//3.获取文件信息
System.out.println("获取文件绝对路径:"+file.getAbsolutePath());
System.out.println("获取路径"+file.getPath());
System.out.println("获取文件名称"+file.getName());
System.out.println("获取父目录"+file.getParent());
System.out.println("获取文件长度"+file.length());
System.out.println("文件创建时间"+new Date(file.lastModified()));
//4.判断
System.out.println("是否可写"+file.canWrite());
}
}
/*
* File类的使用
* 1.分隔符
* 2.文件操作
* 3.文件夹操作
* */
public class Demo21 {
public static void main(String[] args) throws IOException {
directoryOpe();
}
//5.文件夹操作
public static void directoryOpe(){
//1.创建文件夹
File dir=new File("c:/Xiang/IOTest/aaa/bbb/ccc"); //此时电脑中还没有实际创建文件夹
System.out.println(dir.toString());
if(!dir.exists()){
//dir.mkdir(); mkdir()只能创建单级目录
System.out.println("文件夹创建结果:"+dir.mkdirs());
}
//2.删除文件夹
//2.1直接删除
//System.out.println("删除结果:"+dir.delete()); //只会删除最底层的ccc目录(且必须为空目录),上层的aaa、bbb不会删除
//2.2使用jvm退出时删除,自动删除
//dir.deleteOnExit();
//3.获取文件夹信息
System.out.println("获取绝对路径:"+dir.getAbsolutePath());
System.out.println("获取路径:"+dir.getPath());
System.out.println("获取名称:"+dir.getName());
System.out.println("获取父目录:"+dir.getParent());
System.out.println("获取创建时间:"+new Date(dir.lastModified()).toLocaleString());
//4.判断
System.out.println("是否是文件夹:"+dir.isDirectory());
System.out.println("是否是隐藏"+dir.isHidden());
//5.遍历文件夹
File dir2=new File("C:\\Xiang\\图片");
String [] files=dir2.list();
System.out.println("-----------------");
for (String file : files) {
System.out.println(file); //打印图片文件夹下所有文件名
}
//FileFilter调用File类中的listFiles()方法================================
File[] files2=dir2.listFiles(new FileFilter() {
public boolean accept(File pathname) {
if(pathname.getName().endsWith(".png")){
return true;
}
return false;
}
});
for (File file : files2) {
System.out.println(file.getName());
}
}
}
FileFilter
-
public interface FileFilter
- boolean accept(File pathname)
-
当调用File类中的listFiles()方法时,支持传入FileFilter接口实现类,对获取文件进行过滤,只有满足条件的文件才可以出现在listFiles()的返回值中
示例见前面代码末尾,只返回后缀名为.png的文件
递归遍历文件夹和递归删除文件夹
/*
* 递归遍历文件夹
* */
public class Demo22 {
public static void main(String[] args) {
listDir(new File("c:/Xiang/IOTest/aaa"));
}
public static void listDir(File dir){
File[] files = dir.listFiles();
System.out.println(dir.getAbsolutePath());
if(files!=null&&files.length>0){
for (File file : files) {
if(file.isDirectory()){
listDir(file);
}else{
System.out.println(file.getAbsolutePath());
}
}
}
}
}
Properties
- Properties:属性集合,可以保持在流中或从流中加载。属性列表中每个键及其对应值都是一个字符串
- 特点
- 存储属性名和属性值
- 属性名和属性值都是字符串类型
- 没有泛型
- 和流有关
/*
* 演示Properties集合的使用
* */
public class Demo24 {
public static void main(String[] args) throws Exception {
//创建集合
Properties properties = new Properties();
//添加数据
properties.setProperty("username","zhangsan");
properties.setProperty("age","20");
System.out.println(properties.toString());
//遍历
//3.1keySet()
Set<Object> objects = properties.keySet();
for (Object o : objects) {
System.out.println(o);
}
//3.2 entrySet()
//3.3 stringPropertiesNames ()
System.out.println("=============================");
Set<String> pronames = properties.stringPropertyNames();
for (String pro : pronames) {
System.out.println(pro+"======"+properties.getProperty(pro));
}
//和流有关的方法
//====1.list方法 列表====
//PrintWriter pw = new PrintWriter("c:/Xiang/IOTest/print.txt");
//properties.list(pw);
//pw.close();
//====2.store方法 保存====
FileOutputStream fos = new FileOutputStream("c:/Xiang/IOTest/print02.properties");
properties.store(fos,"注释");
fos.close();
//====3.load方法 加载====
Properties properties2 = new Properties();
FileInputStream fis = new FileInputStream("c:/Xiang/IOTest/print02.properties");
properties2.load(fis);
fis.close();
System.out.println(properties2.toString());
}
}