IO流(随机流,数组内存流

File file1=new File("test1.txt");
RandomAccessFile in2=new RandomAccessFile(file1,"rw");

后面参数是"rw"他可读可写,用seek()来设定位置

不过在用readLine()时,非ASCII码(比如中文)会出现乱码现象

不过有办法

用"iso-8859-1"重新编码,然后重新化为String 

String s1=in2.readLine();
byte b1[]=s1.getBytes("iso-8859-1");
String s3=new String(b1,"GB2312");//GB2312要看机器的默认编码

 


数组内存流

这类流的目的地和源都是内存

分别有字节数组和字符数组

            File file1=new File("test1.txt");
            ByteArrayOutputStream out1=new ByteArrayOutputStream();//new字节输出流
            byte[] b1="hello world".getBytes();//字节数组
            out1.write(b1);//输出到内存
            ByteArrayInputStream in1=new ByteArrayInputStream(out1.toByteArray());//public byte[] toByteArray()可以返回输出流写入到内存缓冲区的全部字节
            byte[] b2=new byte[out1.toByteArray().length];//new字节输入流
            in1.read(b2);//读入
            System.out.println(new String(b2));
            CharArrayWriter out2=new CharArrayWriter();//new字符输出流
            char[] b3="你好啊".toCharArray();//化为字符数组
            out2.write(b3);//写入内存
            CharArrayReader in2=new CharArrayReader(out2.toCharArray());//new字符输入流
            char[] b4=new char[out2.toCharArray().length];
            System.out.println(b3);

 

posted on 2016-11-02 11:55  Kooing  阅读(279)  评论(0编辑  收藏  举报

导航