IO流 【java笔记】

来源:互联网 发布:mac装win10鼠标不能用 编辑:程序博客网 时间:2024/05/22 11:45
  1. 在Java程序中,对于数据的输入/输出操作以“流”(stream)——管道。方式进行;J2SDK提供了各种各样的“流”类,用以获取不同种类的数据;
  2. java.io包中定义了多个流类型(类或抽象类)来实现输入/输出功能;从不同角度对其进行分类:
    1. 按数据流的方向不同可以分为输入和输出流(站在程序的角度)。
    2. 按处理数据单位不同可以分为字节流和字符流。
    3. 按照功能不同可以分为节点流和处理流。
      1. 节点流为可以从一个特定的数据源(节点)读写数据(如:文件,内存)
      2. 处理流是“连接”在已存在的流(节点流或处理流)之上,通过对数据的处理为程序提供更为强大的功能。
  3. J2SDK所提供的所有流类型位于包java.io内都分别继承自以下四种抽象流类型:
    • 接口!字节流(Stream)字符流(两字节)输入流InputStreamReader输出流OutputStreamWriter

  4. InputStream:
    1. int read() throws IOException //读取一个字节
    2. int read(byte[] buffer) throws IOException //数组填满后在处理,返回读取个数
    3. int read(byte[] buffer,int offset,int length) throws IOException //开始存储位置,存储长度,返回读取个数
    4. void close() throws IOException
    5. long skip(long n)  throws IOException//跳过n个不读
  5. OutputStream:
    1. void write(int b)  throws IOException
    2. void write(byte[] b) throws IOException
    3. void write(byte[] b,int off, int len) throws IOException
    4. void flush() throws IOException
    5. void close() throws IOException
  6. Writer:
    1. void write(int c)  throws IOException
    2. void write(char[] c) throws IOException
    3. void write(char[] b,int off, int len) throws IOException
    4. void write(String s) throws IOException
    5. void write(String s,int off, int len) throws IOException
    6. void flush() throws IOException
    7. void close() throws IOException
  7. Reader:
    1. int read() throws IOException //读取一个字节
    2. int read(char[] buffer) throws IOException //数组填满后在处理,返回读取个数
    3. int read(char [] buffer,int offset,int length) throws IOException //开始存储位置,存储长度,返回读取个数
    4. void close() throws IOException
    5. long skip(long n)  throws IOException//跳过n个不读
  8. 文件路径中的'\\'可用'/'代替
  9. 形象思维,new一个数据流,想到一根水管插到水桶里,准备接水
  10. 节点流类型:
    1. File(文件)
    2. Memory Array
    3. Memory String
    4. Pipe(管道)
  11. 处理流类型:
    1. Buffering
    2. Filtering
    3. Converting between bytes and character
    4. Object Serialization
    5. Data conversion
    6. Counting
    7. Peeking ahead
    8. Printing
  12. 缓冲流:
    1. 缓冲流要“套接”在相应的节点流之上,对读写的数据提供了缓冲的功能,提高了读写的效率,同时增加了一些新的方法。
    2. J2SDK提供了四种缓存流,其常用的构造方法为:
      1. Bufferedreader(Reader in)
      2. Bufferedreader(Reader in,int sz)//sz为自定义缓存区大小
        1. br.readLine();
      3. BufferedWriter(Writer out)
      4. BufferedWriter(Writer out,int sz)
        1. bw.newLine();
        2. bw.write(String s);
      5. BufferedInputStream(InputStream in)
      6. BufferedInputStream(InputStream in,int size)
      7. BufferedOutputStream(OutputStream out)
      8. BufferedOutputStream(OutputStream out,int size)
    3. 缓冲输入流支持其父类的mark和reset方法。
    4. BufferedReader提供了readLine方法用于读取一行字符串(以\r或\n分隔)。
    5. BufferedWriter提供了newLine用于写入一个行分隔符。
    6. 对于输出的缓冲流,写出的数据会先在内存中缓存,使用flush方法将会使内存中的数据立刻写出。
  13. 转换流:
    1. InputStreamReader和OutpuStreamWriter用与字节数据到字符数据之间的转换
    2. InputStreamReader需要和InputStream“套接”;
    3. OutpuStreamWriter需要和OutpuStream“套接”;
    4. 转换流在构造时可以指定其编码集合,如:InputStream isr = new InputStreamReader(System.in,"ISO8859_1");
  14. System.in 标准输入流(static InputStream)
  15. 数据流:
    1. DataInputStream和DataOutputStream分别继承自InputStream和OutputStream,它属于处理流,需要分别“套接”在InputStream和OutputStream类型的节点流上。
    2. DataInputStream和DataOutputStream提供了可以存取与机器无关的Java原始数据类型(如:int,double等)的方法
    3. DataInputStream和DataOutputStream的构造方法为:
      1. DataInputStream(InputStream in)
      2. DataOutputStream(OutputStream out)
  16. ByteArrayOutputStream和ByteArrayInputStream为节点流。
    1. new ByteArrayOutputStream()执行两个操作:定义一个缓存数组(队列,先进先出),构建节点流。
    2. baos.toByteArray()返回该缓存数组
  17. Print流:
    1. PrintWriter和PrintStream都属于输出流,分别针对与字符和字节。
    2. PrintWriter和PrintStream提供了重载的print。
    3. Println方法用于多种数据类型的输出。
    4. PrintWriter和PrintStream的输出操作不会抛出异常,用户通过检测错误状态获取错误信息。
    5. PrintWriter和PrintStream有自动flush功能。
      1. PrintWriter(Writer out)
      2. PrintWriter(Writer out,boolean autoFlush)
      3. PrintWriter(OutputStream out)
      4. PrintWriter(OutputStream out,boolean autoFlush)
      5. PrintStream(OutputStream out)
      6. PrintStream(OutputStream out,boolean autoFlush)
    6. System.setOut(Print流);
  18. Object流:
    1. transient关键字(修饰成员变量,使透明,序列化时不写,为默认值)
    2. serializable接口(序列化,标志接口)
    3. externalizable接口(自己控制序列化过程,需要重写接口方法)
0 0
原创粉丝点击