引言

Input/Output输入输出的意思,对于计算机而言它是非常基础的操作,IO流,指的是以流的方式来读写数据,流是一种形式或则方式,流生活中,比如水流 气流,都是一种物质传递的一种形式。引申含义就是数据传递

一. IO流分类

  1. 方向:输入流 和 输出流
  2. 内容:字节流 和 字符流

    提示 以内存(程序)为中心 进入内存为输入 出内存为输出。

13.IO流 - 图1

二.字节流

2.1 文件流[重点]

以字节形式读取文件数据,通常适用于读二进制文件 如图片 视频 音频文件等。

2.1.1 输入流 FileInputStream

  1. public static void main(String[] args) throws IOException {
  2. //创建流
  3. FileInputStream ins = new FileInputStream("D:\\test.txt");
  4. // 2. 读数据
  5. int l = (int)file.length();
  6. byte[] b = new byte[l];
  7. StringBuilder s = new StringBuilder();
  8. //循环读取字节
  9. while (ins.read(b)!=-1){
  10. String st = new String(b,0,b.length);
  11. s.append(st);
  12. }
  13. ins.close();
  14. System.out.println(s);
  15. }

2.1.2 输出流 FileOutStream

  1. public class FileOutputStreamCase {
  2. public static void main(String[] args) throws IOException {
  3. //1. 创建流
  4. FileOutputStream fos =
  5. new FileOutputStream("G:/word.txt",true);
  6. //2. 写数据
  7. String massage = "我喜欢景甜";
  8. fos.write( massage.getBytes() );
  9. //3 . 关闭流
  10. fos.close();
  11. }
  12. }

2.2 缓存流

没有特别功能,指定提供了缓冲,优先从缓存中读写,降低和IO设备访问次数提高效率。

2.2.1 输入流 BufferedInputStream

  1. public static void main(String[] args) throws IOException {
  2. //1. 创建流
  3. BufferedInputStream bis =
  4. new BufferedInputStream(
  5. new FileInputStream("G:/word.txt"));
  6. //2. 读文件
  7. byte[] buff = new byte[1024];
  8. int len = 0;
  9. while( (len=bis.read(buff)) !=-1 ){
  10. String xx = new String( buff, 0 , len );
  11. System.out.println(xx);
  12. }
  13. //3.关闭
  14. bis.close();
  15. }

2.2.2 输出流 BufferedOutputStream

  1. public static void main(String[] args) throws IOException {
  2. //1. 创建流
  3. BufferedOutputStream bos =
  4. new BufferedOutputStream(
  5. new FileOutputStream("G:/word.txt") );
  6. //2. 写数据
  7. String massage = "q床前明月光,疑是地上霜q";
  8. bos.write( massage.getBytes() );
  9. bos.flush(); //缓冲流的方法
  10. //3. 关闭流
  11. bos.close();
  12. }

2.3 序列化 反序列化

对象流作用是,可以把一个对象写到文件中,或者把一个文件中的对象,读取到内存。它就是序列化和反序列化的流:
序列化: 把对象二进制信息 写入流。
反序列化: 从流中读取对象的二进制信息,还原到内存。

一个类的对象支持序列化,那么这个类型必须实现一个 Serializable 标志接口。 细节: 一个类要可以被序列化,要求其属性也是可序列化的除非显示添加 transient 修饰 ,表示这个属性不参与序列化。静态属性不参与序列化

  1. public class Coder implements Serializable {
  2. public static int xx = 1000;
  3. //共性属性和行为
  4. private String name;
  5. private int age;
  6. private int workYear;
  7. private transient Computer computer; //临时属性不参与序列化
  8. public Coder() {
  9. }
  10. public Coder(String name, int age, int workYear) {
  11. this.name = name;
  12. this.age = age;
  13. this.workYear = workYear;
  14. }
  15. public Coder(String name, int age, int workYear, Computer computer) {
  16. this.name = name;
  17. this.age = age;
  18. this.workYear = workYear;
  19. this.computer = computer;
  20. }
  21. @Override
  22. public String toString() {
  23. return "Coder{" +
  24. "name='" + name + '\'' +
  25. ", age=" + age +
  26. ", workYear=" + workYear +
  27. ", computer=" + computer +" "+xx+
  28. '}';
  29. }
  30. }
  31. public static void main(String[] args) throws IOException {
  32. Coder coder = new Coder("张三",30,5);
  33. System.out.println(coder); //临时对象程序结束都没啦
  34. //1 创建流
  35. ObjectOutputStream oos = new ObjectOutputStream( new FileOutputStream("obj.dat") );
  36. //2. 写数据
  37. oos.writeInt(10);
  38. oos.writeInt(100);
  39. oos.writeObject( coder );
  40. //3.关闭
  41. oos.close();
  42. }

反序列化:

  1. public static void main(String[] args) throws IOException, ClassNotFoundException {
  2. //1. 创建流
  3. ObjectInputStream ois = new ObjectInputStream(new FileInputStream("obj.dat"));
  4. //2. 读,必须按照序列化顺序读取且不可多读,否则引发EOFException异常。
  5. int num = ois.readInt();
  6. int num2 = ois.readInt();
  7. Coder coder = (Coder) ois.readObject();
  8. System.out.println(num);
  9. System.out.println(num2);
  10. System.out.println(coder);
  11. //3.关闭
  12. ois.close();
  13. }

三.字符流

3.1 文件流[重点]

以子符形式读取文件数据,通常适用于读取纯文本文件 存文本信息。

3.1.1 FileReader 输入流

  1. public static void main(String[] args) throws IOException {
  2. //1 创建流
  3. FileReader reader = new FileReader("node.txt");
  4. //2 读数据
  5. char[] buff = new char[1024];
  6. int len=0;
  7. while ( (len=reader.read(buff)) !=-1 ){
  8. String str = new String( buff,0,len );
  9. System.out.println(str);
  10. }
  11. //3 关闭
  12. reader.close();
  13. }

3.1.2 FileWriter 输出流

  1. public static void main(String[] args) throws IOException {
  2. Scanner scanner = new Scanner(System.in);
  3. System.out.println("输入标题");
  4. String title = scanner.next();
  5. System.out.println("输入内容");
  6. String content = scanner.next();
  7. //把数据写到文件中
  8. //1. 创建
  9. FileWriter fw = new FileWriter("node.txt",true);
  10. //2. 写数据
  11. fw.write(title);
  12. fw.write("\n------------------------------------------\n");
  13. fw.write(content);
  14. fw.write("\n-----------------end----------------------\n");
  15. //3.关闭
  16. fw.close();
  17. }

3.2 缓冲流

3.2.1 BufferedReader 输入流

特别方法 readLine() 可以一次读取一行,如果未读到返回null

  1. BufferedReader BR = new BufferedReader(new FileReader("D:\\aa.txt"));
  2. String string = null;
  3. int count =0;
  4. while ((string=BR.readLine())!=null){
  5. count++;
  6. System.out.println("第"+count+"行:"+string);
  7. }
  8. BR.close();

3.2.2 BufferedWriter 输出流

特别方法 newLine() 相当于写一个换行符

  1. FileWriter fw = new FileWriter("D:\\aa.txt");
  2. BufferedWriter bw = new BufferedWriter(fw);
  3. bw.newLine(); //写一个回车 换行
  4. bw.write("wrsndm");
  5. bw.flush();
  6. bw.close();

3.3 转换流[重点]

有些时候,如果读取的文件是文本文件,但是提供的却是字节流,字符流明显读取文本字符更加方便,这时可以把字节流转换成字符流操作。

只有字节流转 字符流 没有字符流转字节流,转换流本身就是一种字符流。

3.3.1 InputStreamReader 输入流

  1. /**
  2. * 字节输入流 转 字符输入流
  3. */
  4. public class InputStreamReaderCase {
  5. public static void main(String[] args) throws IOException {
  6. //字符流
  7. BufferedReader br = new BufferedReader( new InputStreamReader( new FileInputStream("d:/text.txt") ) );
  8. String buff = null;
  9. while ( (buff=br.readLine()) !=null ){
  10. System.out.println(buff);
  11. }
  12. br.close();
  13. }
  14. }

3.3.2 OutputStreamWriter 输出流

  1. //字节流转字符流 输出
  2. FileOutputStream out = new FileOutputStream("D:\\aa.txt",true);
  3. OutputStreamWriter opt = new OutputStreamWriter(out,"GBK");
  4. opt.write("wrsndm");
  5. opt.flush();
  6. out.close();
  7. opt.close();

3.4 编码与字符集

编码格式:
字符集:charset
ASCII:英文字符,标点
iso8859:欧洲字符集
GBK,GBK2313,GB180:中文字符集
cjk:中日韩字符集
Unicode:全球的统一编码 包括世界上所有的字符和符号


字符编码:charsetEncoding 用一个或者多个字节来表示一个字符
ASCII:一个字节表示一个字符
GBK:用两个字节表示
utf-8:用1~3个字节表示一个字符
utf-16:用2个字节表示一个字符
utf-32:用4个字符表示一个字符

3.5 二进制流

DataOutputStream 写二进制文件
使用 writeUTF 写入字符串
writeInt 写入整数
DataInputStream 取二进制文件
readUtf 读取字符串
readInt 读取整数

  1. class Erjinzhi{
  2. public static void main(String[] args) throws IOException {
  3. //存数据
  4. // Studen s = new Studen("张三",21,22.0);
  5. // DataOutputStream da = new DataOutputStream(new FileOutputStream("D:\\aa.txt"));
  6. // da.writeUTF(s.getName());
  7. // da.writeInt(s.getAge());
  8. // da.writeDouble(s.getScore());
  9. // da.close();
  10. //取数据
  11. DataInputStream di = new DataInputStream(new FileInputStream("D:\\aa.txt"));
  12. String name = di.readUTF();
  13. int age = di.readInt();
  14. double score = di.readDouble();
  15. System.out.println("姓名:"+name+"\t年龄是:"+age+"\t分数是:"+score);
  16. di.close();
  17. }
  18. }

四.打印流

打印流也是输出流, 存在字节打印流和字符打印流。

字节打印流PrintStream

  1. public class PrintDemo {
  2. public static void main(String[] args) throws IOException {
  3. // 直接打印到文件
  4. PrintStream out = new PrintStream("d:/abc.txt");
  5. out.println("helloword");
  6. // 打印到 其他字节输出流
  7. FileOutputStream os = new FileOutputStream("d:/abc.txt",true);
  8. PrintStream out2 = new PrintStream( os);
  9. out2.println("java");
  10. }
  11. }

字符打印流 PrintWriter

  1. public class PrintDemo {
  2. public static void main(String[] args) throws IOException {
  3. //直接 打印到文件
  4. PrintWriter pw = new PrintWriter("D:/abc.txt");
  5. pw.println("css");
  6. pw.close();
  7. //打印 到其它字符输出流
  8. PrintWriter pw2 = new PrintWriter( new FileWriter("d:/abc.txt",true));
  9. pw2.println("java");
  10. pw2.close();
  11. //打印 到其他字节输出流
  12. PrintWriter pw2 = new PrintWriter( new FileOutputStream("d:/abc.txt",true) );
  13. pw2.println("java");
  14. pw2.close();
  15. }
  16. }

五.随机文件访问类

RandomAccessFile 此类是一个文件读写操作工具类,它和其他流不同,它没有方向性,它既可以读文件,也可以写文件。
支持模式: r 只读 w只写 rw读写
通过seek(int pos) 方法移动指针

  1. package print;
  2. import java.io.*;
  3. public class PrintDemo {
  4. public static void main(String[] args) throws IOException {
  5. //随机访问文件类
  6. RandomAccessFile raf = new RandomAccessFile("d:/abc.txt","rw");
  7. //移动光标到末尾 写数据
  8. raf.seek( raf.length() );
  9. raf.writeBytes("mysql");
  10. //移动光标到 开始 读操作
  11. raf.seek(0);
  12. String line = raf.readLine();
  13. System.out.println(line);
  14. raf.close();
  15. }
  16. }

六.File类[重点]

用于表示文件或者目录(路径) 的类型 就是File类,一个文件文件或者文件夹都是File的对象。

6.1. File 类实例化

实例化一个File对象,是否意味创建了一个实体文件? 答案是否定的,创建一个File对象仅仅是堆里创建了对象,如果本地没有实体文件存在,那么也不会创建这个文件。如果存在这个实体文体,那么就可以用对象获得实体文件的信息。

文件操作:
File类:表示磁盘上的文件或目录
File file = new File(“文件路径”);
常用方法:
exists() 判断文件或目录是否存在
createNewFile() 创建文件
is File() 判断是否时文件 是文件返回true
isDirectory() 判断是是否是目录 是目录返回true
getName() 获得名称
getPath() 获得路径
getAbsolutePath() 获取绝对路径
ListFiles() 获取指定目录下所有文件 返回File
lastModified() 获取文件或者目录最后的修改日期
length() 获取文件或目录的大小 以字节为单位
mkdirs() 创建文件夹
delete() 删除
getParent() 获取父目录

  1. public class demo1 {
  2. public static void main(String[] args) {
  3. File f = new File("D:\\aa.txt");
  4. Date date = new Date(f.lastModified());
  5. SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
  6. String d = sdf.format(date);
  7. if (f.exists()){
  8. System.out.println("文件名称"+f.getName());
  9. System.out.println("文件的绝对路径:"+f.getAbsolutePath());
  10. System.out.println("是否是文件夹:"+f.isFile());
  11. System.out.println(f.getParent());
  12. System.out.println(f.length()/1024/1024);
  13. System.out.println("最后的修改日期:"+d);
  14. }else{
  15. try {
  16. f.createNewFile();
  17. } catch (IOException e) {
  18. e.printStackTrace();
  19. }
  20. }
  21. File f1 = new File("D:\\aa");
  22. if (f1.mkdirs()){
  23. System.out.println("创建成功");
  24. }
  25. }
  26. }