A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

本帖最后由 Himoriarty 于 2015-6-10 13:09 编辑

     ------- <a  target="blank">android培训</a>、<a  target="blank">java培训</a>、期待与您交流! ----------
       1、字节流读取的时候,读到一个字节就返回一个字节。字符流使用了字节流读到一个或多个字节(中文对应的字节为两个,UTF-8码表中是三个)时,先去查指定的编码表,将查到的字符返回。
       2、字节流可以处理所有类型数据,如图片、MP3等。字符流只能处理字符数据。
       3、字节流输入流都是以InputStream结尾,字节流输出流都是以OutputStream结尾,在InputStream或者OutputStream前面代表这个流的作用。字符流输入流都是以Writer结尾,字符流输出流都是以Reader结尾,相同与字节流前面也是代表这个流的作用。
        如果处理的是纯文本数据,优先使用字符流,除此之外都考虑使用字节流。

  1. <div class="blockcode"><blockquote>//字符流练习

  2. import java.io.*;
  3. class MyBufferedReaderDemo
  4. {
  5.         public static void main(String[] args) throws IOException
  6.         {
  7.                 MyBufferedReader mybf = new MyBufferedReader(new FileReader("CopyTest2.java"));
  8.                 String line = null;
  9.                 while((line = mybf.MyReadLine()) != null)
  10.                 {
  11.                         System.out.println(line);
  12.                 }
  13.                 mybf.MyClose();
  14.         }
  15. }

  16. class MyBufferedReader
  17. {
  18.         private FileReader r;
  19.         MyBufferedReader(FileReader r)
  20.         {
  21.                 this.r = r;
  22.         }
  23.         public String MyReadLine() throws IOException
  24.         {
  25.                 StringBuilder sb = new StringBuilder();
  26.                
  27.                 int ch = 0;
  28.                 while((ch = r.read()) != -1)
  29.                 {
  30.                         if(ch == '\r')
  31.                                 continue;
  32.                         if(ch == '\n')
  33.                                 return sb.toString();
  34.                         else
  35.                                 sb.append((char)ch);
  36.                 }
  37.                 if(sb.length() != 0)
  38.                         sb.toString();
  39.                 return null;
  40.         }
  41.        
  42.         public void MyClose() throws IOException
  43.         {
  44.                 r.close();
  45.         }
  46. }
复制代码
  1. //字节流练习

  2. import java.io.*;

  3. class MyBufferedInputStream
  4. {
  5.         private InputStream in;
  6.        
  7.         private byte[] buf = new byte[1024];
  8.        
  9.         private int pos = 0,count = 0;
  10.        
  11.         MyBufferedInputStream(InputStream in)
  12.         {
  13.                 this.in = in;
  14.         }
  15.        
  16.         public int myRead()  throws IOException
  17.         {
  18.                 //通过in对象对去硬盘数据
  19.                 if(count == 0)
  20.                 {
  21.                         count = in.read(buf);
  22.                
  23.                         if(count < 0)
  24.                                 return -1;
  25.                         pos = 0;
  26.                        
  27.                         byte b = buf[pos];
  28.                        
  29.                         count--;
  30.                         pos++;
  31.                         return b&255;
  32.                 }
  33.                 else if(count > 0)
  34.                 {
  35.                         byte b = buf[pos];
  36.                        
  37.                         count--;
  38.                         pos++;
  39.                         return b&255;
  40.                 }
  41.                 return -1;
  42.                
  43.         }
  44.         public void myClose() throws IOException
  45.         {
  46.                 in.close();
  47.         }
  48. }

  49. class MyInputStreamDemo
  50. {
  51.         public static void main(String[] args)  throws IOException
  52.         {
  53.                 long start = System.currentTimeMillis();
  54.                 copy_1();
  55.                 long end = System.currentTimeMillis();
  56.         }
  57.         public static void copy_1() throws IOException
  58.         {
  59.                 MyBufferedInputStream bufis = new MyBufferedInputStream(new FileInputStream("YPOK 01.mp3"));
  60.                 BufferedOutputStream bufos = new BufferedOutputStream(new FileOutputStream("YPOK 02.mp3"));
  61.                
  62.                 int by = 0;
  63.                
  64.                 while((by = bufis.myRead()) != -1)
  65.                 {
  66.                         bufos.write(by);
  67.                 }
  68.                
  69.                 bufos.close();
  70.                 bufis.myClose();
  71.         }
  72. }

复制代码




----------android培训java培训、java学习型技术博客、期待与您交流!------------

0 个回复

您需要登录后才可以回帖 登录 | 加入黑马