IO系统

IO流

IO种类:文件、控制台、网络连接


1.字节流

1.1InputStream子类简单说明:

  • ByteArrayInputStream:包含一个内部缓冲区,该缓冲区中包含从流中读取的字节

  • FileInputStream:从文件系统的某个文件中获得输入字节,可以通过String、File或FileDescriptor 作为数据源使用

  • FilterInputStream:包含其他一些输入流,可将这些流作为基本数据源‘’

  • ObjectInputStream:与FileInputStream一起使用时,为应用程序提供对象图形的持久存储;用于恢复序列化的对象

  • PipedInputStream:管道输入流应该连接到管道输出流,提供要写到管道输出流的所有字节。通常,数据由某个线程从管道输入流读取,并由其他线程将其写入到相应的管道输出流

  • SequenceInputStream:其他输入流的逻辑串联,从输入流的有序集合开始,从第一个输入流开始读取,直到到达文件末尾,接着从第二个输入流读取,以此类推,直到到达包含的最后一个输入流的文件末尾位置

  • StringBufferInputStream:(已过时,不能将字符转换成字节,从字符串创建流的首选方法通过SringReader类进行创建)为应用程序创建输入流,流中数据由字符串内容提供

1.1.1FilterInputStream主要子类简单说明:

  • BufferedInputStream:为输入流添加功能,包括缓冲输入等,每次读取先在缓冲区中读取

  • DataInputStream:数据输入流,读取基本类型数据以及String对象,跨平台

  • DeflaterInputStream:为实现压缩数据实现输入流过滤器

  • InflaterInputStream:解压缩

1.2OutputStream子类简单说明:

  • ByteArrayOutputStream:在内存中创建一个缓冲区,发送给流的所有数据都会置入这个缓冲区(byte[]数组),缓冲区随着数据的写入不断增长

  • FileOutputStream:将数据写入File或FileDescriptor的输出流

  • FilterOutputStream:过滤输出流,提供一些额外功能

  • ObjectOutputStream:将java对象的基本数据类型和图形写入OutputStream,实现对象持久化

  • PipedOutputStream:管道输出流

1.2.1FilterOutputStream子类简单说明:

  • BufferedOutputStream:实现缓冲的输出流

  • DataOutputStream:数据输出流

  • DeflatOutputStream:压缩数据输出流过滤器

  • InflatOutputStream:解压缩数据输出流过滤器

  • PrintStream:格式化输出,比如输出到缓冲台

2.字符流

2.1Reader子类说明:

  • BufferReader:实现缓冲的字符输入流

  • FiletrReader:读取已过滤的字符流的抽象类

  • InputStreamReader:字节流与字符流之间的桥梁

    • FileReader:读取字符文件的便捷类
  • PipedReader:管道输入流

  • StringReader:源为字符串的输入流

2.2Writer子类说明:

  • BufferedWriter:实现缓冲的字符输出流

  • FilterWriter:写入已过滤的字符流的抽象类

  • OutputStreamWriter:字符流与字节流的桥梁

  • PipedWriter:管道输出流

  • PrintWriter:向文本输出流打印对象的格式化表示形式

  • StringWriter:源为字符串的输出流

3.RandomAcessFile:

包含了已知长度记录的文件,以便我们能用seek()从一条记录移至另一条,然后读取和修改记录,各记录长度并不一定相同,只要知道它们有多大位于文件何处即可。

不属于InputStream或OutputStream分层结构的一部分,与其他IO类型有完全不同的行为,我们可以在一个文件里向前或向后移动,搜索,标记位置等;类似InputStream和OutputStream的联合使用。

4.File:

既代表一个特定文件的名字,也代表目录内一系列文件的名字。

目录列表器:观看一个目录列表,可以有俩种方式。
1.不含参数情况下调用list()–得到完整的列表
2.目录过滤器–对显示进行限制

package com.zd.java.io;

import java.io.File;
import java.io.FilenameFilter;

/**
 * 列出目录列表,不含参数情况下调用list会获得file对象包含的完整列表
 * 若对列表进行限制,需要采用目录过滤器
 * 
 * 利用内部类创建精简的类,以解决一些复杂问题
 * Created by ZD on 2017/10/11.
 */
public class DirList3 {

    public static void main(final String[] args){
        File path = new File(".");
        String[] list;
        if (args.length == 0){
            list = path.list();
        }else {
            list = path.list(new FilenameFilter() {//目录过滤器
                @Override
                public boolean accept(File dir, String name) {//list()方法需要回调accept()方法
                    String f = new File(name).getName();
                    return f.indexOf(args[0]) != -1;
                }
            });
            for (int i = 0; i < list.length; i++){
                System.out.println(list[i]);
            }
        }
    }
}

File类不仅仅是对现有目录路径、文件或文件组的一个表示;也可用file对象新建一个目录,甚至创建一个完整的目录路径;也可用它了解文件的属性,检查一个file对象到底代表一个文件还是一个目录;以及删除一个文件。

5.IO字节流的典型应用:

尽管库内存在大量IO流内,可通过多种方式组合到一起,但实际上只有几种方式才会经常用到。
package com.zd.java.io;

import com.zd.java.io.quick.InFile;
import com.zd.java.io.quick.OutFile;
import com.zd.java.io.quick.PrintFile;
import java.io.*;

/**
 * IO字节流配置的创建与使用
 * Created by ZD on 2017/10/11.
 */
public class IOStreamDemo {

    public static void main(String[] args){

        try {
            //1.Buffered input file--缓冲的输入文件
            //打开文件FileInputStream,缓冲处理BufferedInputStream,格式化输入DataInputStream
            DataInputStream in = new DataInputStream(new BufferedInputStream(new FileInputStream("test.out"));
            String s,s2 = new String();//s2聚集完整的文件内容,包括换行,换行在readline中被去掉
            while ((s = in.readLine()) != null){
                s2 += s + "\n";
            }
            in.close();

            //2.Input from memory--从内存输入
            //s2已包含完整文件内容
            StringBufferInputStream in2 = new StringBufferInputStream(s2);//字符串缓冲输入流
            int c;
            while ((c = in2.read()) != -1){//使用read()依次读取每个字符,read()将下一个字节返回为int,需要造型
                System.out.println((char) c);
            }

            //3.formatted memory input-格式化内存输入
            //使用DataInputStream重新封装
            try{
                DataInputStream in3 = new DataInputStream(new StringBufferInputStream(s2));
                while (true){
                    System.out.println((char)in3.readByte());//readByte()每次读取一个字符,所有制均有效
                }
            }catch (EOFException e){
                System.out.println("end of stream encountered");
            }

            //4.line numbering & file output-行的编号与文件输出
            //LineNumberInputStream跟踪输入行的编号
            try{
                LineNumberInputStream li = new LineNumberInputStream(new StringBufferInputStream(s2));
                DataInputStream in4 = new DataInputStream(li);
                //格式化写入一个文件,FileOutputStream-连接文件,BufferedOutputStream-输出缓冲流,PrintStream--格式化输出
                PrintStream out1 = new PrintStream(new BufferedOutputStream(new FileOutputStream("IODemo.out")));
                while((s = in4.readLine()) != null){
                    out1.println("line "+li.getLineNumber());
                }
                out1.close();
            }catch (EOFException e){
                System.out.println("end of stream encountered");
            }


            //5.Storing & recovering data--保存与恢复数据
            //PrintStream--格式化数据,按人习惯阅读,但为输出数据,以便另一个数据流恢复,使用DataOutputStream写入数据
            //并用DataInputStream恢复数据
            try{
                DataOutputStream out2 = new DataOutputStream(new BufferedOutputStream(new FileOutputStream("Data.txt")));
                out2.writeBytes("here is the value of pi:\n");
                out2.writeDouble(3.14159);
                out2.close();
                DataInputStream in5 = new DataInputStream(new BufferedInputStream(new FileInputStream("Data.txt")));
                System.out.println(in5.readLine());
                System.out.println(in5.readDouble());
            }catch (EOFException e){
                System.out.println("end of stream encountered");
            }

            //6.reading/writing random access files--读写随机访问文件
            //RandomAccessFile
            RandomAccessFile rf = new RandomAccessFile("rtest.dat","rw");
            for (int i = 0; i < 10; i++){
                rf.writeDouble(i*1.414);
            }
            rf.close();

            rf = new RandomAccessFile("rtest.dat","rw");
            rf.seek(5*8);
            rf.writeDouble(47.0001);
            rf.close();

            rf = new RandomAccessFile("rtest.dat","r");
            for (int i = 0; i < 10; i++) {
                System.out.println("Value " + i + ":" + rf.readDouble());
            }
            rf.close();


            //7.file input shorthand--快速文件输入
            //减少创建文件时重复InputStream等操作
            InFile in6 = new InFile(args[0]);
            String s3 = new String();
            System.out.println("first line in file:"+
            in6.readLine());
            in6.close();

            //8.formatted file output shorthand--快速输出格式化恩建
            PrintFile out3 = new PrintFile("Data2.txt");
            out3.print("test of printFile");
            out3.close();


            //9.data file output shorthand--快速输出数据文件
            OutFile out4 = new OutFile("Data3.txt");
            out4.writeBytes("test of outData file\n\r");
            out4.writeChars("test of outdata file\n\r");
            out4.close();


        } catch (FileNotFoundException e) {
            System.out.println("file not found:"+args[0]);
            e.printStackTrace();
        } catch (IOException e) {
            System.out.println("io exception");
            e.printStackTrace();
        }
    }

}

6.StreamTokenizer:

StreamTokenizer随同InputStream工作

用于将任何InputStream分割为一系列“记号”(token),这些记号实际上是一些断断续续的文本块,
中间用我们选择的任何东西分割,例如:我们的记号可以是单词,中间用空白以及标点符号分割

7.StringTokenizer

与StreamTokenizer功能相似

每次返回字串内的一个记号,这些记号是由制表符、空格以及新行分割的连续字符;如果进行简单的
操作可以使用StringTokenier,复杂的需要使用StreamTokenier

8.IO字符流:

字节流只支持8位字节流,不能很好控制16为字符,因此在原有库的基础上扩展了字符流,几乎所有字节流都有对应的字符流,一般情况下,尝试性选择字符流类,若代码不能通过编译,则换回字节流。

下面为字节流和字符流的对应关系:

字节流字符流对应

通过filter(过滤器)修改数据流的行为,下表是字节流过滤器和字符流过滤器各类的对应关系:

过滤器字节流字符流

使用字符流实现:

package com.zd.java.io.newio;
import java.io.*;
import java.nio.Buffer;
/**
 * 使用字符流内实现
 * Created by ZD on 2017/10/12.
 */
public class NewIODemo {

    public static void main(String[] args){

        try {
            //1.reading input by lines
            BufferedReader in = new BufferedReader(new FileReader("test.out"));
            String s,s2 = new String();
            while ((s = in.readLine()) != null){
                s2 += s + "\n";
            }
            in.close();

            //2.input from memory
            StringReader in2 = new StringReader(s2);
            int c;
            while ((c = in2.read()) != -1){
                System.out.println((char)c);
            }

            //3.formatted memory input
            //must use deprecated class
            try{
                DataInputStream in3 = new DataInputStream(new StringBufferInputStream(s2));
                while (true){
                    System.out.println((char)in3.readByte());
                }
            }catch (EOFException e){
                System.out.println("end of stream0");
            }

            //4.line numbering & file output
            try {
                LineNumberReader li = new LineNumberReader(new StringReader(s2));
                BufferedReader in4 = new BufferedReader(li);
                PrintWriter out1 = new PrintWriter(new FileWriter("IODemo.out"));
                while ((s = in4.readLine()) != null){
                    out1.print("Line "+li.getLineNumber() + s);
                }
                out1.close();
            }catch (EOFException e){
                System.out.println("end of stream");
            }

            //5.storing & recovering data
            try {
                DataOutputStream out2 = new DataOutputStream(new BufferedOutputStream(new FileOutputStream("Data.txt")));
                out2.writeDouble(3.14159);
                out2.writeBytes("that was pi");
                out2.close();

                DataInputStream in5 = new DataInputStream(new BufferedInputStream(new FileInputStream("Data.txt")));
                BufferedReader in5br = new BufferedReader(new InputStreamReader(in5));
                System.out.println(in5.readDouble());
                System.out.println(in5br.readLine());
            }catch (EOFException e){
                System.out.println("end of stream");
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

9.重定向:

重定向标准输入、输出以及错误IO流·,如果在屏幕上生成大量输出,不方便
package com.zd.java.io.newio.redirect;

import java.io.*;
/**
 *重定向输出
 * Created by ZD on 2017/10/12.
 */
public class Redirecting {

    public static void main(String[] args){
        try {
            BufferedInputStream in = new BufferedInputStream(new FileInputStream("Data.txt"));
            PrintStream out = new PrintStream(new BufferedOutputStream(new FileOutputStream("test1.out")));
            System.setIn(in);
            System.setOut(out);//重定向输出文件
            System.setErr(out);

            BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
            String s;
            while ((s = br.readLine())!=null){
                System.out.println(s);
                out.close();
            }
        } catch (FileNotFoundException e) {
            System.out.println("未找到文件");
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

10.压缩:

下表显示的是压缩类功能:

压缩类

10.1使用GZIP进行简单压缩

GZIP接口适用于的那个数据流压缩

package com.zd.java.io.newio.compress;

import java.io.*;
import java.util.zip.GZIPOutputStream;
/**
 * 用GZIP进行压缩
 * 只有单个数据流需要压缩,此为最适当的选择
 * Created by ZD on 2017/10/12.
 */
public class GZIPCompress {
    public static void main(String[] args){
        try {
          BufferedReader in = new BufferedReader(new FileReader("pom.xml"));
            BufferedOutputStream out = new BufferedOutputStream(new GZIPOutputStream(new FileOutputStream("test.gz")));
            System.out.println("writing file");
            int c;
            while ((c = in.read())!=-1){
                out.write(c);
            }
            in.close();
            out.close();

            System.out.println("reading file");
            BufferedReader in2 = new BufferedReader(new InputStreamReader(new FileInputStream("test.gz")));
            String s;
            while ((s = in2.readLine()) != null){
                System.out.println(s);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

10.2使用ZIP进行多文件保存:

package com.zd.java.io.newio.compress;

import java.io.*;
import java.util.zip.GZIPOutputStream;

/**
 * 用GZIP进行压缩
 * 只有单个数据流需要压缩,此为最适当的选择
 * Created by ZD on 2017/10/12.
 */
public class GZIPCompress {
    public static void main(String[] args){
        try {
          BufferedReader in = new BufferedReader(new FileReader("pom.xml"));
            BufferedOutputStream out = new BufferedOutputStream(new GZIPOutputStream(new FileOutputStream("test.gz")));
            System.out.println("writing file");
            int c;
            while ((c = in.read())!=-1){
                out.write(c);
            }
            in.close();
            out.close();

            System.out.println("reading file");
            BufferedReader in2 = new BufferedReader(new InputStreamReader(new FileInputStream("test.gz")));
            String s;
            while ((s = in2.readLine()) != null){
                System.out.println(s);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

10.3jar保存

 jar文件是跨平台的,在jar文件之前,web浏览器必须重复多次请求web服务器,以便下载完构成一个程序片的所有文件,除此以外每个文件都是为经压缩的,将所有这些文件合并到一个jar文件里以后,只需向远程服务器发出一次请求即可,由于采用压缩技术,可以在更短时间里获得全部数据,ar文件是由一系列采用zip压缩格式的文件构成,同时还有一个详情单                
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值