内存操作流

---恢复内容开始---

内存流的基本操作

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

  在Java中有两类数据流:

    字节内存流:ByteArrayInputStream ByteArrayOutputStream

    字符内存流:CharArrayReader CharArrayWriter

  观察ByteArrayInputStream 和ByteArrayOutputStream类中提供的构造方法:

    ByteArrayInputStream类构造:public ByteArrayInputStream(byte[] buf)

    ByteArrayOutputStream类构造:public ByteArrayOutputStream()

范例:通过内存流实现一个大小写转换操作

 1 package cn.Tony.demo;
 2 import java.io.ByteArrayInputStream;
 3 import java.io.ByteArrayOutputStream;
 4 import java.io.InputStream;
 5 import java.io.OutputStream;
 6 public class TestDemo{
 7     public static void main(String[] args) throws Exception {
 8         String msg="hello world";
 9         InputStream input=new ByteArrayInputStream(msg.getBytes());
10         OutputStream output=new ByteArrayOutputStream();
11         int temp=0;
12         while((temp=input.read())!=-1) {
13             output.write(Character.toUpperCase(temp));
14         }
15         System.out.println(output);
16         output.close();
17         input.close();
18     }    
19 }

  这个发现IO操作,没有文件产生

  实现合并文件处理(文件量不大),因为内存操作流里面核心就是将

OutputStream输出的数据保存在了程序里面,所有可以通过这特征,进行处理

  现在假设有两个文件:data-a.txt data-b.txt

范例:实现文件处理

 1 package cn.Tony.demo;
 2 
 3 import java.io.ByteArrayOutputStream;
 4 import java.io.File;
 5 import java.io.FileInputStream;
 6 import java.io.FileOutputStream;
 7 import java.io.InputStream;
 8 import java.io.OutputStream;
 9 
10 
11 
12 public class TestDemo{
13     public static void main(String[] args) throws Exception {
14         File file[]=new File[] {
15             new File("D:"+File.separator+"IO"+File.separator+"data-a.txt"),
16             new File("D:"+File.separator+"IO"+File.separator+"data-b.txt")
17         };
18     
19         String data[]=new String[2]; //定义一个字符串的对象数组
20         for(int x=0;x<file.length;x++) {
21             data[x]=readFile(file[x]);
22         }
23         StringBuffer buf=new StringBuffer();//组合操作
24         String contentA[]=data[0].split(" ");
25         String contentB[]=data[1].split(" ");
26         OutputStream output=new FileOutputStream(new File("D:"+File.separator+"IO"+File.separator+"data.txt"));
27         for(int x=0;x<contentA.length;x++) {
28             String str=contentA[x]+"("+contentB[x]+")";
29             buf.append(contentA[x]).append("(").append(contentB[x]).append(")").append("");
30             output.write(str.getBytes());
31         }
32         output.close();
33         System.out.println(buf);
34     }    
35     public static String readFile(File file) throws Exception {
36         if(file.exists()) {
37             InputStream input=new FileInputStream(file);
38             ByteArrayOutputStream bos=new ByteArrayOutputStream();
39         
40             byte data[]=new byte[10];
41             int temp=0;
42             while((temp=input.read(data))!=-1) {//内容都在内存流
43                 bos.write(data, 0, temp);
44             }
45             bos.close();
46             input.close();
47             return new String(bos.toByteArray());//将所有的
48         }
49         return null;
50     }
51 }

  如果只使用InputStream类。 结合内存流操作会好很多,

总结:

  这里的开发,在现在已经老了。随之有新的功能,可以代替!

 

---恢复内容结束---

原文地址:https://www.cnblogs.com/Tony98/p/10536494.html