一、FileInputStream

1.可将读取的数据以byte数组的形式保存

  1. package javase.day31.io;
  2. import java.io.FileInputStream;
  3. import java.io.FileNotFoundException;
  4. import java.io.IOException;
  5. public class FileInputStreamTest04 {
  6. public static void main(String[] args) {
  7. FileInputStream fis = null;
  8. try {
  9. fis = new FileInputStream("io/src/tram");
  10. //准备一个byte数组
  11. byte [] bytes = new byte[4];
  12. /*
  13. while (true){
  14. int readCount = fis.read(bytes);
  15. if (readCount == -1){
  16. break;
  17. }
  18. System.out.println(new String(bytes,0,readCount));
  19. }
  20. */
  21. //好方法
  22. int readCount = 0;
  23. while ((readCount = fis.read(bytes))!=-1){
  24. System.out.println(new String(bytes,0,readCount));
  25. }
  26. } catch (FileNotFoundException e) {
  27. e.printStackTrace();
  28. } catch (IOException e) {
  29. e.printStackTrace();
  30. } finally {
  31. try {
  32. fis.close();
  33. } catch (IOException e) {
  34. e.printStackTrace();
  35. }
  36. }
  37. }
  38. }

2.返回剩余没读到的字节数量

int available()
image.png

3.跳过几个字节不读取

image.png

二、FileOutputStream

  1. package javase.day31.io;
  2. import java.io.FileNotFoundException;
  3. import java.io.FileOutputStream;
  4. import java.io.IOException;
  5. public class FileOutputStreamTest01 {
  6. public static void main(String[] args) {
  7. FileOutputStream fos = null;
  8. try {
  9. fos = new FileOutputStream("myfile",true);//加true加在原文件后面,不会清空
  10. byte[] bytes = {97,98,99,100,101};
  11. fos.write(bytes);
  12. //字符串
  13. String s ="我是在你的";
  14. //将字符串转换为byte数组
  15. byte[] bs = s.getBytes();
  16. fos.write(bs);
  17. //写完后一定要刷新
  18. fos.flush();
  19. } catch (FileNotFoundException e) {
  20. e.printStackTrace();
  21. } catch (IOException e) {
  22. e.printStackTrace();
  23. } finally {
  24. if (fos != null){
  25. try {
  26. fos.close();
  27. } catch (IOException e) {
  28. e.printStackTrace();
  29. }
  30. }
  31. }
  32. }
  33. }

三、复制文件(FileInputStream+FileOutputStream)

  1. package javase.day31.io;
  2. import java.io.FileInputStream;
  3. import java.io.FileNotFoundException;
  4. import java.io.FileOutputStream;
  5. import java.io.IOException;
  6. public class Copy01 {
  7. public static void main(String[] args) {
  8. FileInputStream fis = null;
  9. FileOutputStream fos = null;
  10. try {
  11. fis = new FileInputStream("myfile");
  12. fos = new FileOutputStream("copymyfile");
  13. byte [] bytes = new byte[1024];
  14. int readCount=0;
  15. while ((readCount = fis.read(bytes)) != -1){
  16. fos.write(bytes,0,readCount);
  17. }
  18. //刷新
  19. fos.flush();
  20. } catch (FileNotFoundException e) {
  21. e.printStackTrace();
  22. } catch (IOException e) {
  23. e.printStackTrace();
  24. } finally {
  25. if (fis !=null){
  26. try {
  27. fis.close();
  28. } catch (IOException e) {
  29. e.printStackTrace();
  30. }
  31. }
  32. if (fos !=null){
  33. try {
  34. fos.close();
  35. } catch (IOException e) {
  36. e.printStackTrace();
  37. }
  38. }
  39. }
  40. }
  41. }

四、FileReader

  1. package javase.day31.io;
  2. import java.io.FileNotFoundException;
  3. import java.io.FileReader;
  4. import java.io.IOException;
  5. public class FileReaderTest {
  6. public static void main(String[] args) {
  7. FileReader reader = null;
  8. try {
  9. reader = new FileReader("io/src/readerfile");
  10. //开始读
  11. char[] chars = new char[4];//一次4个字符
  12. int readCount = 0;
  13. while ((readCount = reader.read(chars))!=-1){
  14. System.out.println(new String(chars,0,readCount));
  15. }
  16. } catch (FileNotFoundException e) {
  17. e.printStackTrace();
  18. } catch (IOException e) {
  19. e.printStackTrace();
  20. } finally {
  21. if (reader !=null){
  22. try {
  23. reader.close();
  24. } catch (IOException e) {
  25. e.printStackTrace();
  26. }
  27. }
  28. }
  29. }
  30. }

五、FileWriter

  • 文件字符输出流(只能输出普通文本)
  • 可以直接写入字符串 ```java package javase.day31.io;

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

/**

  • 文件字符输出流。只能输出普通文本 */ public class FileWriterTest01 { public static void main(String[] args) {

    1. FileWriter out = null;
    2. try {
    3. out = new FileWriter("writerfile");
    4. //开始写
    5. char[] chars = {'我','是','中','国','人'};
    6. out.write(chars);
    7. out.write(chars,2,3);
    8. out.write("我是java工程师");
    9. //刷新
    10. out.flush();
    11. } catch (IOException e) {
    12. e.printStackTrace();
    13. }finally {
    14. if (out !=null){
    15. try {
    16. out.close();
    17. } catch (IOException e) {
    18. e.printStackTrace();
    19. }
    20. }
    21. }

    } }

  1. <a name="k11B4"></a>
  2. # 六、复制2(FileReader+FileWrite)
  3. ```java
  4. package javase.day31.io;
  5. import java.io.FileNotFoundException;
  6. import java.io.FileReader;
  7. import java.io.FileWriter;
  8. import java.io.IOException;
  9. public class Copy02 {
  10. public static void main(String[] args) {
  11. FileReader in =null;
  12. FileWriter out = null;
  13. try {
  14. in=new FileReader("writerfile");
  15. out=new FileWriter("copymyfile2");
  16. char[] chars = new char[1024 * 1024];
  17. int readCount = 0;
  18. while ((readCount = in.read(chars))!= -1){
  19. out.write(chars,0,readCount);
  20. }
  21. //刷新
  22. out.flush();
  23. } catch (FileNotFoundException e) {
  24. e.printStackTrace();
  25. } catch (IOException e) {
  26. e.printStackTrace();
  27. }finally {
  28. try {
  29. in.close();
  30. } catch (IOException e) {
  31. e.printStackTrace();
  32. }
  33. try {
  34. out.flush();
  35. } catch (IOException e) {
  36. e.printStackTrace();
  37. }
  38. }
  39. }
  40. }

七、BufferedReader

  1. package javase.day31.io;
  2. import java.io.BufferedReader;
  3. import java.io.FileNotFoundException;
  4. import java.io.FileReader;
  5. import java.io.IOException;
  6. public class BufferedReaderTest01 {
  7. public static void main(String[] args) {
  8. FileReader reader = null;
  9. BufferedReader br = null;
  10. try {
  11. reader = new FileReader("io/src/javase/day31/io/le.java");
  12. br = new BufferedReader(reader);
  13. String s = null;
  14. //br.readline()方法获取一个文本行,但不带换行符
  15. while ((s = br.readLine())!= null){
  16. System.out.println(s);
  17. }
  18. } catch (FileNotFoundException e) {
  19. e.printStackTrace();
  20. } catch (IOException e) {
  21. e.printStackTrace();
  22. } finally {
  23. try {
  24. //对于包装流来说,只需要关闭最外层流就行,里面的节点流会自动关闭(看源代码)
  25. br.close();
  26. } catch (IOException e) {
  27. e.printStackTrace();
  28. }
  29. }
  30. }
  31. }

八、BufferedWriter

  1. package javase.day31.io;
  2. import java.io.*;
  3. public class BufferedWriterTest {
  4. public static void main(String[] args) {
  5. BufferedWriter out = null;
  6. try {
  7. //out = new BufferedWriter(new FileWriter("BufferedWriterCopy"));
  8. out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("BufferedWriterCopy02",true)));
  9. } catch (IOException e) {
  10. e.printStackTrace();
  11. }finally {
  12. try {
  13. if (out !=null){
  14. out.close();
  15. }
  16. } catch (IOException e) {
  17. e.printStackTrace();
  18. }
  19. }
  20. }
  21. }

九、DataOutputStream

  1. package javase.day31.io;
  2. import java.io.DataOutputStream;
  3. import java.io.FileNotFoundException;
  4. import java.io.FileOutputStream;
  5. import java.io.IOException;
  6. public class DataOutputStreamTest {
  7. public static void main(String[] args) {
  8. DataOutputStream dos= null;
  9. try {
  10. //创建数据专属的字节输出流
  11. dos = new DataOutputStream(new FileOutputStream("data"));
  12. //写数据
  13. byte b =100;
  14. short s =200;
  15. int i =300;
  16. long l = 400;
  17. float f =3.0f;
  18. double d =3.14;
  19. boolean sex =false;
  20. char c= 'a';
  21. //写
  22. dos.writeByte(b);
  23. dos.writeShort(s);
  24. dos.writeInt(i);
  25. dos.writeLong(l);
  26. dos.writeFloat(f);
  27. dos.writeDouble(d);
  28. dos.writeBoolean(sex);
  29. dos.writeChar(c);
  30. //刷新
  31. dos.flush();
  32. } catch (FileNotFoundException e) {
  33. e.printStackTrace();
  34. } catch (IOException e) {
  35. e.printStackTrace();
  36. } finally {
  37. if (dos !=null){
  38. try {
  39. dos.close();
  40. } catch (IOException e) {
  41. e.printStackTrace();
  42. }
  43. }
  44. }
  45. }
  46. }

十、DataInputStream

  1. package javase.day31.io;
  2. import java.io.DataInputStream;
  3. import java.io.FileInputStream;
  4. import java.io.FileNotFoundException;
  5. import java.io.IOException;
  6. /*
  7. DataInputStream:数据字节输入流
  8. DataOutputStream:写的文件,只能用DataInputStream去读。并且读的时候你需要提前知道写入的顺序
  9. 读的顺序需要和写的顺序一致。才可以正常取出数据
  10. */
  11. public class DataInputStreamTest {
  12. public static void main(String[] args) {
  13. DataInputStream dis =null;
  14. try {
  15. dis = new DataInputStream(new FileInputStream("data"));
  16. } catch (FileNotFoundException e) {
  17. e.printStackTrace();
  18. }finally {
  19. if (dis !=null){
  20. try {
  21. dis.close();
  22. } catch (IOException e) {
  23. e.printStackTrace();
  24. }
  25. }
  26. }
  27. }
  28. }

十一、PrintStream(标准输出流)

可以改变输出方向吗?可以。

  1. package javase.day31.io;
  2. import java.io.FileNotFoundException;
  3. import java.io.FileOutputStream;
  4. import java.io.PrintStream;
  5. public class PrintStreamTest {
  6. public static void main(String[] args) throws FileNotFoundException {
  7. System.out.println("hello world!");
  8. PrintStream ps = System.out;
  9. ps.println("zhangsan");
  10. //标准输出流不需要手动close()关闭
  11. //之前System类使用的方法和属性
  12. // System.gc();
  13. // System.currentTimeMillis();
  14. // PrintStream ps2 = System.out;
  15. // System.exit(0);
  16. // System.arraycopy(...)
  17. //标准输出流不再指向控制台,指向“log”文件
  18. PrintStream printStream = new PrintStream(new FileOutputStream("log"));
  19. //修改输出方向,将输出方向修改到“log”文件
  20. System.setOut(printStream);
  21. //再输出
  22. System.out.println("hello world");
  23. System.out.println("hello litty");
  24. }
  25. }

十二、序列化和反序列化(两个流)

1.ObjectOutputStream和ObjectInputStream

  1. package bean;
  2. import java.io.Serializable;
  3. public class Student implements Serializable {
  4. private int no;
  5. private String name;
  6. public Student(){
  7. }
  8. public Student(int no, String name) {
  9. this.no = no;
  10. this.name = name;
  11. }
  12. public int getNo() {
  13. return no;
  14. }
  15. public void setNo(int no) {
  16. this.no = no;
  17. }
  18. public String getName() {
  19. return name;
  20. }
  21. public void setName(String name) {
  22. this.name = name;
  23. }
  24. @Override
  25. public String toString() {
  26. return "Student{" +
  27. "no=" + no +
  28. ", name='" + name + '\'' +
  29. '}';
  30. }
  31. }
  1. package bean;
  2. import java.io.FileOutputStream;
  3. import java.io.ObjectOutputStream;
  4. /*
  5. 1.Student对象不支持序列化
  6. 2.参与序列化和反序列化的对象,必须实现Serializable接口。
  7. 3.注意:通过源代码发现,这个接口只是一个标志性接口:标识的作用,虚拟机看到这个类实现了这个接口,可能会对这个类特殊待遇
  8. */
  9. public class ObjectOutputStreamTest01 {
  10. public static void main(String[] args) throws Exception {
  11. Student s = new Student(111,"zhangsan");
  12. //序列化
  13. ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("students"));
  14. //序列化对象
  15. oos.writeObject(s);
  16. //刷新
  17. oos.flush();
  18. oos.close();
  19. }
  20. }
  1. package bean;
  2. import java.io.*;
  3. public class ObjectInputStreamTest01 {
  4. public static void main(String[] args) throws IOException, ClassNotFoundException {
  5. ObjectInputStream ois = new ObjectInputStream(new FileInputStream("students"));
  6. Object obj = ois.readObject();
  7. System.out.println(obj);
  8. ois.close();
  9. }
  10. }

2.重点:

  1. 参与序列化的类型必须实现java.io.Serializable接口。<br /> **并且建议**将序列化版本号手动的写出来。<br /> private static final long serialVersionUID = 1L;

3.一次序列化多个对象

  1. transient(修饰的属性不参与序列化)

image.png

  1. user ```java package bean;

import java.io.Serializable;

public class User implements Serializable { private int no; //transient 游离的 不参与序列化操作 private transient String name;

  1. public User() {
  2. }
  3. public User(int no, String name) {
  4. this.no = no;
  5. this.name = name;
  6. }
  7. public int getNo() {
  8. return no;
  9. }
  10. public void setNo(int no) {
  11. this.no = no;
  12. }
  13. public String getName() {
  14. return name;
  15. }
  16. public void setName(String name) {
  17. this.name = name;
  18. }
  19. @Override
  20. public String toString() {
  21. return "User{" +
  22. "no=" + no +
  23. ", name='" + name + '\'' +
  24. '}';
  25. }

}

  1. 将多个序列化对象放到集合中,
  2. ```java
  3. package bean;
  4. import java.io.FileNotFoundException;
  5. import java.io.FileOutputStream;
  6. import java.io.IOException;
  7. import java.io.ObjectOutputStream;
  8. import java.util.ArrayList;
  9. import java.util.List;
  10. public class ObjectOutputStreamTest02 {
  11. public static void main(String[] args) throws IOException {
  12. List<User> userList = new ArrayList<>();
  13. userList.add(new User(111,"zhangsan"));
  14. userList.add(new User(122,"lucy"));
  15. userList.add(new User(133,"jack"));
  16. ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("users"));
  17. //序列化对象
  18. oos.writeObject(userList);
  19. oos.flush();
  20. oos.close();
  21. }
  22. }
  1. package bean;
  2. import java.io.FileInputStream;
  3. import java.io.ObjectInputStream;
  4. import java.util.LinkedList;
  5. import java.util.List;
  6. public class ObjectInputStreamTest02 {
  7. public static void main(String[] args) throws Exception{
  8. ObjectInputStream ois = new ObjectInputStream(new FileInputStream("users"));
  9. // Object obj = ois.readObject();
  10. // System.out.println(obj instanceof List);
  11. List<User> userList = (List<User>) ois.readObject();
  12. for (User user : userList){
  13. System.out.println(user);
  14. }
  15. ois.close();
  16. }
  17. }

4.序列化版本号

image.png