IO基础之转换流、内存流和合并流

来源:互联网 发布:网络专供是真的假的 编辑:程序博客网 时间:2024/06/06 04:07
转换流:把字节流转换成字符流
InputStreamReader:把字节输入流转成字符输入流。
OutputStreamWriter:把字节输出流转成字符输出流。


为什么有字节流转字符流,没有字符流转字节流:
字节流可以操作一切文件(纯文本文件/二进制文件)
字符流用来操作中文纯文本文件使用的,本身是对字节流的增强。


下面实现一下转换:

/** * Created by Layne_Yao on 2017-7-28 下午2:59:07. * CSDN:http://blog.csdn.net/Jsagacity */public class StreamTransformReaderDemo {public static void main(String[] args) throws Exception, FileNotFoundException {File srcFile = new File("file/stream.txt");File destFile = new File("target/stream.txt");//把字节流转换成字符流Reader in = new InputStreamReader(new FileInputStream(srcFile),"GBK");Writer out = new OutputStreamWriter(new FileOutputStream(destFile),"GBK");//接下来操作字符流char[] buffer = new char[1024];int len = -1;while((len = in.read(buffer))!=-1){out.write(buffer,0,len);}in.close();out.close();}}


内存流(数组流):
把数据先临时存在数组中,待会再从数组中获取出来:
1、字节内存流:ByteArrayOutputStream/ByteArrayInputStream
2、字符内存流:CharArrayWriter/CharArrayReader
3、字符串流:StringWriter/StringReader

字节内存流:

/** * Created by Layne_Yao on 2017-7-28 下午3:22:45. * CSDN:http://blog.csdn.net/Jsagacity *///字节的数组流/内存流public class ByteArrayDemo {public static void main(String[] args) throws Exception {//字节数组输出流:程序->内存ByteArrayOutputStream bos = new ByteArrayOutputStream();bos.write("ABCDE".getBytes());//要使用存储的临时数据byte[] buffer = bos.toByteArray();//字节数组输入流:内存-->程序ByteArrayInputStream bis = new ByteArrayInputStream(buffer);byte[] bys = new byte[5];int len = -1;while ((len = bis.read(bys)) != -1) {System.out.println(new String(bys, 0, len));}//其实无需关闭流bis.close();bos.close();}}


字符内存流:

/** * Created by Layne_Yao on 2017-7-28 下午3:35:21. * CSDN:http://blog.csdn.net/Jsagacity */// 字符内存流/数组流public class CharArrayDemo {public static void main(String[] args) throws Exception {// 字符数组输出流CharArrayWriter writer = new CharArrayWriter();writer.write("这是字符内存流!!!");char[] data = writer.toCharArray();// 字符数组输入流CharArrayReader reader = new CharArrayReader(data);char[] bys = new char[10];int len = -1;while ((len = reader.read(bys)) != -1) {System.out.println(new String(bys, 0, len));}//其实无需关闭流writer.close();reader.close();}}


字符串流:

/** * Created by Layne_Yao on 2017-7-28 下午3:50:24. * CSDN:http://blog.csdn.net/Jsagacity *///字符串流(字符串的内存流)public class StringWriterReaderDemo {public static void main(String[] args) throws Exception {//字符串的输出流StringWriter sWriter = new StringWriter();sWriter.write("字符串流(字符串的内存流)");//字符串的输入流StringReader sReader = new StringReader(sWriter.toString());char[] buffer = new char[1024];int len = -1;while((len = sReader.read(buffer))!=-1){System.out.println(new String(buffer,0,len));}//其实无需关闭流sWriter.close();sReader.close();}}


合并流:

主要的功能是将两个文件的内容合并成一个文件;

下面将两个文件合并在一起:

/** * Created by Layne_Yao on 2017-7-28 下午3:40:21. * CSDN:http://blog.csdn.net/Jsagacity *///合并流public class SequenceDemo {public static void main(String args[]) throws Exception {//两个文件的输入流InputStream is1 = new FileInputStream("file/a.txt");InputStream is2 = new FileInputStream("file/b.txt");//文件的输出流OutputStream os = new FileOutputStream("target/ab.txt");//将两个输入流合并SequenceInputStream sis = new SequenceInputStream(is1, is2); int len = -1;while ((len = sis.read()) != -1) {os.write(len);}sis.close();is1.close();is2.close();os.close();}}

原创粉丝点击