如: xml version="1.0" encoding="GBK"
PushbackInputStream 读取后,会变成: xml version='1.0' encoding='GBK'
而UTF-8不会!
java 代码
public static void testStream() throws IOException{ int MAX=2048 ; URL url=new URL("http://www.open-open.com/indexrss.xml" ); / URL url=new URL("http://www.blogjava.net/kukoo/Rss.aspx" ); InputStream input=url.openStream(); byte bytes[]=new byte [MAX]; PushbackInputStream bfInput=new PushbackInputStream(input); OutputStream out=new FileOutputStream(new File("e:/temp/rss.xml" )); BufferedOutputStream bfOut=new BufferedOutputStream(out); int read=0 ; int totalBytes=0 ; int offset = 0 ; int max = MAX; while ((read=bfInput.read(bytes,offset,max))!=-1 &&offset totalBytes+=read; bfOut.write(bytes, offset, max); offset+=read; max-=read; System.out.println("read:" +read+"total:" +totalBytes); } System.out.println(read+":" +totalBytes); bfOut.flush(); bfOut.close(); bfInput.close(); Reader reader=new InputStreamReader(new ByteArrayInputStream(bytes,0 ,offset)); BufferedReader bufferReader=new BufferedReader(reader); String longer=bufferReader.readLine(); System.out.println("" +longer); }