Java SE 高级开发之Java IO 之 内存操作流

内存流概念
在之前的文章中的操作都是针对于文件进行的IO处理。除了文件之外,IO的操作也可以发生在内存之中,这种流称之为内存操作流。文件流的操作里面一定会产生一个文件数据(不管最后这个文件数据是否被保留)。
如果现在需求是:需要进行IO处理,但是又不希望产生文件。这种情况下就可以使用内存作为操作终端

对于内存流也分为两类:

 字节内存流:ByteArrayInputStream、ByteArrayOutputStream
 字符内存流:CharArrayReader、CharArrayWriter

首先来观察ByteArrayInputStream和ByteArrayOutputStream的构造方法:

public ByteArrayInputStream(byte buf[])
public ByteArrayOutputStream()

接下来看内存流的继承关系:
输出流
Java SE 高级开发之Java IO 之 内存操作流_第1张图片
输入流
Java SE 高级开发之Java IO 之 内存操作流_第2张图片

例:通过内存流实现大小写转换

public class Test {
    public static void main(String[] args) {
        String msg = "hello world";
        //实例化InputStream类对象,实例化的时候需要将你操作的数据保存到内存之中
        //最终读取的就是你设置的内容

        //用的是字节内存流
        InputStream inputStream = new ByteArrayInputStream(msg.getBytes());
        OutputStream outputStream = new ByteArrayOutputStream();

        int temp = 0;
        try {
            while((temp = inputStream.read()) != -1) {
                //每个字节进行处理,处理之后所有数据都在outputStream类中
                //Character.toUpperCase(temp)   将读到的数据转换为大写
                outputStream.write(Character.toUpperCase(temp));
            }
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        //直接输出outputStream对象
        System.out.println(outputStream);

        //关闭内存字节输入输出流
        try {
            inputStream.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        try {
            outputStream.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

Java SE 高级开发之Java IO 之 内存操作流_第3张图片

这个时候发生了IO操作,但是没有文件产生,可以理解为一个临时文件处理。

内存流操作
内存操作流还有一个很小的功能,可以实现两个文件的合并处理(文件量不大)。
内存操作流最为核心的部分就是:将所有OutputStream输出的程序保存在了程序里面,所以可以通过这一特征实现处理。

需求:现在有两个文件:data-a.txt、data-b.txt。现在要求将这两个文件的内容做一个合并处理。
例:内存流实现文件合并处理

public class Test {
    public static void main(String[] args) {
        File[] files = new File[] {new File("C:"+File.separator+"Users"+File.separator+"贵军"+
                File.separator+"Desktop"+ File.separator+"data-a.txt"),
                new File("C:"+File.separator+"Users"+File.separator+"贵军"+
                        File.separator+"Desktop"+ File.separator+"data-b.txt")};
        String[] data = new String[2];
        for(int i = 0; i < files.length; i++) {
            data[i] = readFile(files[i]);

        }
        StringBuffer buffer = new StringBuffer();       //组合操作
        String[] contentA = data[0].split(" ");
        String[] contentB = data[1].split(" ");
        for(int i = 0; i < contentA.length; i++) {
            buffer.append(contentA[i]).append("(").append(contentB[i]).append(")").append(" ");
        }
        System.out.println(buffer);
    }

    //读取文件内容,使用File对象,因为其包含有完整的路径信息
    public static String readFile(File file){
        if(file.exists()) {
            InputStream inputStream = null;
            try {
                inputStream = new FileInputStream(file);
            } catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            //没有向上转型, 因为稍后要使用到toByteArray()方法
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            int temp = 0;
            byte[] data = new byte[10];
            try {
                while((temp = inputStream.read(data)) != -1) {
                    //将数据保存在byteArrayOutputSteram中
                    byteArrayOutputStream.write(data, 0, temp);
                }
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            try {
                byteArrayOutputStream.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            try {
                inputStream.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            //返回读取的内容
            return new String(byteArrayOutputStream.toByteArray());
        }
        return null;
    }
}

如果只是使用InputStream类,在进行数据完整读取的时候会很不方便,结合内存流的使用会好很多。

你可能感兴趣的:(Java,Java基础知识的细节分析)