IO--ReaderAndWriter
来源:互联网 发布:预测性数据分析 编辑:程序博客网 时间:2024/06/05 20:43
Reader and Writer:
Reader按照自定义的编码从输入流中进行读取
Writer按照自定义的编码写入输出流
InputStreamReader:
在初始化的时候传入自定义的编码以及输入流,并生成解码器进行读操作。
public InputStreamReader(InputStream in, String charsetName) throws UnsupportedEncodingException { super(in); if (charsetName == null) throw new NullPointerException("charsetName"); sd = StreamDecoder.forInputStreamReader(in, this, charsetName); }
在read的时候调用解码器的read方法
public int read() throws IOException { return sd.read(); }观察一下可以发现InputStreamReader使用了适配器的设计模式,在InputStream的read方法外加了一层Reader的read方法的壳,同时增加了按照固定编码进行解码的功能。
OutputStreamWriter:
在初始化的时候传入自定义的编码以及输出流,并生成编码器进行写操作。
public OutputStreamWriter(OutputStream out, String charsetName) throws UnsupportedEncodingException { super(out); if (charsetName == null) throw new NullPointerException("charsetName"); se = StreamEncoder.forOutputStreamWriter(out, this, charsetName); }
在write的时候调用编码器的write方法
public void write(int c) throws IOException { se.write(c); }
Reader和Writer的不同:
Reader的read方法直接返回读取的值
public int read(char cbuf[]) throws IOException { return read(cbuf, 0, cbuf.length); }Writer的write方法将数据写入Writer中的writerBuffer中,如果需要往输出流里写的话需要调用flash或者close方法
public void write(String str, int off, int len) throws IOException { synchronized (lock) { char cbuf[]; if (len <= WRITE_BUFFER_SIZE) { if (writeBuffer == null) { writeBuffer = new char[WRITE_BUFFER_SIZE]; } cbuf = writeBuffer; } else { // Don't permanently allocate very large buffers. cbuf = new char[len]; } str.getChars(off, (off + len), cbuf, 0); write(cbuf, 0, len); } }装饰类:
PrintWriter,BufferedWriter,BufferedReader
0 0
- IO--ReaderAndWriter
- IO
- IO
- io
- IO
- io
- IO
- io
- IO
- IO
- IO
- io
- IO
- IO
- IO
- IO
- IO
- IO
- 风云变幻,且看HiWork如何帮助团队更好地解决异地沟通问题!
- 查询app包的uuid
- 查看ubuntu12.04.5系统的信息
- jdk环境变量配置
- GRE写作必备句型
- IO--ReaderAndWriter
- Java - Static keyword in Java
- Action中通过Ajax返回特定值的方法(而不是整个页面)
- iOS PAD编写 POPOver
- Ubuntu 14.10+NDK+ffmpeg编译+Android
- 可变参数的宏定义
- WebView 设置软键盘弹出将屏幕上移
- Android 开发环境下载地址 -- 百度网盘 adt-bundle android-studio sdk adt 下载
- winform中窗体的快捷鍵设置