将一个对象转成Reader或InputStream

这个例子是将一个list对象转成一个reader对象,也可以看成是将一个Writer对象转成Reader对象的例子。
       
        //CSVReader和CSVWriter是oencsv-1.8.jar中的类
        FileReader fr = new FileReader("download.csv");
        CSVReader cr = new CSVReader(fr);
        List list = cr.readAll();

        //---start---这几行代码的顺序不能变-----------------------
        //---确保在向PipedWriter写入数据前创建PipedReader
        PipedWriter pw = new PipedWriter();
        PipedReader pr = new PipedReader(pw);
        BufferedReader br = new BufferedReader(pr);
        CSVWriter cw = new CSVWriter(pw);
        cw.writeAll(list);
        cw.close();
        //----end----br就是转换后的Reader对象-----------

        //--------test---------
        String readLine = br.readLine();
        while (readLine!=null&&!readLine.equals("")) {
            System.out.println(readLine);
            readLine = br.readLine();
        }
        System.out.println("over");


如果想转成InputStream那需要把PipedWriter和PipedReader替换成PipedOutputStream和PipedInputStream。
这是通常的做法,但很可惜opencsv中没有CSVInputStream和CSVOutputStream,所以不能简单的替换,而是将Reader转换成InputStream。
        //------ReaderInputStream是ibatis.jar中提供。
        FileReader fr = new FileReader("download.csv");
        CSVReader cr = new CSVReader(fr);
        List list = cr.readAll();
        PipedWriter pw = new PipedWriter();
        PipedReader pr = new PipedReader(pw);
        //------将Reader转换成InputStream----------
        ReaderInputStream ris = new ReaderInputStream(pr);
        CSVWriter cw = new CSVWriter(pw);
        cw.writeAll(list);
        cw.close();
        //------ris是转换后的InputStream对象

ReaderInputStream的代码,可以直接使用。
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStreamWriter;
import java.io.Reader;
import java.io.UnsupportedEncodingException;
import java.io.Writer;

/**
 *@author luyang
 *@create_time 2009/03/18
 */
public class ReaderInputStream  extends InputStream {
    protected Reader reader;
    protected ByteArrayOutputStream byteArrayOut;
    protected Writer writer;
    protected char[] chars;
    protected byte[] buffer;
    protected int index, length;

    /**
     * Constructor to supply a Reader
     *
     * @param reader - the Reader used by the InputStream
     */
    public ReaderInputStream(Reader reader) {
      this.reader = reader;
      byteArrayOut = new ByteArrayOutputStream();
      writer = new OutputStreamWriter(byteArrayOut);
      chars = new char[1024];
    }

    /**
     * Constructor to supply a Reader and an encoding
     *
     * @param reader   - the Reader used by the InputStream
     * @param encoding - the encoding to use for the InputStream
     * @throws UnsupportedEncodingException if the encoding is not supported
     */
    public ReaderInputStream(Reader reader, String encoding) throws UnsupportedEncodingException {
      this.reader = reader;
      byteArrayOut = new ByteArrayOutputStream();
      writer = new OutputStreamWriter(byteArrayOut, encoding);
      chars = new char[1024];
    }

    /**
     * @see java.io.InputStream#read()
     */
    public int read() throws IOException {
      if (index >= length)
        fillBuffer();
      if (index >= length)
        return -1;
      return 0xff & buffer[index++];
    }

    protected void fillBuffer() throws IOException {
      if (length < 0)
        return;
      int numChars = reader.read(chars);
      if (numChars < 0) {
        length = -1;
      } else {
        byteArrayOut.reset();
        writer.write(chars, 0, numChars);
        writer.flush();
        buffer = byteArrayOut.toByteArray();
        length = buffer.length;
        index = 0;
      }
    }

    /**
     * @see java.io.InputStream#read(byte[], int, int)
     */
    public int read(byte[] data, int off, int len) throws IOException {
      if (index >= length)
        fillBuffer();
      if (index >= length)
        return -1;
      int amount = Math.min(len, length - index);
      System.arraycopy(buffer, index, data, off, amount);
      index += amount;
      return amount;
    }

    /**
     * @see java.io.InputStream#available()
     */
    public int available() throws IOException {
      return (index < length) ? length - index :
          ((length >= 0) && reader.ready()) ? 1 : 0;
    }

    /**
     * @see java.io.InputStream#close()
     */
    public void close() throws IOException {
      reader.close();
    }
  }

你可能感兴趣的:(java,ibatis)