流的概念
流的分类
按方向【重点】:
- 输入流:将<存储设备>中的内容读入到<内存>中
- 输出流:将<内存>中的内容写入到<存储设备>中
按单位:
- 字节流:以字节为单位,可以读写所有数据
- 字符流:以字符为单位,只能读写文本数据
按功能:
- 节点流:具有实际传输数据的读写功能
- 过滤流:在字节流的基础之上增强功能
字节流
字节流的父类(抽象类):
-
InputStream:字节输入流
-
OutputStream:字节输出流
文件字节流:
public int read(byte[] b) //从流中读取多个字节,将读到内容存入b数组,返回实际读到的字节数;如果达到文件的尾部,则返回-1
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
| package com.io;
import java.io.FileInputStream;
public class Demo01 { public static void main(String[] args) throws Exception{ FileInputStream fis = new FileInputStream("E:\\aaa.txt");
byte[] buf = new byte[1024];
int count = 0; while ((count=fis.read(buf))!=-1){ System.out.println(new String(buf,0,count)); } fis.close(); System.out.println("执行完毕"); } }
|
public void write(byte[] b) //一次写多个字节,将b数组中所有字节,写入输出流
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| package com.io;
import java.io.FileOutputStream; import java.nio.charset.StandardCharsets;
public class Demo02 { public static void main(String[] args) throws Exception{ FileOutputStream fos = new FileOutputStream("E:\\bbb.txt",true);
String string = "helloworld"; fos.write(string.getBytes()); fos.close(); System.out.println("执行完毕"); } }
|
字节流复制文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
| package com.io;
import java.io.FileInputStream; import java.io.FileOutputStream;
public class Demo03 { public static void main(String[] args) throws Exception{ FileInputStream fis = new FileInputStream("E:\\001.jpg"); FileOutputStream fos = new FileOutputStream("E:\\002.jpg"); byte[] buf = new byte[1024]; int count = 0; while ((count=fis.read(buf))!=-1){ fos.write(buf,0,count); } fis.close(); System.out.println("执行完毕"); } }
|
字节缓冲流
缓冲流:BufferedInputStream/BufferedOutputStream
- 提高IO效率,减少访问磁盘的次数
- 数据存储在缓存区中,flush是将缓存区的内容写入文件中,也可以直接close
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
| package com.io;
import java.io.BufferedInputStream; import java.io.FileInputStream;
public class Demo04 { public static void main(String[] args) throws Exception{ FileInputStream fis = new FileInputStream("E:\\aaa.txt"); BufferedInputStream bis = new BufferedInputStream(fis);
byte[] buf = new byte[1024]; int count = 0; while ((count=bis.read(buf))!=-1){ System.out.println(new String(buf,0,count)); } bis.close(); } }
|
BufferedOutputStream的使用
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| package com.io;
import java.io.BufferedOutputStream; import java.io.FileOutputStream; import java.nio.charset.StandardCharsets;
public class Demo05 { public static void main(String[] args) throws Exception{ FileOutputStream fos = new FileOutputStream("E:\\buffer.txt"); BufferedOutputStream bos = new BufferedOutputStream(fos); for (int i = 0; i < 10; i++) { bos.write("helloworld\r\n".getBytes()); bos.flush(); } bos.close(); } }
|
对象流
序列化
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
| package com.io;
import java.io.FileOutputStream; import java.io.ObjectOutputStream; import java.io.Serializable;
public class Demo06 { public static void main(String[] args) throws Exception{ FileOutputStream fos = new FileOutputStream("E:\\stu.bin"); ObjectOutputStream oos = new ObjectOutputStream(fos); Student zhangsan = new Student("张三", 20); oos.writeObject(zhangsan); oos.close(); System.out.println("序列化完毕"); } }
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 + '}'; } } */
|
反序列化
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| package com.io;
import java.io.FileInputStream; import java.io.ObjectInputStream;
public class Demo07 { public static void main(String[] args) throws Exception{ FileInputStream fis = new FileInputStream("E:\\stu.bin"); ObjectInputStream ois = new ObjectInputStream(fis); Student s =(Student) ois.readObject(); ois.close(); System.out.println("执行完毕"); System.out.println(s.toString()); } }
|
序列化与反序列化注意事项
- 序列化类必须要实现Serializable接口
- 序列化类中对象属性要求实现Serializable接口
- 序列化版本号ID,保证序列化的类和反序列化的类是同一个类
- 使用transient(瞬间的)修饰属性,这个属性不能序列化
- 静态属性不能序列化
- 序列化多个对象,可以借助集合实现
编码方式
当编码方式和解码方式不一致时,会出现乱码
字符流
字符流抽象类
字符流的父类(抽象类):
- Reader:字符输入流
- Writer:字符输出流
文件字符流
FileReader:
- public int read(char[] c) //从流中读取多个字符,将读到内容存入c数组,返回实际读到的字符数;如果达到文件的尾部,则返回-1
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
| package com.io;
import java.io.FileReader;
public class Demo08 { public static void main(String[] args) throws Exception{ FileReader fr = new FileReader("E:\\hello.txt");
char[] buf = new char[1024]; int count = 0; while ((count=fr.read(buf))!=-1) { System.out.println(new String(buf,0,count)); } fr.close(); } }
|
FileWriter:
- public void write(String str) //一次写多个字符,将b数组中所有字符,写入输出流
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| package com.io;
import java.io.FileWriter;
public class Demo09 { public static void main(String[] args) throws Exception{ FileWriter fw = new FileWriter("E:\\write.txt"); for (int i = 0; i < 10; i++) { fw.write("java是世界上最好的语言\r\n"); fw.flush(); } fw.close(); System.out.println("执行完毕"); } }
|
字符流复制文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
| package com.io;
import java.io.FileReader; import java.io.FileWriter;
public class Demo10 { public static void main(String[] args) throws Exception{ FileReader fr = new FileReader("E:\\write.txt"); FileWriter fw = new FileWriter("E:\\write2.txt"); int date = 0; while ((date=fr.read())!=-1) { fw.write(date); fw.flush(); } fr.close(); fw.close(); System.out.println("复制完毕 "); } }
|
字符缓冲流
缓冲流:BufferedReader/BufferedWriter
BufferedReader的使用
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
| package com.io;
import java.io.BufferedReader; import java.io.FileReader;
public class Demo11 { public static void main(String[] args) throws Exception{ FileReader fr = new FileReader("E:\\write.txt"); BufferedReader br = new BufferedReader(fr);
String line = null; while ((line= br.readLine())!=null) { System.out.println(line); } br.close(); } }
|
BufferedWriter的使用
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| package com.io;
import java.io.BufferedWriter; import java.io.FileWriter;
public class Demo12 { public static void main(String[] args) throws Exception{ FileWriter fw = new FileWriter("E:\\buffer.txt"); BufferedWriter bw = new BufferedWriter(fw); for (int i = 0; i < 10; i++) { bw.write("好好学习,天天向上"); bw.newLine(); bw.flush(); } bw.close(); System.out.println("执行完毕"); } }
|
打印流
PrintWriter:
- 封装了print() / println()方法,支持写入后换行
- 支持数据原样打印
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| package com.io;
import java.io.PrintWriter;
public class Demo13 { public static void main(String[] args) throws Exception{ PrintWriter pw = new PrintWriter("E:\\print.txt"); pw.println(97); pw.println(true); pw.println(3.14); pw.println('a'); pw.close(); System.out.println("执行完毕"); } }
|
转换流
桥转换流:InputStreamReader / OutputStreamWriter
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| package com.io;
import java.io.FileInputStream; import java.io.InputStreamReader;
public class Demo14 { public static void main(String[] args) throws Exception{ FileInputStream fis = new FileInputStream("E:\\write.txt"); InputStreamReader isr = new InputStreamReader(fis, "utf-8"); int date = 0; while ((date=isr.read())!=-1) { System.out.print((char)date); } isr.close(); } }
|
OutputStreamWriter的使用
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| package com.io;
import java.io.FileOutputStream; import java.io.OutputStreamWriter;
public class Demo15 { public static void main(String[] args) throws Exception{ FileOutputStream fos = new FileOutputStream("E:\\info.txt"); OutputStreamWriter osw = new OutputStreamWriter(fos,"gbk"); for (int i = 0; i < 10; i++) { osw.write("我爱北京,我爱故乡\r\n"); osw.flush(); } osw.close(); System.out.println("执行成功"); } }
|
File类
文件(夹)操作
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
| package com.io;
import java.io.File; import java.util.Date;
public class Demo16 { public static void main(String[] args) throws Exception { fileOpe(); } public static void separator() { System.out.println("路径分隔符"+ File.pathSeparator); System.out.println("名称分隔符"+ File.separator); } public static void fileOpe() throws Exception{ File file = new File("E:\\file.txt"); System.out.println(file.toString()); if (!file.exists()) { boolean b = file.createNewFile(); System.out.println("创建结果:"+b); }
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()).toLocaleString());
System.out.println("是否可写:"+file.canWrite()); System.out.println("是否是文件:"+file.isFile()); System.out.println("是否隐藏:"+file.isHidden()); } public static void directoryOpe() throws Exception{ File dir = new File("E:\\aaa\\bbb\\ccc"); System.out.println(dir.toString()); if (!dir.exists()) { System.out.println("创建结果:" + dir.mkdirs()); } dir.deleteOnExit(); Thread.sleep(5000); 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()); System.out.println("是否是文件夹:"+dir.isDirectory()); System.out.println("是否隐藏:"+dir.isHidden()); File dir2 = new File("E:\\图片"); String[] files = dir2.list(); System.out.println("================================="); for (String string : files) { System.out.println(string); } } }
|
FileFilter接口
- pubic interface FileFilter
boolean accept (File pathname)
- 当调用File类中的listFiles()方法时,支持传入FileFilter接口实现类,对获取文件进行过滤,只有满足条件的文件才可出现在listFiles()的返回值中
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
| package com.io;
import java.io.File; import java.io.FileFilter; import java.util.Date;
public class Demo16 { public static void main(String[] args) throws Exception { fileOpe(); } public static void separator() { System.out.println("路径分隔符"+ File.pathSeparator); System.out.println("名称分隔符"+ File.separator); } public static void fileOpe() throws Exception{ File file = new File("E:\\file.txt"); System.out.println(file.toString()); if (!file.exists()) { boolean b = file.createNewFile(); System.out.println("创建结果:"+b); }
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()).toLocaleString());
System.out.println("是否可写:"+file.canWrite()); System.out.println("是否是文件:"+file.isFile()); System.out.println("是否隐藏:"+file.isHidden()); } public static void directoryOpe() throws Exception{ File dir = new File("E:\\aaa\\bbb\\ccc"); System.out.println(dir.toString()); if (!dir.exists()) { System.out.println("创建结果:" + dir.mkdirs()); } dir.deleteOnExit();` Thread.sleep(5000); 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()); System.out.println("是否是文件夹:"+dir.isDirectory()); System.out.println("是否隐藏:"+dir.isHidden()); File dir2 = new File("E:\\图片"); String[] files = dir2.list(); System.out.println("================================="); for (String string : files) { System.out.println(string); }
System.out.println("=============FileFilter接口的使用"); File[] files2 = dir2.listFiles(new FileFilter() { @Override public boolean accept(File pathname) { if (pathname.getName().endsWith(".jpg")) { return true; } return false; } }); for (File file : files2) { System.out.println(file.getName()); } } }
|
递归遍历和递归删除
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
| package com.io.List;
import java.io.File;
public class ListDemo { public static void main(String[] args) { listDir(new File("E:\\aaa")); deleteDir(new File("E:\\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()); } } } }
public static void deleteDir(File dir) { File[] files = dir.listFiles(); if (files!=null&&files.length>0) { for (File file : files) { if (file.isDirectory()) { deleteDir(file); }else { System.out.println(file.getAbsolutePath()+"删除:"+file.delete()); } } } System.out.println(dir.getAbsolutePath()+"删除:"+dir.delete()); } }
|
补充:Properties
Properties:属性集合
特点:
- 存储属性名和属性值
- 属性名和属性值都是字符串类型
- 没有泛型
- 和流有关
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
| package com.io;
import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.PrintWriter; import java.util.Properties; import java.util.Set;
public class Demo17 { 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()); Set<String> pronames = properties.stringPropertyNames(); for (String pro : pronames) { System.out.println(pro+"===="+properties.getProperty(pro)); } PrintWriter pw = new PrintWriter("E:\\print.txt"); properties.list(pw); pw.close(); FileOutputStream fos = new FileOutputStream("E:\\store.properties"); properties.store(fos,"注释"); fos.close(); Properties properties2 = new Properties(); FileInputStream fis = new FileInputStream("E:\\store.properties"); properties2.load(fis); fis.close(); System.out.println(properties2.toString()); } }
|