第二十三章 IO流

1 IO流

1.1 什么是IO?

I: Input
O: Output
通过IO可以完成硬盘文件的读和写

 

1.2 IO流的分类?

有多种分类方式:
一种方式是按照流的方向进行分类:
以内存作为参照物
往内存中进去,叫做输入,或者叫做读。
从内存中出来,叫做输出,或者叫做写。

另一种方式是按照读取数据方式不同进行分类:
  有的流是按照字节的方式读取数据,一次读取1个byte,等同于一次读取8个二进制位。
  这种流是万能的,什么类型的文件都可以读取,包括文件文件、图片、音频文件、视频文件等等。
  假设文件file1.txt,采用字节流的话是这样读的:
  a中国bc张三fe
  第一次读:一个字节,正好读到'a'
  第二次读:一个字节,正好读到'中'字符的一半
 
  有有流是按照字符的方式读取数据的,一次读取一个字符,这种流是为了方便读取普通文本而存在的,这种流不能读取:图片、音频、视频等文件,只能读取纯文本文件,连word文件都无法读取。
  假设文件file1.txt,采用字符流的话是这样读的:
  a中国bc张三fe
  第一次读:一个字符,'a'字符('a'字符在windows系统中占用1个字节)
  第二次读:一个字符,'中'字符('中'字符在windows系统中占用2个字节)


综上所述:流的分类
输入流、输出流
字节流、字符流

 

 

 

 

1.3 流应该怎么学习

Java中的IO流都已经写好了,我们程序员不需要关心,我们最主要还是掌握,在java中已经提供了哪些流,每个流的特点是什么,每个流对象上常用的方式有哪些
java中所有流都是在:java.io.*
java中主要还是研究:
怎么new流对象。
调用流对象的方法,哪个方法是读,哪个方法是写。

 

1.4 流的四大家族

四大家族的首领:
   java.io.InputStream // 字节输入流
   java.io.OutputStream // 字节输出流
   java.io.Reader // 字符输入流
   java.io.Writer // 字符输出流
   四大家族的首领都是抽象类。(abstract class)
   所有的流都实现了:
  java.io.Closeable接口,都是可关闭的,都有close()方法。
  流毕竟是一个管道,这个是内存和硬盘之间的通道,用完之后一定要关闭,不然会耗费(占用)很多资源。用完流一定要关闭。
所有的输出流都实现了:
java.io.Flushable接口,都是可刷新的,都有flush()方法。
养成一个好习惯,输出流在最终输出之后,一定要记得flush刷新一下。
这个刷新表示将通道/管道当中剩余未输出的数据强制输出完(清空管道)
刷新的作用就是清空管道。
注意:如果没有flush可能会导致丢失数据。
   
注意:在java中只要“类名”以Stream结尾的都是字节流。以"Reader/Writer"结尾的都是字符流。

 

1.5 java.io包下需要掌握的流有16个

文件专属:
   java.io.FileInputStream(掌握)
   java.io.FileOutputStream(掌握)
   java.io.FileReader
   java.io.FileWriter

转换流:(将字节流转换成字符流)
   java.io.InputStreamReader
   java.io.OutputStreamWriter

缓冲流专属:
   java.io.BufferedReader
   java.io.BufferedWriter
   java.io.BufferedInputStream
   java.io.BufferedOutputStream

数据流专属:
   java.io.DataInputStream
   java.io.DataOutputStream

标准输出流:
   java.io.PrintWriter
   java.io.PrintStream(掌握)

对象专属流:
   java.io.ObjectInputStream(掌握)
   java.io.OjbectOutputStream(掌握)

 

1.6 FileInputStream初步

FileInputStreamTest01.java

package com.bjpowernode.java.io;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

/*
java.io.FileInputStream:
   1、文件字节输入流,万能的,任何类型的文件都可以采用这个流来读。
   2、字节的方式,完成输入的操作,完成读的操作(硬盘 --> 内存)
*/
public class FileInputStreamTest01 {
   public static void main(String[] args) {
       FileInputStream fis = null;
       try {
           // 文件路径:E:\资料\刘晓宏\course\JavaProjects2\03-javaSE\temp(IDEA中会自动把\变成\\,因为java中\这个表示转义)
           // 以下都是采用了绝对路径
           // 创建文件字节输入流对象
           fis = new FileInputStream("E:\\资料\\刘晓宏\\course\\JavaProjects2\\03-javaSE\\temp");

           // 开始读
           int readData = fis.read();  // 这个方法的返回值是:读取到的“字节”本身。
           System.out.println(readData);  // 97

           readData = fis.read();
           System.out.println(readData);  // 98

           readData = fis.read();
           System.out.println(readData);  // 99

           readData = fis.read();
           System.out.println(readData);  // 100

           readData = fis.read();
           System.out.println(readData);  // 101

           readData = fis.read();
           System.out.println(readData);  // 102

           // 已经读到文件的末尾了,再读的时候读取不到任何数据,返回-1
           readData = fis.read();
           System.out.println(readData);  // -1

      } catch (FileNotFoundException e) {
           e.printStackTrace();
      } catch (IOException e) {
           e.printStackTrace();
      } finally {
           // 在finally语句块当中确保流一定关闭
           if (fis != null) {  // 避免空指针异常
               // 关闭流的前提是:流不是空,流是null的时候没必要关闭。
               try {
                   fis.close();
              } catch (IOException e) {
                   e.printStackTrace();
              }
          }
      }
  }
}

FileInputStreamTest02.java

package com.bjpowernode.java.io;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

/*
对第一个程序进制改进,循环方式

分析这个程序的缺点:
   一次读取一个字节byte,这样内存和硬盘交互太频繁,基本上时间/资源都是浪费在交互正面了。
   能不能一次读取多个字节呢?可以
*/
public class FileInputStreamTest02 {
   public static void main(String[] args) {
       FileInputStream fis = null;
       try {
           fis = new FileInputStream("E:\\资料\\刘晓宏\\course\\JavaProjects2\\03-javaSE\\temp");
           while(true) {
               int readData = fis.read();
               if(readData == -1) {
                   break;
              }
               System.out.println(readData);
          }

           // 改造while循环
           int readData = 0;
           while((readData = fis.read()) != -1) {
               System.out.println(readData);
          }

      } catch (FileNotFoundException e) {
           e.printStackTrace();
      } catch (IOException e) {
           e.printStackTrace();
      } finally {
           if (fis != null) {
               try {
                   fis.close();
              } catch (IOException e) {
                   e.printStackTrace();
              }
          }
      }
  }
}

 

1.7 IDEA中的当前路径/往byte数组中读

package com.bjpowernode.java.io;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

/*
int read(byte[] b)
   一次最多读取b.length个字节。
   减少硬盘和内存的交互,提高程序的执行效率。
   往byte[]数组当中读。
*/
public class FileInputStreamTest03 {
   public static void main(String[] args) {
       FileInputStream fis = null;
       try {
           // 相对路径的话呢?相对路径一定是从当前所在的位置作为起点开始找!
           // IDEA默认的当前路径是哪里?工程Project的根就是IDEA的默认当前路径
           // fis = new FileInputStream("tempfile2");
           // fis= new FileInputStream("chapter23/tempfile2");
           // fis= new FileInputStream("chapter23/src/tempfile3");
           fis= new FileInputStream("chapter23/src/com/bjpowernode/java/io/tempfile4");

           // 开始读,采用byte数组,一次读取多个字节,最多读取“数组.length”个字节
           byte[] bytes = new byte[4];  // 准备一个4个长度的byte数组,一次最多读取4个字节。
           int readCount = fis.read(bytes);
           // 这个方法的返回值是:读取到的字节数量。(不是字节本身)
           System.out.println(readCount);  // 第一次读到了4个字节。
           // 将字节数组全部转换成字符串
           // System.out.println(new String(bytes)); // abcd
           // 不应该全部都转换,应该是读取了多少个字节,转换多少个
           System.out.println(new String(bytes, 0, readCount));

           readCount = fis.read(bytes);  // 第二次只能读取到2个字节。
           System.out.println(readCount);  // 2
           // 将字节数组全部转换成字符串
           // System.out.println(new String(bytes)); // efcd
           System.out.println(new String(bytes, 0, readCount));

           readCount = fis.read(bytes);  // 一个字节都没有读到,返回-1
           System.out.println(readCount);  // -1

      } catch (FileNotFoundException e) {
           e.printStackTrace();
      } catch (IOException e) {
           e.printStackTrace();
      } finally {
           if (fis != null) {
               try {
                   fis.close();
              } catch (IOException e) {
                   e.printStackTrace();
              }
          }
      }
  }
}

 

1.8 FileInputStream最终版

package com.bjpowernode.java.io;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

/*
最终版,需要掌握。
*/
public class FileInputStreamTest04 {
   public static void main(String[] args) {
       FileInputStream fis = null;
       try {
           fis = new FileInputStream("chapter23/src/tempfile3");
           // 准备一个byte数组
           byte[] bytes = new byte[4];
           /*
           while(true){
               int readCount = fis.read(bytes);
               if(readCount == -1) {
                   break;
               }
               // 把byte数组转换成字符串,读到多少个转换多少个。
               System.out.print(new String(bytes, 0, readCount));
           }
            */

           // 改进while循环
           int readCount = 0;
           while((readCount = fis.read(bytes)) != -1) {
               System.out.print(new String(bytes, 0, readCount));
          }

      } catch (FileNotFoundException e) {
           e.printStackTrace();
      } catch (IOException e) {
           e.printStackTrace();
      } finally {
           if (fis != null) {
               try {
                   fis.close();
              } catch (IOException e) {
                   e.printStackTrace();
              }
          }
      }
  }
}

 

1.8 FileInputStream的其它方法

package com.bjpowernode.java.io;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

/*
FileInputStream类的其它常用方法:
   int available()   // 返回流当中剩余的没有读到的字节数量
   long skip(long n) // 跳过几个字节不读
*/
public class FileInputStreamTest05 {
   public static void main(String[] args) {
       FileInputStream fis = null;
       try {
           fis = new FileInputStream("tempfile");
           System.out.println("总字节数量: " + fis.available());
           // 读1个字节
           // int readByte = fis.read();
           // 还剩下可以读的字节数量是多少?
           // System.out.println(fis.available()); // 5
           // 这个方法有什么用?
           // byte[] bytes = new byte[fis.available()]; // 这种方式不太适合太大的文件,因为byte[]不能太大。
           // 不需要循环了
           // 直接读一次就可以了
           // int readCount = fis.read(bytes);
           // System.out.println(new String(bytes));

           // skip跳过几个字节不读取,这个方法也可能以后会用
           fis.skip(3);  // 跳过3个byte
           System.out.println(fis.read());  // 100

      } catch (FileNotFoundException e) {
           e.printStackTrace();
      } catch (IOException e) {
           e.printStackTrace();
      } finally {
           if (fis != null) {
               try {
                   fis.close();
              } catch (IOException e) {
                   e.printStackTrace();
              }
          }
      }
  }
}

 

1.9 FileOutputStream的使用

package com.bjpowernode.java.io;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

/*
文件字节输出流,负责写。
从内存到硬盘。
*/
public class FileOutputStreamTest01 {
   public static void main(String[] args) {
       FileOutputStream fos = null;
       try {
           // myfile文件不存在的时候会自动新建
           // 这种方式谨慎使用,这种方式会先将原文件清空,然后重新写入。
           // fos = new FileOutputStream("myfile");
           // fos = new FileOutputStream("chapter23/src/tempfile3");
           // 以追加的方式在文件末尾写入,不会清空原文件内容
           fos = new FileOutputStream("chapter23/src/tempfile3", true);
           // 开始写
           byte[] bytes = {97, 98, 99, 100};
           // 将byte数组全部写出
           fos.write(bytes);  // abcd
           // 将byte数组的一部分写出
           fos.write(bytes, 0, 2);  // 再写出ab

           String s = "我是一个中国人,NB";
           byte[] bs = s.getBytes();
           fos.write(bs);

           // 写完之后,最后一定要刷新
      } catch (FileNotFoundException e) {
           e.printStackTrace();
      } catch (IOException e) {
           e.printStackTrace();
      } finally {
           if (fos != null) {
               try {
                   fos.close();
              } catch (IOException e) {
                   e.printStackTrace();
              }
          }
      }
  }
}

 

1.10 文件复制

package com.bjpowernode.java.io;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

/*
使用FileInputStream + FileOutputStream完成文件的拷贝。
拷贝的过程应该是一边读,一边写。
使用以上的字节流拷贝文件的时候,文件类型随意,万能的。
*/
public class Copy01 {
   public static void main(String[] args) {
       FileInputStream fis = null;
       FileOutputStream fos = null;
       try {
           // 创建一个输入流对象
           fis = new FileInputStream("E:\\桌面图片放映源\\9fc59d2b9ea43331116d0784260b60dd.jpg");
           // 创建一个输出流对象
           fos = new FileOutputStream("C:\\Users\\xiaohong\\Desktop\\a.pn");

           // 最核心的:一边读,一边写
           byte[] bytes = new byte[1024 * 1024];  // 1MB(一次最多拷贝1MB)
           int readCount = 0;
           while((readCount = fis.read(bytes)) != -1){
               fos.write(bytes, 0, readCount);
          }

           // 刷新,输出流最后要刷新
           fos.flush();
      } catch (FileNotFoundException e) {
           e.printStackTrace();
      } catch (IOException e) {
           e.printStackTrace();
      } finally {
           // 分开try,不要一起try
           // 一起try的时候,其中一个出现异常,可能会影响到另一外流的关闭。
           if (fos != null) {
               try {
                   fos.close();
              } catch (IOException e) {
                   e.printStackTrace();
              }
          }
           if (fis != null) {
               try {
                   fis.close();
              } catch (IOException e) {
                   e.printStackTrace();
              }
          }
      }
  }
}

 

1.11 FileReader的使用

package com.bjpowernode.java.io;

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

/*
FileReader:
   文件字符输入流,只能读取普通文本。
   读取文本内存时,比较方便,快捷。
*/
public class FileReaderTest {
   public static void main(String[] args) {
       FileReader reader = null;
       try {
           // 创建文件字符输入流
           reader = new FileReader("tempfile");
           // 准备一个char数组
           char[] chars = new char[20];
           // 往char数组中读
           reader.read(chars);
           for(char c : chars) {
               System.out.println(c);
          }

           // 开始读
           /*
           char[] chars = new char[4]; // 一次读取4个字符
           int readCount = 0;
           while((readCount = reader.read(chars)) != -1){
               System.out.print(new String(chars, 0, readCount));
           }
            */
      } catch (FileNotFoundException e) {
           e.printStackTrace();
      } catch (IOException e) {
           e.printStackTrace();
      } finally {
           if(reader != null) {
               try {
                   reader.close();
              } catch (IOException e) {
                   e.printStackTrace();
              }
          }
      }
  }
}

 

1.12 FileWriter的使用

package com.bjpowernode.java.io;

import java.io.FileWriter;
import java.io.IOException;

/*
FileWriter:
   文件字符输出流,写。
   只能输出普通文本
*/
public class FileWriterTest {
   public static void main(String[] args) {
       FileWriter fw = null;
       try {
           // 创建文件字符输出流对象
           // fw = new FileWriter("file");
           fw = new FileWriter("file", true);
           // 开始写
           char[] chars = {'我', '是', '中', '国', '人'};
           fw.write(chars);
           fw.write(chars, 2, 3);

           fw.write("我是一个小无赖");
           fw.write("\n");
           fw.write("hello world");

      } catch (IOException e) {
           e.printStackTrace();
      } finally {
           if (fw != null) {
               try {
                   fw.close();
              } catch (IOException e) {
                   e.printStackTrace();
              }
          }
      }
  }
}

 

1.13 复制普通文本文件

package com.bjpowernode.java.io;

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

/*
使用FileReader FileWriter进制拷贝的话,只能拷贝“普通文本”文件。
*/
public class Copy02 {
   public static void main(String[] args) {
       FileReader in = null;
       FileWriter out = null;
       try {
           // 读
           in = new FileReader("chapter23/src/com/bjpowernode/java/io/Copy02.java");
           // 写
           out = new FileWriter("Copy02.java");
           // 一边读一边写
           char[] chars = new char[1024 * 1024];
           int readCount = 0;
           while((readCount = in.read(chars)) != -1) {
               out.write(chars, 0, readCount);
          }

           // 刷新
           out.flush();
      } catch (FileNotFoundException e) {
           e.printStackTrace();
      } catch (IOException e) {
           e.printStackTrace();
      } finally {
           if (out != null) {
               try {
                   out.close();
              } catch (IOException e) {
                   e.printStackTrace();
              }
          }
           if (in != null) {
               try {
                   in.close();
              } catch (IOException e) {
                   e.printStackTrace();
              }
          }
      }
  }
}

 

1.14 带有缓冲区的字符流

package com.bjpowernode.java.io;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

/*
BufferedReader:
   带有缓冲区的字符输入流
   使用这个流的时候不需要自定义char数组,或者说不需要自定义byte数组,自带缓冲。
*/
public class BufferedReaderTest01 {
   public static void main(String[] args) throws IOException {

       FileReader reader = new FileReader("Copy02.java");
       // 当一个流的构造方法中需要一个流的时候,这个被传进来的流叫做:节点流
       // 外部这个负责包装的流,叫做:包装流,还有一个名字叫做:处理流。
       // 像当前这个程序来说:FileReader就是一个节点流。BufferedReader就是包装流或者处理流。
       BufferedReader br = new BufferedReader(reader);

       // 读一行
       /*
       String firstLint = br.readLine();
       System.out.println(firstLint);

       String secondLine = br.readLine();
       System.out.println(secondLine);
        */

       // br.readLine()方法读取一个文本行,但不带换行符
       String s = null;
       while((s = br.readLine()) != null) {
           System.out.println(s);
      }

       // 关闭流
       // 对于包装流来说,只需要关闭最外层流就行,里面的节点流会自动关闭。(可以看源代码)
       br.close();
  }
}

 

1.15 节点流和包装流

package com.bjpowernode.java.io;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.InputStreamReader;

/*
转换流:InputStreamReader
*/
public class BufferedReaderTest02 {
   public static void main(String[] args) throws Exception{
       /*
       // 字节流
       FileInputStream in = new FileInputStream("Copy02.java");

       // 通过转换流转换(InputStreamReader将字节流转换成字符流。)
       // in是节点流,reader是包装流
       InputStreamReader reader = new InputStreamReader(in);

       // 这个构造方法只能传一个字符流,不能传字节流
       // BufferedReader br = new BufferedReader(in);
       // reader是节点流,br是包装流。
       BufferedReader br = new BufferedReader(reader);
        */

       // 合并
       BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream("Copy02.java")));

       String line = null;
       while((line = br.readLine()) != null) {
           System.out.println(line);
      }

       // 关闭最外层
       br.close();
  }
}

 

1.16 带有缓冲区字符输出流

package com.bjpowernode.java.io;

import java.io.BufferedWriter;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.OutputStreamWriter;

/*
BufferedWriter: 带有缓冲的字符输出流
OutputStreamWriter: 转换流
*/
public class BufferedWriterTest {
   public static void main(String[] args) throws Exception {
       // 带有缓冲区的字符输出流
       // BufferedWriter out = new BufferedWriter(new FileWriter("copy"));
       // BufferedWriter out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("copy2")));
       BufferedWriter out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("copy2", true)));

       // 开始写
       out.write("hello world");
       out.write("\n");
       out.write("hello kitty!");

       // 刷新
       out.flush();

       // 关闭最外层
       out.close();

  }
}

 

1.17 数据流

package com.bjpowernode.java.io;

import java.io.DataInputStream;
import java.io.FileInputStream;

/*
DataInputStream: 数据字节输入流
DataOutputStream写的文件,只能使用DataInputStream去读,并且读的时候你需要提前知道写入的顺序。
读的顺序需要和写的顺序一致,才可以正常取出数据。

中国电信开发项目:
   
*/
public class DataInputStreamTest01 {
   public static void main(String[] args) throws Exception {
       DataInputStream dis = new DataInputStream(new FileInputStream("data"));
       // 开始读
       byte b = dis.readByte();
       short s = dis.readShort();
       int i = dis.readInt();
       long l = dis.readLong();
       float f = dis.readFloat();
       double d = dis.readDouble();
       boolean sex = dis.readBoolean();
       char c = dis.readChar();
       System.out.println(b);
       System.out.println(s);
       System.out.println(i + 1000);
       System.out.println(l);
       System.out.println(f);
       System.out.println(d);
       System.out.println(sex);
       System.out.println(c);

       dis.close();
  }
}

 

1.18 标准输出流

package com.bjpowernode.java.io;

import java.io.FileOutputStream;
import java.io.PrintStream;

/*
java.io.PrintStream: 标准的字节输出流,默认输出到控制台。
*/
public class PrintStreamTest {
   public static void main(String[] args) throws Exception{
       // 联合起来写
       System.out.println("hello World");

       // 分开写
       PrintStream ps = System.out;
       ps.println("hello xiaohong");
       ps.println("hello lisi");
       ps.println("hello wangwu");

       // 标准输出流不需要手动关闭
       // 可以改变标准输出流的输出方向吗?可以
       // 这些是之前System类使用过的方法和属性
       /*
       System.gc();
       System.currentTimeMillis();
       PrintStream ps2 = System.out;
       System.exit(0);
       System.arraycopy();
        */

       // 标准输出流不再指向控制台,指向“log”文件
       PrintStream printStream = new PrintStream(new FileOutputStream("log"));
       // 修改输出方式,将输出方向修改到“log”文件。
       System.setOut(printStream);
       // 再输出
       System.out.println("hello world");
       System.out.println("hello xiaohong");
       System.out.println("hello jingxian");
  }
}

Logger.java

package com.bjpowernode.java.io;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.PrintStream;
import java.text.SimpleDateFormat;
import java.util.Date;

/*
日志工具
*/
public class Logger {

   /*
   记录日志的方法
    */
   public static void log(String msg) {
       try {
           // 标准输出流指向一个日志文件
           PrintStream out = new PrintStream(new FileOutputStream("log.txt", true));
           // 改变输出方向
           System.setOut(out);
           // 日期当前时间
           Date nowTime = new Date();
           SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss SSS");
           String strTime = sdf.format(nowTime);

           System.out.println(strTime + ": " + msg);
      } catch (FileNotFoundException e) {
           e.printStackTrace();
      }
  }
}

LogTest.java

package com.bjpowernode.java.io;

public class LogTest {
   public static void main(String[] args) {
       // 测试工具类是否好用
       Logger.log("调用了System类的gc方法,建议启动垃圾回收");
       Logger.log("调用了UserServiced的doSome方法");
       Logger.log("用户尝试进制登录,验证失败");
  }
}

 

 

2 java.io.File类

2.1 File类的理解

package com.bjpowernode.java.io;

import java.io.File;

/*
File
   1 File类和四大家族没有关系, 所以File类不能完成文件的读和写.
   2 File对象代表什么?
       文件和目录路径名的抽象表示形式.
           F:\xiaohong // 这是一个File对象
           F:\xiaohong\a.txt // 这也是一个File对象
           一个File对象有可能对应的是目录,也可能是文件.
           File只是一个路径名的抽象表示形式
   3 需要掌握File类中常用的方法
*/
public class FileTest01 {
   public static void main(String[] args) throws Exception {
       // 创建一个File对象
       File f1 = new File("D:\\file");

       // 判断是否存在
       System.out.println(f1.exists());

       // 如果D:\file不存在,则以文件的形式创建出来
       /*
       if(!f1.exists()) {
           // 以文件形式新建
           f1.createNewFile();
       }
        */

       // 如果D:\file不存在,则以目录的形式创建出来
       /*
       if(!f1.exists()) {
           // 以目录的形式新建
           f1.mkdir();
       }
        */

       // 可以创建多重目录吗?
       /*
       File f2 = new File("D:/a/b/c/d/e/f");
       if(!f2.exists()) {
           f2.mkdirs();
       }
        */

       File f3 = new File("D:\\a\\b\\c");
       // 获取文件的父路径
       String parentPath = f3.getParent();
       System.out.println(parentPath);  // D:\a\b
       File parentFile = f3.getParentFile();
       System.out.println("绝对路径: " + parentFile.getAbsolutePath());  // 绝对路径: D:\a\b

       File f4 = new File("copy");
       System.out.println("绝对路径: " + f4.getAbsolutePath());  // 绝对路径: D:\java\javase\copy
  }
}

 

2.2 File类的常用方法

FileTest02.java

package com.bjpowernode.java.io;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;

/*
File类的常用方法
*/
public class FileTest02 {
   public static void main(String[] args) {
       File f1 = new File("E:\\资料\\刘晓宏\\course\\03-JavaSE\\课堂笔记\\typora笔记\\day29-第二十二章-集合2.md");
       // 获取文件名
       System.out.println("文件名: " + f1.getName());

       // 判断是否是一个目录
       System.out.println(f1.isDirectory());  // false

       // 判断是否是一个文件
       System.out.println(f1.isFile());  // true

       // 获取文件最后一次修改时间
       long haoMiao = f1.lastModified();  // 1970年到现在的总毫秒数
       // 将总毫秒数转换成日期
       Date time = new Date(haoMiao);
       SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss SSS");
       String strTime = sdf.format(time);
       System.out.println(strTime);

       // 获取文件大小的
       System.out.println(f1.length());  // 55157字节
  }
}

FileTest03.java

package com.bjpowernode.java.io;

import java.io.File;

/*
File中的listFiles方法.
*/
public class FileTest03 {
   public static void main(String[] args) {
       // File[] listFiles()
       // 获取当前目录下所有的子文件
       File f = new File("chapter23");
       File[] files = f.listFiles();
       // foreach
       for(File file : files) {
           System.out.println(file.getAbsolutePath());
           System.out.println(file.getName());
      }
  }
}

 

2.3 目录拷贝

package com.bjpowernode.java.io;

import java.io.*;

/*
拷贝目录
*/
public class CopyAll {
   public static void main(String[] args) {
       // 拷贝源
       File srcFile = new File("E:\\资料\\刘晓宏\\course\\02-JavaSE\\课堂笔记");
       // 拷贝目标
       File destFile = new File("C:\\Users\\xiaohong\\Desktop\\1\\");
       // 调用方法拷贝
       copyDir(srcFile, destFile);

  }

   private static void copyDir(File srcFile, File destFile) {
       if(srcFile.isFile()) {
           // srcFile如果是一个文件的话,递归结束
           // 是文件时候需要拷贝
           FileInputStream in = null;
           FileOutputStream out = null;
           if (!destFile.exists()) {
               destFile.mkdirs();
          }
           try {
               // 读这个文件
               in = new FileInputStream(srcFile);
               // 写到这个文件中
               String path = destFile.getAbsolutePath().endsWith("\\") ? destFile.getAbsolutePath() : destFile.getAbsolutePath() + "\\" + srcFile.getAbsolutePath().substring(3);
               out = new FileOutputStream(path);
               // 一边读一边写
               byte[] bytes = new byte[1024 * 1024];  // 一次复制1MB
               int readCount = 0;
               while((readCount = in.read(bytes)) != -1) {
                   out.write(bytes, 0, readCount);
              }

               out.flush();
          } catch (FileNotFoundException e) {
               e.printStackTrace();
          } catch (IOException e) {
               e.printStackTrace();
          } finally {
               if (out != null) {
                   try {
                       out.close();
                  } catch (IOException e) {
                       e.printStackTrace();
                  }
              }
               if (in != null) {
                   try {
                       in.close();
                  } catch (IOException e) {
                       e.printStackTrace();
                  }
              }
          }
           return;
      }
       // 获取源下面的子目录
       File[] files = srcFile.listFiles();
       // 代码不要一起写,要测试
       // System.out.println(files.length);
       for (File file : files) {
           // 获取所有文件的(包括目录和文件)绝对路径
           // System.out.println(file.getAbsolutePath());
           if (file.isDirectory()) {
               // 新建对应的目录
               // System.out.println(file.getAbsolutePath());
               String srcDir = file.getAbsolutePath();
               String destDir = destFile.getAbsolutePath().endsWith("\\") ? destFile.getAbsolutePath() : destFile.getAbsolutePath() + "\\" + srcDir.substring(3);
               // System.out.println(destDir);
               File newFile = new File(destDir);
               if(!newFile.exists()) {
                   newFile.mkdirs();
              }
          }
           // 递归调用
           copyDir(file, destFile);
      }
  }
}
 
 

3 序列化和反序列化

3.1 序列化和反序列化的理解

 

 

3.2 序列化的实现

ObjectOutputStreamTest01.java

package com.bjpowernode.java.io;

import com.bjpowernode.java.bean.Student;
import java.io.FileOutputStream;
import java.io.ObjectOutputStream;

/*
1 java.io.NotSerializableException:
   Student对象不支持序列化

2 参与序列化和反序列化的对象,必须实现Serializable接口。

3 注意:通过源代码发现,Serializable接口只是一个标志接口:
   public interface Serializable {
   }
   这个接口当中什么代码都没有
   那么它起到什么作用呢?
       起到标识的作用,标志的作用,java虚拟机看到这个类实现了这个接口,可能会对这个类进行特殊待遇。
       Serializable这个标志接口是给java虚拟机参考的,java虚拟机看到这个接口之后,会为该类自动生成一个序列化版本号。

4 序列化版本号有什么用呢?

*/

public class ObjectOutputStreamTest01 {
   public static void main(String[] args) throws Exception{
       // 创建java对象
       Student s = new Student(1111, "zhangsan");

       // 序列化
       ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("students"));

       // 序列化对象
       oos.writeObject(s);

       // 刷新
       oos.flush();

       // 关闭
       oos.close();
  }
}

Student.java

package com.bjpowernode.java.bean;

import java.io.Serializable;

public class Student implements Serializable {

   // java虚拟机看到Serializable接口之后,会自动生成一个序列化版本号。
   // 这里没有手动写出来,java虚拟机会默认提供这个序列化版本号。

   private int no;
   private String name;

   public Student() {
  }

   public Student(int no, String name) {
       this.no = no;
       this.name = name;
  }

   public int getNo() {
       return no;
  }

   public void setNo(int no) {
       this.no = no;
  }

   public String getName() {
       return name;
  }

   public void setName(String name) {
       this.name = name;
  }

   @Override
   public String toString() {
       return "Student{" +
               "no=" + no +
               ", name='" + name + '\'' +
               '}';
  }
}

 

3.3 反序列化的实现

package com.bjpowernode.java.bean;

import java.io.FileInputStream;
import java.io.ObjectInputStream;

/*
反序列化
*/
public class ObjectInputStreamTest01 {
   public static void main(String[] args) throws Exception{
       ObjectInputStream ois = new ObjectInputStream(new FileInputStream("students"));
       // 开始反序列化,读
       Object obj = ois.readObject();
       // 反序列化回来一个学生对象,所以会调用学生对象的toString方法
       System.out.println(obj);
       ois.close();
  }
}

 

3.4 序列化多个对象

User.java

package com.bjpowernode.java.bean;

import java.io.Serializable;

public class User implements Serializable {
   private int no;
   private String name;

   public User() {
  }

   public User(int no, String name) {
       this.no = no;
       this.name = name;
  }

   public int getNo() {
       return no;
  }

   public void setNo(int no) {
       this.no = no;
  }

   public String getName() {
       return name;
  }

   public void setName(String name) {
       this.name = name;
  }

   @Override
   public String toString() {
       return "User{" +
               "no=" + no +
               ", name='" + name + '\'' +
               '}';
  }
}

ObjectOutputStreamTest02.java

package com.bjpowernode.java.bean;

import java.io.FileOutputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.List;

/*
一次序列化多个对象呢?
   可以,可以将对象放到集合当中,序列化集合

提示:
   参与序列化的ArrayList集合以及集合中的元素User都需要实现java.io.Serializable接口
*/
public class ObjectOutputStreamTest02 {
   public static void main(String[] args) throws Exception{
       List<User> userList = new ArrayList<>();
       userList.add(new User(1, "zhangsan"));
       userList.add(new User(2, "lisi"));
       userList.add(new User(3, "wangwu"));
       ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("users"));

       // 序列化集合,这个集合对象中放了很多其他对象
       oos.writeObject(userList);

       oos.flush();
       oos.close();

  }
}

ObjectInputStreamTest02.java

package com.bjpowernode.java.bean;

import java.io.FileInputStream;
import java.io.ObjectInputStream;
import java.util.List;

/*
反序列化集合
*/
public class ObjectInputStreamTest02 {
   public static void main(String[] args) throws Exception{
       ObjectInputStream ois = new ObjectInputStream(new FileInputStream("users"));
       // Object obj = ois.readObject();
       // System.out.println(obj instanceof List);
       List<User> userList = (List<User>)ois.readObject();
       for(User user : userList) {
           System.out.println(user);
      }
       ois.close();
  }
}

 

3.5 transient关键字

package com.bjpowernode.java.bean;

import java.io.Serializable;

public class User implements Serializable {
   private int no;
   // transient关键字表示游离的,不参与序列化。
   private transient String name;  // 表示name不参与序列化操作

   public User() {
  }

   public User(int no, String name) {
       this.no = no;
       this.name = name;
  }

   public int getNo() {
       return no;
  }

   public void setNo(int no) {
       this.no = no;
  }

   public String getName() {
       return name;
  }

   public void setName(String name) {
       this.name = name;
  }

   @Override
   public String toString() {
       return "User{" +
               "no=" + no +
               ", name='" + name + '\'' +
               '}';
  }
}

 

3.6 关于序列化版本号

Student.java

package com.bjpowernode.java.bean;

import java.io.Serializable;

public class Student implements Serializable {

   // java虚拟机看到Serializable接口之后,会自动生成一个序列化版本号。
   // 这里没有手动写出来,java虚拟机会默认提供这个序列化版本号。
   // 建议将序列化版本号手动的与出来, 不建议自动生成
   // java虚拟机识别一个类的时候先通过类名, 如果类名一致, 再通过序列化版本号区分
   private static final long serialVersionUID = 8683452581121892123L;

   private int no;
   private String name;

   // 过了很久, Student这个类源代码改变了.
   // 源代码改动之后, 需要重新编译,编译之后生成全新的字节码文件.
   // 并且class文件再次运行的时候, java虚拟机生成的序列化版本号也会发生相应的改变.
   private int age;
   private String email;
   private String address;

   public Student() {
  }

   public Student(int no, String name) {
       this.no = no;
       this.name = name;
  }

   public int getNo() {
       return no;
  }

   public void setNo(int no) {
       this.no = no;
  }

   public String getName() {
       return name;
  }

   public void setName(String name) {
       this.name = name;
  }

   @Override
   public String toString() {
       return "Student{" +
               "no=" + no +
               ", name='" + name + '\'' +
               '}';
  }
}

ObjectOutputStreamTest01.java

1 java.io.NotSerializableException:
   Student对象不支持序列化

2 参与序列化和反序列化的对象,必须实现Serializable接口。

3 注意:通过源代码发现,Serializable接口只是一个标志接口:
   public interface Serializable {
  }
   这个接口当中什么代码都没有
   那么它起到什么作用呢?
       起到标识的作用,标志的作用,java虚拟机看到这个类实现了这个接口,可能会对这个类进行特殊待遇。
       Serializable这个标志接口是给java虚拟机参考的,java虚拟机看到这个接口之后,会为该类自动生成一个序列化版本号。

4 序列化版本号有什么用呢?
   java.io.InvalidClassException:
       com.bjpowernode.java.bean.Student;
       local class incompatible:
           stream classdesc serialVersionUID = -684255398724514298,
           local class serialVersionUID = -3463447116624555755

   java语言中是采用什么机制来区分类的?
       第一:首先通过类名进行比对,如果类名不一样,肯定不是同一个类。
       第二:如果类名一样,再怎么进行类的区分?靠序列化版本号进行区分。

   小明编写了一个类: com.bjpowernode .java.bean.Student implLements Serializable
   小宏编写了一个类: com.bjpowernode .java.bean.Student implements Serializable
   不同的人编写了同一个类,但“这两个类确实不是同一个类”。这个时候序列化版本就起上作用了。
   对于java虚拟机来说,java虚拟机是可以区分开这两个类的,因为这两个类都实现了Serializable接口,
   都有默认的序列化版本号,他们的序列化版本号不一样。所以区分开了。

   请思考?
       这种自动生成序列化版本号有什么缺陷?
           这种自动生成序列化版本号缺点是:一旦代码确定之后,不能进行后续的修改.
           因为只要修改,必然会重新编译,此时会生成新的序列化版本号,这个时候java
           虚拟机会认为这是一个全新的类.

   最终结论:
       凡是一个类实现了Serializable接口,建议给该类提供一个固定不变的序列化版本号.
       这样,以后这个类即使代码修改了,但是版本号不变,java虚拟机会认为是同一个类.


package com.bjpowernode.java.io;

import com.bjpowernode.java.bean.Student;
import java.io.FileOutputStream;
import java.io.ObjectOutputStream;

public class ObjectOutputStreamTest01 {
   public static void main(String[] args) throws Exception{
       // 创建java对象
       Student s = new Student(1111, "zhangsan");

       // 序列化
       ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("students"));

       // 序列化对象
       oos.writeObject(s);

       // 刷新
       oos.flush();

       // 关闭
       oos.close();
  }
}

ObjectInputStreamTest01.java

package com.bjpowernode.java.bean;

import java.io.FileInputStream;
import java.io.ObjectInputStream;

/*
反序列化
*/
public class ObjectInputStreamTest01 {
   public static void main(String[] args) throws Exception{
       ObjectInputStream ois = new ObjectInputStream(new FileInputStream("students"));
       // 开始反序列化,读
       Object obj = ois.readObject();
       // 反序列化回来一个学生对象,所以会调用学生对象的toString方法
       System.out.println(obj);
       ois.close();
  }
}

 

3.7 IO和Properties联合使用

IO流: 文件的读和写。
Properties: 是一个Map集合,key和value都是String类型
IO + Properties的联合应用

非常好的一个设计理念:
   以后经常改变的数据, 可以单独写到一个文件中, 使用程序动态读取
   将来只需要修改这个文件的内容, java代码不需要改动,不需要重新
   编译, 服务器也不需要重启, 就可以拿到动态的信息

   类似于以上机制的这种文件被称为配置文件
   并且当配置文件中的内容格式是:
       key1=value1
       key2=value2
   我们把这种配置文件叫做属性配置文件

   java规范中有要求: 属性配置文件建议以.properties结尾, 但这不是必须的
   这种以.properties结尾的文件在java中被称为: 属性配置文件
   其中Properties对象是专门存放属性配置文件内容的一个类


package com.bjpowernode.java.io;

import java.io.FileReader;
import java.util.Properties;

public class IOPropertiesTest01 {
   public static void main(String[] args) throws Exception{
       /*
       Properties是一个Map集合, key和value都是String类型
       想将userinfo文件中的数据加载到Properties对象当中
        */

       // 新建一个输入流对象
       FileReader reader = new FileReader("chapter23/userinfo.properties");

       // 新建一个Map集合
       Properties pro = new Properties();

       // 调用Properties对象的load方法将文件中的数据加载到Map集合中
       pro.load(reader);  // 文件中的数据顺着管道加载到Map集合中, 其中userinfo文件中相关数据等号左边做key, 等号右边做value

       // 通过key来获取value呢?
       String username = pro.getProperty("username");
       System.out.println(username);

       String password = pro.getProperty("password");
       System.out.println(password);

       String data = pro.getProperty("data");
       System.out.println(data);

       String age = pro.getProperty("age");
       System.out.println(age);
  }
}

userinfo.properties

#################在属性配置文件中井号是注释####################
# 建议key和value之间使用等号的方式
# 属性配置文件的key重复的话, value会自动覆盖
username=xiaohong
password=123456
# 最好不要有空格
data = 100
# 冒号也可以, 但是不建议用冒号
age:18
 
 

import com.bjpowernode.java.bean.Student;
import java.io.FileOutputStream;
import java.io.ObjectOutputStream;

/*
1 java.io.NotSerializableException:
   Student对象不支持序列化

2 参与序列化和反序列化的对象,必须实现Serializable接口。

3 注意:通过源代码发现,Serializable接口只是一个标志接口:
   public interface Serializable {
   }
   这个接口当中什么代码都没有
   那么它起到什么作用呢?
       起到标识的作用,标志的作用,java虚拟机看到这个类实现了这个接口,可能会对这个类进行特殊待遇。
       Serializable这个标志接口是给java虚拟机参考的,java虚拟机看到这个接口之后,会为该类自动生成一个序列化版本号。

4 序列化版本号有什么用呢?

*/

public class ObjectOutputStreamTest01 {
   public static void main(String[] args) throws Exception{
       // 创建java对象
       Student s = new Student(1111, "zhangsan");

       // 序列化
       ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("students"));

       // 序列化对象
       oos.writeObject(s);

       // 刷新
       oos.flush();

       // 关闭
       oos.close();
  }
}

Student.java

package com.bjpowernode.java.bean;

import java.io.Serializable;

public class Student implements Serializable {

   // java虚拟机看到Serializable接口之后,会自动生成一个序列化版本号。
   // 这里没有手动写出来,java虚拟机会默认提供这个序列化版本号。

   private int no;
   private String name;

   public Student() {
  }

   public Student(int no, String name) {
       this.no = no;
       this.name = name;
  }

   public int getNo() {
       return no;
  }

   public void setNo(int no) {
       this.no = no;
  }

   public String getName() {
       return name;
  }

   public void setName(String name) {
       this.name = name;
  }

   @Override
   public String toString() {
       return "Student{" +
               "no=" + no +
               ", name='" + name + '\'' +
               '}';
  }
}

 

3.3 反序列化的实现

package com.bjpowernode.java.bean;

import java.io.FileInputStream;
import java.io.ObjectInputStream;

/*
反序列化
*/
public class ObjectInputStreamTest01 {
   public static void main(String[] args) throws Exception{
       ObjectInputStream ois = new ObjectInputStream(new FileInputStream("students"));
       // 开始反序列化,读
       Object obj = ois.readObject();
       // 反序列化回来一个学生对象,所以会调用学生对象的toString方法
       System.out.println(obj);
       ois.close();
  }
}

 

3.4 序列化多个对象

User.java

package com.bjpowernode.java.bean;

import java.io.Serializable;

public class User implements Serializable {
   private int no;
   private String name;

   public User() {
  }

   public User(int no, String name) {
       this.no = no;
       this.name = name;
  }

   public int getNo() {
       return no;
  }

   public void setNo(int no) {
       this.no = no;
  }

   public String getName() {
       return name;
  }

   public void setName(String name) {
       this.name = name;
  }

   @Override
   public String toString() {
       return "User{" +
               "no=" + no +
               ", name='" + name + '\'' +
               '}';
  }
}

ObjectOutputStreamTest02.java

package com.bjpowernode.java.bean;

import java.io.FileOutputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.List;

/*
一次序列化多个对象呢?
   可以,可以将对象放到集合当中,序列化集合

提示:
   参与序列化的ArrayList集合以及集合中的元素User都需要实现java.io.Serializable接口
*/
public class ObjectOutputStreamTest02 {
   public static void main(String[] args) throws Exception{
       List<User> userList = new ArrayList<>();
       userList.add(new User(1, "zhangsan"));
       userList.add(new User(2, "lisi"));
       userList.add(new User(3, "wangwu"));
       ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("users"));

       // 序列化集合,这个集合对象中放了很多其他对象
       oos.writeObject(userList);

       oos.flush();
       oos.close();

  }
}

ObjectInputStreamTest02.java

package com.bjpowernode.java.bean;

import java.io.FileInputStream;
import java.io.ObjectInputStream;
import java.util.List;

/*
反序列化集合
*/
public class ObjectInputStreamTest02 {
   public static void main(String[] args) throws Exception{
       ObjectInputStream ois = new ObjectInputStream(new FileInputStream("users"));
       // Object obj = ois.readObject();
       // System.out.println(obj instanceof List);
       List<User> userList = (List<User>)ois.readObject();
       for(User user : userList) {
           System.out.println(user);
      }
       ois.close();
  }
}

 

3.5 transient关键字

package com.bjpowernode.java.bean;

import java.io.Serializable;

public class User implements Serializable {
   private int no;
   // transient关键字表示游离的,不参与序列化。
   private transient String name;  // 表示name不参与序列化操作

   public User() {
  }

   public User(int no, String name) {
       this.no = no;
       this.name = name;
  }

   public int getNo() {
       return no;
  }

   public void setNo(int no) {
       this.no = no;
  }

   public String getName() {
       return name;
  }

   public void setName(String name) {
       this.name = name;
  }

   @Override
   public String toString() {
       return "User{" +
               "no=" + no +
               ", name='" + name + '\'' +
               '}';
  }
}

 

3.6 关于序列化版本号

Student.java

package com.bjpowernode.java.bean;

import java.io.Serializable;

public class Student implements Serializable {

   // java虚拟机看到Serializable接口之后,会自动生成一个序列化版本号。
   // 这里没有手动写出来,java虚拟机会默认提供这个序列化版本号。
   // 建议将序列化版本号手动的与出来, 不建议自动生成
   // java虚拟机识别一个类的时候先通过类名, 如果类名一致, 再通过序列化版本号区分
   private static final long serialVersionUID = 8683452581121892123L;

   private int no;
   private String name;

   // 过了很久, Student这个类源代码改变了.
   // 源代码改动之后, 需要重新编译,编译之后生成全新的字节码文件.
   // 并且class文件再次运行的时候, java虚拟机生成的序列化版本号也会发生相应的改变.
   private int age;
   private String email;
   private String address;

   public Student() {
  }

   public Student(int no, String name) {
       this.no = no;
       this.name = name;
  }

   public int getNo() {
       return no;
  }

   public void setNo(int no) {
       this.no = no;
  }

   public String getName() {
       return name;
  }

   public void setName(String name) {
       this.name = name;
  }

   @Override
   public String toString() {
       return "Student{" +
               "no=" + no +
               ", name='" + name + '\'' +
               '}';
  }
}

ObjectOutputStreamTest01.java

1 java.io.NotSerializableException:
   Student对象不支持序列化

2 参与序列化和反序列化的对象,必须实现Serializable接口。

3 注意:通过源代码发现,Serializable接口只是一个标志接口:
   public interface Serializable {
  }
   这个接口当中什么代码都没有
   那么它起到什么作用呢?
       起到标识的作用,标志的作用,java虚拟机看到这个类实现了这个接口,可能会对这个类进行特殊待遇。
       Serializable这个标志接口是给java虚拟机参考的,java虚拟机看到这个接口之后,会为该类自动生成一个序列化版本号。

4 序列化版本号有什么用呢?
   java.io.InvalidClassException:
       com.bjpowernode.java.bean.Student;
       local class incompatible:
           stream classdesc serialVersionUID = -684255398724514298,
           local class serialVersionUID = -3463447116624555755

   java语言中是采用什么机制来区分类的?
       第一:首先通过类名进行比对,如果类名不一样,肯定不是同一个类。
       第二:如果类名一样,再怎么进行类的区分?靠序列化版本号进行区分。

   小明编写了一个类: com.bjpowernode .java.bean.Student implLements Serializable
   小宏编写了一个类: com.bjpowernode .java.bean.Student implements Serializable
   不同的人编写了同一个类,但“这两个类确实不是同一个类”。这个时候序列化版本就起上作用了。
   对于java虚拟机来说,java虚拟机是可以区分开这两个类的,因为这两个类都实现了Serializable接口,
   都有默认的序列化版本号,他们的序列化版本号不一样。所以区分开了。

   请思考?
       这种自动生成序列化版本号有什么缺陷?
           这种自动生成序列化版本号缺点是:一旦代码确定之后,不能进行后续的修改.
           因为只要修改,必然会重新编译,此时会生成新的序列化版本号,这个时候java
           虚拟机会认为这是一个全新的类.

   最终结论:
       凡是一个类实现了Serializable接口,建议给该类提供一个固定不变的序列化版本号.
       这样,以后这个类即使代码修改了,但是版本号不变,java虚拟机会认为是同一个类.


package com.bjpowernode.java.io;

import com.bjpowernode.java.bean.Student;
import java.io.FileOutputStream;
import java.io.ObjectOutputStream;

public class ObjectOutputStreamTest01 {
   public static void main(String[] args) throws Exception{
       // 创建java对象
       Student s = new Student(1111, "zhangsan");

       // 序列化
       ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("students"));

       // 序列化对象
       oos.writeObject(s);

       // 刷新
       oos.flush();

       // 关闭
       oos.close();
  }
}

ObjectInputStreamTest01.java

package com.bjpowernode.java.bean;

import java.io.FileInputStream;
import java.io.ObjectInputStream;

/*
反序列化
*/
public class ObjectInputStreamTest01 {
   public static void main(String[] args) throws Exception{
       ObjectInputStream ois = new ObjectInputStream(new FileInputStream("students"));
       // 开始反序列化,读
       Object obj = ois.readObject();
       // 反序列化回来一个学生对象,所以会调用学生对象的toString方法
       System.out.println(obj);
       ois.close();
  }
}

 

3.7 IO和Properties联合使用

IO流: 文件的读和写。
Properties: 是一个Map集合,key和value都是String类型
IO + Properties的联合应用

非常好的一个设计理念:
   以后经常改变的数据, 可以单独写到一个文件中, 使用程序动态读取
   将来只需要修改这个文件的内容, java代码不需要改动,不需要重新
   编译, 服务器也不需要重启, 就可以拿到动态的信息

   类似于以上机制的这种文件被称为配置文件
   并且当配置文件中的内容格式是:
       key1=value1
       key2=value2
   我们把这种配置文件叫做属性配置文件

   java规范中有要求: 属性配置文件建议以.properties结尾, 但这不是必须的
   这种以.properties结尾的文件在java中被称为: 属性配置文件
   其中Properties对象是专门存放属性配置文件内容的一个类


package com.bjpowernode.java.io;

import java.io.FileReader;
import java.util.Properties;

public class IOPropertiesTest01 {
   public static void main(String[] args) throws Exception{
       /*
       Properties是一个Map集合, key和value都是String类型
       想将userinfo文件中的数据加载到Properties对象当中
        */

       // 新建一个输入流对象
       FileReader reader = new FileReader("chapter23/userinfo.properties");

       // 新建一个Map集合
       Properties pro = new Properties();

       // 调用Properties对象的load方法将文件中的数据加载到Map集合中
       pro.load(reader);  // 文件中的数据顺着管道加载到Map集合中, 其中userinfo文件中相关数据等号左边做key, 等号右边做value

       // 通过key来获取value呢?
       String username = pro.getProperty("username");
       System.out.println(username);

       String password = pro.getProperty("password");
       System.out.println(password);

       String data = pro.getProperty("data");
       System.out.println(data);

       String age = pro.getProperty("age");
       System.out.println(age);
  }
}

userinfo.properties

#################在属性配置文件中井号是注释####################
# 建议key和value之间使用等号的方式
# 属性配置文件的key重复的话, value会自动覆盖
username=xiaohong
password=123456
# 最好不要有空格
data = 100
# 冒号也可以, 但是不建议用冒号
age:18
posted @   路走  阅读(33)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· DeepSeek “源神”启动!「GitHub 热点速览」
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· 我与微信审核的“相爱相杀”看个人小程序副业
· C# 集成 DeepSeek 模型实现 AI 私有化(本地部署与 API 调用教程)
· spring官宣接入deepseek,真的太香了~
点击右上角即可分享
微信分享提示