PushbackInputStream 读取GBK字符时,会将双引号变成单引号

如: xml version="1.0" encoding="GBK"
PushbackInputStream 读取后,会变成:  xml version='1.0' encoding='GBK'
而UTF-8不会!
java 代码
  1. public static void  testStream() throws IOException{   
  2.     int MAX=2048;   
  3.     URL url=new URL("http://www.open-open.com/indexrss.xml");   
  4. /       URL url=new URL("http://www.blogjava.net/kukoo/Rss.aspx");   
  5.     InputStream input=url.openStream();   
  6.     byte bytes[]=new byte[MAX];   
  7.     PushbackInputStream bfInput=new PushbackInputStream(input);   
  8.     OutputStream out=new FileOutputStream(new File("e:/temp/rss.xml"));   
  9.     BufferedOutputStream bfOut=new BufferedOutputStream(out);   
  10.     int read=0;   
  11.     int totalBytes=0;          
  12.     int offset = 0;   
  13.     int max = MAX;   
  14.     while((read=bfInput.read(bytes,offset,max))!=-1&&offset
  15.         totalBytes+=read;   
  16.         bfOut.write(bytes, offset, max);   
  17.         offset+=read;              
  18.         max-=read;             
  19.         System.out.println("read:"+read+"total:"+totalBytes);   
  20.     }   
  21.        
  22.     System.out.println(read+":"+totalBytes);   
  23.     bfOut.flush();   
  24.     bfOut.close();   
  25.     bfInput.close();   
  26.     Reader reader=new InputStreamReader(new ByteArrayInputStream(bytes,0,offset));   
  27.     BufferedReader bufferReader=new BufferedReader(reader);   
  28.     String longer=bufferReader.readLine();   
  29.     System.out.println(""+longer);   
  30. }  
`PushbackInputStream` 是Java IO库中的一个类,它提供了一种在读取输入流将数据“推回”流中的方法,从而可以重新读取该数据。具体来说,`PushbackInputStream` 允许您在读取输入流将最近读取的字节推回到流中,以便稍后再次读取它们。 例如,如果您正在读取一个文本文件,并且意外地读取了一些不是文本的字节,您可以使用 `PushbackInputStream` 将这些字节推回到流中,然后重新读取它们。 以下是一个示例代码片段,演示如何使用 `PushbackInputStream`: ```java import java.io.*; public class PushbackInputStreamExample { public static void main(String[] args) throws IOException { String str = "Hello, world!"; ByteArrayInputStream bais = new ByteArrayInputStream(str.getBytes()); PushbackInputStream pbis = new PushbackInputStream(bais); int b = pbis.read(); System.out.println((char) b); // Output: H pbis.unread(b); // push back the first byte read b = pbis.read(); // read the first byte again System.out.println((char) b); // Output: H byte[] buf = new byte[5]; pbis.read(buf); System.out.println(new String(buf)); // Output: "ello," pbis.unread(buf); // push back the five bytes read byte[] buf2 = new byte[5]; pbis.read(buf2); System.out.println(new String(buf2)); // Output: "ello," } } ``` 在这个示例中,我们首先将字符串 "Hello, world!" 转换为一个字节数组,然后使用 `ByteArrayInputStream` 将其包装为一个输入流。接下来,我们使用 `PushbackInputStream` 包装这个输入流。 我们首先读取第一个字节并将其输出。然后,我们使用 `unread` 方法将该字节推回流中,并再次读取它。然后,我们读取五个字节,并使用 `unread` 方法将它们推回流中。最后,我们再次读取五个字节,这次从流中读取了之前推回的字节。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值