IO简介
In/Out: 相对于程序而言的输入(读取)和输出(写出)的过程。
即: 通过java程序到磁盘读取数据的过程, 我们称为In的过程, 也就是读取(输入)
将java程序中的数据写入磁盘的过程, 我们称为Out的过程, 也就是写出(输出)
在Java中,根据处理的数据单位不同,分为字节流和字符流。
字节流: 一个字节(byte)一个字节的去读取, 或者写出
字符流: 一个字符一个字符的去读取, 或者写出
JDK核心类库中提供了IO流相关的类, 这些类都在<java.io>包下
流的概念
程序中数据的读取和写入, 可以想象成水流在管道中流动。
- 流只能单方向流动
 
- 输入流用来读取in
 
- 输出流用来写出Out
 
- 数据只能从头到尾顺序的读取一次或写出一次
 

节点流和处理流
按照流是否直接与特定的地方(如磁盘,内存,设备等)相连,分为节点流和处理流两类
节点流
可以从或向一个特定的地方(节点)读写数据
处理流
是对一个已存在的流的连接和封装,通过所封装的流的功能调用实现数据读写
处理流特点
处理流的构造方法总是要带一个其他的流对象做参数,一个流对象经过其他流的多次包装,成为流的链接.
通常节点流也被称之为低级流.处理流也被称之为高级流或者过滤流
不能独立存在, 必须要连接低级流
节点流
OutputStream抽象类
Output输出   Stream字节流
此抽象类是表示输出字节流的所有类的超类。输出流接受输出字节并将这些字节发送到某个接收器。
FileOutputStream文件输出流
直接插在文件上,直接写出文件数据
创建对象:
1 2 3 4 5 6
   | FileOutputStream(String name)  •     创建一个向具有指定名称的文件中写入数据的输出文件流。 FileOutputStream(File file)  •     创建一个向指定 File 对象表示的文件中写入数据的文件输出流。 FileOutputStream(File file, boolean append) –追加 •     创建一个向指定 File 对象表示的文件中写入数据的文件输出流。
   | 
 
注意: 以上构造方法中, 如果参数指向的文件以及父目录都不存在, 将会抛出FileNotFoundException异常!
如果参数指向的文件不存在, 但文件的所在目录存在, 将不会抛异常, 程序会自动创建该文件!
FileOutputStream代码案例
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
   | package io;  import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; 
 
 
 
  public class FOSDemo {     public static void main(String[] args) throws IOException {                                                               FileOutputStream fos = new FileOutputStream("./test.txt",true);                                             fos.write(1);         fos.write(97);         fos.write(98);         fos.write(99);         fos.write(13);         fos.write(10);                  fos.write("Hello EveryBody\r\n".getBytes());                  fos.write("ABCDE".getBytes(),1,3);         System.out.println("输出完成");                  fos.close();     } }
   | 
 
此抽象类是表示字节输入流的所有类的超类/抽象类。
直接插在文件上,直接读取文件数据。
创建对象
FileInputStream(File file)
通过打开一个到实际文件的连接来创建一个 FileInputStream,该文件通过文件系统中的 File 对象 file 指定。
FileInputStream(String pathname)
通过打开一个到实际文件的连接来创建一个 FileInputStream,该文件通过文件系统中的路径名 name 指定。
FileInputStream代码案例
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 io;  import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; 
 
 
  public class FISDemo {     public static void main(String[] args) throws IOException {         FileInputStream fis = new FileInputStream("./test.txt");                                                                        int data;                  while ((data = fis.read())!=-1){             System.out.print((char)data);         }                  fis.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 io;  import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; 
 
 
  public class CopyDemo {     public static void main(String[] args) throws IOException {                  FileInputStream fis = new FileInputStream("./ATM.jpeg");                  FileOutputStream fos = new FileOutputStream("./ATMCopy.jpeg");         int d;         long start = System.currentTimeMillis();         while ((d = fis.read())!=-1){                          fos.write(d);         }         long end = System.currentTimeMillis();         System.out.println("复制完毕!共耗时:"+(end-start)+"ms");                  fis.close();         fos.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
   | package io;  import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; 
 
 
 
  public class CopyDemo2 {     public static void main(String[] args) throws IOException {         FileInputStream fis = new FileInputStream("./aaa.jpg");         FileOutputStream fos = new FileOutputStream("./ccc.jpg");         
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
          
 
 
 
 
 
          byte[] data = new byte[1024*10];         int len;         long start = System.currentTimeMillis();         while ((len = fis.read(data))!=-1){                          fos.write(data,0,len);         }         long end = System.currentTimeMillis();         System.out.println("复制完毕!共耗时:"+(end-start)+"ms");         fis.close();         fos.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
   | package io;  import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.nio.charset.StandardCharsets; 
 
 
 
 
  public class WriteStringDemo {     public static void main(String[] args) throws IOException {                  FileOutputStream fos = new FileOutputStream("./demo.txt");         String line = "smell smelly,taste tasty";                                    byte[] data = line.getBytes(StandardCharsets.UTF_8);         fos.write(data);         fos.write("闻着臭,吃着香".getBytes(StandardCharsets.UTF_8));         fos.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
   | package io;  import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.nio.charset.StandardCharsets; import java.util.Scanner; 
 
 
 
 
 
 
  public class TestNotes {     public static void main(String[] args) throws IOException {         System.out.println("请开始输入内容,单独输入exit退出记事本!");                  Scanner scanner = new Scanner(System.in);                  FileOutputStream fos = new FileOutputStream("./note.txt");         while (true){                          String line = scanner.nextLine();                                                                 if ("exit".equalsIgnoreCase(line)){                 break;             }             fos.write(line.getBytes(StandardCharsets.UTF_8));         }                  fos.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
   | package io;  import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.nio.charset.StandardCharsets; import java.util.Scanner; 
 
 
 
 
 
 
  public class TestNotes {     public static void main(String[] args) throws IOException {         System.out.println("请开始输入内容,单独输入exit退出记事本!");                  Scanner scanner = new Scanner(System.in);                  FileOutputStream fos = new FileOutputStream("./note.txt",true);         while (true){                          String line = scanner.nextLine();                                                                 if ("exit".equalsIgnoreCase(line)){                 break;             }             fos.write(line.getBytes(StandardCharsets.UTF_8));         }                  fos.close();     } }
   | 
 
处理流
缓冲流
- BufferedOutputStream缓冲输出流
 
- BufferedInputStream 缓冲输入流
 
复制文件代码案例

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
   | package io;  import java.io.*; 
 
 
 
 
  public class CopyDemo3 {     public static void main(String[] args) throws IOException {         FileInputStream fis = new FileInputStream("./aaa.jpg");         BufferedInputStream bis = new BufferedInputStream(fis);         FileOutputStream fos = new FileOutputStream("./ddd.jpg");         BufferedOutputStream bos = new BufferedOutputStream(fos);                  int d;         long start = System.currentTimeMillis();         while ((d = bis.read())!=-1){             bos.write(d);         }         long end = System.currentTimeMillis();         System.out.println("耗时:"+(end-start)+"ms");                  bis.close();         bos.close();     } }
   | 
 
flush代码案例
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
   | package io;  import java.io.BufferedOutputStream; import java.io.FileOutputStream; import java.io.IOException; import java.nio.charset.StandardCharsets; 
 
 
 
 
  public class BOS_flushDemo {     public static void main(String[] args) throws IOException {         FileOutputStream fos = new FileOutputStream("./bos.txt");         BufferedOutputStream bos = new BufferedOutputStream(fos);         bos.write("你是我的眼~~~~~~".getBytes(StandardCharsets.UTF_8));                           bos.close();     } }
   | 
 
对象流

Person代码
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
   | package io;  import java.io.Serializable; import java.util.Arrays; 
 
 
 
 
 
 
 
 
 
 
 
  public class Person implements Serializable {          static final long serialVersionUID = 1L;     private String name;               private transient int age;     private String gender;     private transient String[] otherInfo;                    public Person(String name, int age, String gender, String[] otherInfo) {         this.name = name;         this.age = age;         this.gender = gender;         this.otherInfo = otherInfo;     }               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;     }     public String getGender() {         return gender;     }     public void setGender(String gender) {         this.gender = gender;     }     public String[] getOtherInfo() {         return otherInfo;     }     public void setOtherInfo(String[] otherInfo) {         this.otherInfo = otherInfo;     }               @Override     public String toString() {         return "Person{" +                 "name='" + name + '\'' +                 ", age=" + age +                 ", gender='" + gender + '\'' +                 ", otherInfo=" + Arrays.toString(otherInfo) +                 '}';     } }
   | 
 
OOSDemo案例
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 io;  import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.ObjectOutputStream; 
 
 
 
 
 
 
 
 
  public class OOSDemo {     public static void main(String[] args) throws IOException {         String name = "康荐文";         int age = 18;         String gender = "男";         String[] otherInfo = {"单身的","帅气的","网络鉴黄师","多金"};         Person p = new Person(name, age, gender, otherInfo);         System.out.println(p);                           FileOutputStream fos = new FileOutputStream("./person.obj");                  ObjectOutputStream oos = new ObjectOutputStream(fos);         
 
 
 
          oos.writeObject(p);         System.out.println("写出完毕!");                  oos.close();         
 
 
      } }
   | 
 
OISDemo案例
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 io;  import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.ObjectInputStream; 
 
 
 
 
  public class OISDemo {     public static void main(String[] args) throws IOException, ClassNotFoundException {         FileInputStream fis = new FileInputStream("./person.obj");                  ObjectInputStream ois = new ObjectInputStream(fis);         
 
 
          Person p = (Person) ois.readObject();         System.out.println(p);         ois.close();     } }
   | 
 
字节流和字符流
在Java中,根据处理的数据单位不同,分为字节流和字符流。
字节流: 一个字节(byte)一个字节的去读取, 或者写出
字符流: 一个字符一个字符的去读取, 或者写出
字节流
字节流(stream):针对二进制文件(文本,图片,音频,视频…等)
InputStream(包含input都是输入流)
- FileInputStream
 
- BufferedInputStream
 
- ObjectInputStream
 
OutputStream(包含output都是输出流)
- FileOutputStream
 
- BufferedOutputStream
 
- ObjectOutputStream
 
字符流
字符流(Reader,Writer):针对文本文件,读写容易发生乱码现象,在读写时最好指定编码集为utf-8
Reader(Reader结尾的都是字符输入流)
- FileReader
 
- BufferedReader
 
- InputStreamReader
 
Writer(Writer结尾的都是字符输出流)
- FileWriter
 
- BufferedWriter
 
- OutputStreamWriter
 
- PrintWriter/PrintStream
 
转换字符流
OutputStreamWriter
代码案例
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
   | package io;  import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStreamWriter; import java.nio.charset.StandardCharsets; 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
  public class OSWDemo {     public static void main(String[] args) throws IOException {                           FileOutputStream fos = new FileOutputStream("./osw.txt");                                             OutputStreamWriter osw = new OutputStreamWriter(fos,StandardCharsets.UTF_8);         osw.write("super idol的笑容都没你的甜~~~");         osw.write("八月正午的阳光都没你耀眼");         osw.write("天天向上");         osw.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 io;  import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStreamReader; 
 
 
 
 
  public class ISRDemo {     public static void main(String[] args) throws IOException {                  FileInputStream fis = new FileInputStream("./osw.txt");                  InputStreamReader isr = new InputStreamReader(fis);                                             int d;         while ((d = isr.read())!=-1){             System.out.print((char)d);         }         isr.close();     } }
   | 
 
缓冲字符流
PrintWriter
代码案例
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
   | package io;  import java.io.FileNotFoundException; import java.io.PrintWriter; import java.io.UnsupportedEncodingException; import java.nio.charset.StandardCharsets; 
 
 
 
 
 
 
 
 
 
 
 
  public class PWDemo {     public static void main(String[] args) throws FileNotFoundException, UnsupportedEncodingException {                           PrintWriter pw = new PrintWriter("./pw.txt", "UTF-8");                  pw.println("该配合你演出的我视而不见");         pw.println("阿巴阿巴阿巴阿巴阿巴阿巴");         System.out.println("写出完毕");         pw.close();     } }
   | 
 
代码案例2
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
   | package io;  import java.io.*; import java.nio.charset.StandardCharsets; import java.util.Scanner;  public class PWDemo2 {     public static void main(String[] args) throws FileNotFoundException {                  FileOutputStream fos = new FileOutputStream("pw2.txt",true);                  OutputStreamWriter osw = new OutputStreamWriter(fos, StandardCharsets.UTF_8);                  BufferedWriter bw = new BufferedWriter(osw);                           PrintWriter pw = new PrintWriter(bw,true);         Scanner scanner = new Scanner(System.in);         while (true){             String line = scanner.nextLine();             if ("exit".equalsIgnoreCase(line)){                 break;             }             pw.println(line);         }         pw.close();     } }
   | 
 
