字节流InputStream

/**
 * java I/O 字节流转换
 *
 * @author bpe
 *
 */
public class InputStreamTest {
      public static void main(String[] args ) {
           //合并流 SequenceInputStream
           // 把文件中的内容读取到程序中(使用到了字节流InputStream中所有的方法)
           try {
               // 建立连接 注:java.io.IOException: mark/reset not supported
               // 因为FileInputStream并不能使用唤醒流 也就是reset方法
               // InputStream stream = new FileInputStream("D:\\ bpe \\input.txt");
              BufferedInputStream stream = new BufferedInputStream( new FileInputStream( "D:\\\\bpe\\\\input.txt" ));
               // stream.available() 返回可以从流中读取的字节数
               byte [] data = new byte [ stream .available()];
               // 不读取前5个字符
               // stream.skip(5);
               // 给流设置断点 让流暂停读取
               stream .mark( stream .read( data , 0, data . length / 2));
              System. out .println( "暂停前读取的数据======>>" + new String( data ));
              System. out .println( "让流暂停读取----------------" );
              Thread. sleep (2000);
              System. out .println( "让流开始读取---------------" );
               stream .reset();
               // 读取全部文件 read(byte[] b)
               stream .read( data );
               // read()返回-1表示这个流已经到了末尾
               int i = 0;
               // 定义一个记录索引的变量
               int index = 0;
               // 循环读取每个数据
               while (( i = stream .read()) != -1) { // 把读取的数据放到i中
                    data [ index ] = ( byte ) i ;
                    index ++;
              }
               // 把字节数组转成字符串
              System. out .println( "读取的文件数据=========================>>" + new String( data ));
               // 输出数据==>>inputStream ��FileInputStream.
               // 使用了stream.skip(5) 输出数据==>>Stream ��FileInputStream.
               // 关闭流
               stream .close();
          } catch (Exception e ) {
               // TODO Auto-generated catch block
               e .printStackTrace();
          }
     }
}

猜你喜欢

转载自blog.csdn.net/qq_36934544/article/details/80941800