IO流

IO流:

  用来处理设备之间的数据传输。

  java对数据的操作是通过流的方法。

  java用于操作流的对象都在IO包中。

  流按操作数据氛围两种:字节流和字符流。

  流按流向分:输入流,输出流。

IO流图解:

  以内存为中心点。

  输入流和输出流相对于内存设备而言。

  将外设中的数据读取到内存中:输入;

  将内存的数据写入到外设中:输出。

  

字节流与字符流:

  字符流的由来:其实就是字节流读取文字字节数据后,不直接操作而是先查指定的编码表。获取对应的文字。再对这个文字进行操作。简单说:字节流+编码表。

四大基类(这些基类的子类都是以该基类作为后缀):

  字节流的抽象基类(顶层父类):InputStream,OutputStream

  字符流的抽象基类(顶层父类):Reader,Writer

  记住:如果要操作文字数据,建议有限考虑字符流;而且要将数据从内存写到硬盘上,要使用字符流中的输出流writer。

  硬盘的数据基本体现是文件。

  关闭流,关闭资源。在关闭前会先调用flush刷新缓冲中的数据到目的地。

换行与续写:

  windows:\r\n

  unix:\n

  System.getProperty("line.separator");

  如果构造函数中加入true,可以实现对文件进行续写!

复制文本文件图解:

 字符流的缓冲区:

  作用:缓冲区的出现提高了对数据的读写效率。

  对应类:BufferedWriter,BufferedReader。

  使用前提:缓冲区要结合流才可以使用。

  在流的基础上对流的功能进行了增强。

缓冲流写入示例代码:

 1 import java.io.BufferedWriter;
 2 import java.io.FileWriter;
 3 import java.io.IOException;
 4 
 5 public class Io {
 6     //private static final String LINE_SEPARATOR = System.getProperty("line.separator");
 7 
 8     public static void main(String[] args) throws IOException{
 9         FileWriter fw = new FileWriter("buf.txt");
10         //为了提高写入的效率,使用了字符流缓冲区。
11         //创建一个字符写入流的缓冲区对象,并和指定要被缓冲的流对象相关联。
12         BufferedWriter bufw = new BufferedWriter(fw);
13         
14         //使用缓冲区的写入方法将数据先写入到缓冲区中。
15         //bufw.write("abcd"+LINE_SEPARATOR+"ef");
16         bufw.write("abcd");
17         bufw.newLine();//只有BufferedWriter才有newLine();
18         bufw.write("ef");
19         //使用缓冲区的刷新方法将数据刷到目的地中。
20         bufw.flush();
21         //关闭缓冲区。其实关闭的就是被缓冲的流对象。
22         bufw.close();
23     }
24 
25 }

装饰设计模式:

  对一组对象的功能进行增强时,就可以使用该模式进行问题的解决。

示例代码:

 1 public class Io {
 2     //private static final String LINE_SEPARATOR = System.getProperty("line.separator");
 3 
 4     public static void main(String[] args){
 5         Person p = new Person();
 6         NewPerson p1 = new NewPerson(p);
 7         p1.chiFan();
 8     }
 9 
10 }
11 
12 class Person{
13     void chiFan(){
14         System.out.println("吃饭");
15     }
16 }
17 //这个类的出现是为了增强Person而出现的。
18 class NewPerson{
19     private Person p;
20     NewPerson(Person p){
21         this.p = p;
22     }
23     public void chiFan(){
24         System.out.println("开胃酒");
25         p.chiFan();
26         System.out.println("甜点");
27     }
28 }

LineNumberReader:行号

字节流:
  基本操作与字符流相同,但它不仅可以操作字符文件,还可以操作媒体文件。

  不需要flush()刷新流,但需要close()关闭资源。

  read():一次读取一个字节。

  available():读取出文件字节大小。

拷贝多媒体文件(图片,音乐,视频等):

 1 package javaday.day03;
 2 
 3 import java.io.BufferedInputStream;
 4 import java.io.BufferedOutputStream;
 5 import java.io.FileInputStream;
 6 import java.io.FileNotFoundException;
 7 import java.io.FileOutputStream;
 8 import java.io.IOException;
 9 
10 public class Io{
11     public static void main(String[] args){
12         try {
13             BufferedInputStream in = new BufferedInputStream(new FileInputStream("d:\\1.jpg"));
14             BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream("d:\\1\\2.jpg"));
15             int by = 0;
16             while((by=in.read())!=-1){
17                 out.write(by);
18             }
19             in.close();
20             out.close();
21         } catch (FileNotFoundException e) {
22             e.printStackTrace();
23         } catch (IOException e) {
24             e.printStackTrace();
25         }
26         
27     }
28 }

键盘录入:

  概念:读取一个键盘录入的数据,并打印在控制台上。

  键盘本身就是一个标准的输入设备。

  对于java而言,对于这种输入设备都有对应的对象。

 1 import java.io.IOException;
 2 import java.io.InputStream;
 3 
 4 
 5 public class Io {
 6     //private static final String LINE_SEPARATOR = System.getProperty("line.separator");
 7 
 8     public static void main(String[] args) throws IOException{
 9         readKey();
10     }
11 
12     private static void readKey() throws IOException {
13         InputStream in = System.in;
14         int ch = in.read();//阻塞式方法。没数据读取就等。
15         System.out.println(ch);
16         
17     }
18 
19 }

 附图:

对于上图,需掌握的重点:

转换流:

InputStreamReader:字节到字符的桥梁;

OutputStreamWriter:字符到字节的桥梁。

IO 包中的常见对象:

  字节流:

    FileInputStream

    FileOutputStream

    BufferedInputStream

    BufferedOutputStream

  字符流:

    FileReader

    FileWriter

    BufferedReader

    BufferedWriter

  转换流:

    InputStreamReader

    OutputStreamWriter

  文件对象:

    File

  打印流:

    PrintStream

    PrintWriter

原文地址:https://www.cnblogs.com/thinksasa/p/2741091.html