欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Java InputStream的多種使用詳解

 更新時間:2019年12月03日 10:33:21   作者:這個很科學(xué)  
這篇文章主要介紹了Java InputStream的多種使用詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

以前寫東西,尤其是網(wǎng)絡(luò)傳輸方面總會碰到將某種格式的文本或者圖片等轉(zhuǎn)幻成數(shù)據(jù)流的方式來傳輸,那時候用的就直接網(wǎng)上找點就粘貼,也沒什么搞懂到底是怎么個機(jī)理。后來抽點空就死啃了點這方面的文章,稍微懂了點,特意分享一下。
InputStream  FileInputStream BufferInputStream  InputStreamreader ByteArrayInputStream這些東西到底什么關(guān)系呢?

一、首先我先理解InputStream是老大,剩下的這些都是為其服務(wù)的,先建立一個標(biāo)準(zhǔn),而FileInputStream是其子類。他可以對文件進(jìn)行數(shù)據(jù)流的轉(zhuǎn)換。

String fileName = "E:\\電影\\[高清電影]";

InputStream inputstream = new FileInputStream("fileName");//然后對InputStream 進(jìn)行讀操作,為啥是讀呢?可以把內(nèi)存當(dāng)作主體,這是某個網(wǎng)友說的,你從硬盤往內(nèi)存里Input 東西就是讀取數(shù)據(jù)咯。 另外這里因為FileInputStream繼承InputStream 類//所以可以這樣用

   byte[] by = new byte[8192];//此數(shù)字不唯一哦;

   int len ;

   while(  (len=inputStream.read(by))!=-1 ){ //len就是得出的字節(jié)流了。
         }
   inputStream.close();//最后別忘記關(guān)閉,當(dāng)然應(yīng)該還有個if判斷是否為空和try catch 的語句。

  File f = new File("F:\\……"); 

  if (!f.exists()) { 
  System.out.println("creat " + f.toString()); 

   f.createNewFile(); 
   }

  FileOutputStream fos = new FileOutputStream(f);//InputStream和OutputStream 一般辯證的來看,一個讀,一個寫,兩者差不多的用法。
 fos.write(b, 0, len); 
 fos.flush();
 fos.close(); 

二、在接下來說BufferInputStream,他是數(shù)據(jù)流緩存的東西,不帶緩沖的操作,每讀一個字節(jié)就要寫入一個字節(jié),由于涉及磁盤的IO操作相比內(nèi)存的操作要慢很多,所以不帶緩沖的流效率很低。帶緩沖的流,可以一次讀很多字節(jié),但不向磁盤中寫入,只是先放到內(nèi)存里。等湊夠了緩沖區(qū)大小的時候一次性寫入磁盤,這種方式可以減少磁盤操作次數(shù),速度就會提高很多!這就是兩者的區(qū)別。

public class InputStreamTest {
  private static final String FILENAME="E:\\電影\\[高清電影]阿甘正傳.1994.美國.中文字幕.1280x720.rmvb";
  public static void main(String[] args) throws IOException {
    long l1 = readByBufferedInputStream();
    long l2 = readByInputStream();
    System.out.println("通過BufferedInputStream讀取用時:"+l1+";通過InputStream讀取用時:"+l2);
  }
 
  public static long readByInputStream() throws IOException {
    InputStream in=new FileInputStream(FILENAME);
    byte[] b=new byte[8192];
    int l=0;
    long start=System.currentTimeMillis();
    while(in.read(b,0,8192)!=-1){
    }
    long end=System.currentTimeMillis();
    return end-start;
  }
 
  public static long readByBufferedInputStream() throws IOException {
    BufferedInputStream in=new BufferedInputStream(new FileInputStream(FILENAME));
    byte[] b=new byte[8192];
    int l=0;
    long start=System.currentTimeMillis();
    while(in.read(b,0,8192)!=-1){
    }
    long end=System.currentTimeMillis();
    return end-start;
  }
}

三、InputStreamreader其實就是將字節(jié)流轉(zhuǎn)成字符流。

import java.io.*;
class InputStreamReaderDemo {
 public static void transReadNoBuf() throws IOException {
  /**
   * 沒有緩沖區(qū),只能使用read()方法。
   */
  //讀取字節(jié)流
  //InputStream in = System.in;//讀取鍵盤的輸入。
  InputStream in = new FileInputStream("D:\\demo.txt");//讀取文件的數(shù)據(jù)。
  //將字節(jié)流向字符流的轉(zhuǎn)換。要啟用從字節(jié)到字符的有效轉(zhuǎn)換,
  //可以提前從底層流讀取更多的字節(jié).
  InputStreamReader isr = new InputStreamReader(in);//讀取
  //綜合到一句。
  //InputStreamReader isr = new InputStreamReader(
  //new FileInputStream("D:\\demo.txt"));
   
  char []cha = new char[1024];
  int len = isr.read(cha);
  System.out.println(new String(cha,0,len));
  isr.close();
 
 }
 public static void transReadByBuf() throws IOException {
  /**
   * 使用緩沖區(qū) 可以使用緩沖區(qū)對象的 read() 和 readLine()方法。
   */
  //讀取字節(jié)流
  //InputStream in = System.in;//讀取鍵盤上的數(shù)據(jù)
  InputStream in = new FileInputStream("D:\\demo.txt");//讀取文件上的數(shù)據(jù)。
  //將字節(jié)流向字符流的轉(zhuǎn)換。
  InputStreamReader isr = new InputStreamReader(in);//讀取
  //創(chuàng)建字符流緩沖區(qū)
  BufferedReader bufr = new BufferedReader(isr);//從字符輸入流中讀取文本,緩沖各個字符,從而實現(xiàn)字符、數(shù)組和行的
   //高效讀取。 可以指定緩沖區(qū)的大小,或者可使用默認(rèn)的大小。大多數(shù)情況下,默認(rèn)值足夠大。類似于BufferInputStream
   //只是兩者緩沖的對象不一樣。
 //BufferedReader bufr = new BufferedReader(
  //new InputStreamReader(new FileInputStream("D:\\demo.txt")));可以綜合到一句。
   /*int ch =0;
  ch = bufr.read();
  System.out.println((char)ch);
  */
  String line;
  while((line = bufr.readLine())!=null){
   System.out.println(line);
  }
  isr.close();
 }
}

四、最后一個ByteArrayInputStream 這個其實用的的不多,但是ByteArrayOutputStream還是用的多點,所以拿ByteArrayOutputStream來上代碼。他的作用就是把字節(jié)流轉(zhuǎn)成字符。其實我感覺沒啥用,可能是我知道的不多    

從文件中讀取二進(jìn)制數(shù)據(jù),全部存儲到ByteArrayOutputStream中。

FileInputStream fis=new FileInputStream("test");

BufferedInputStream bis=new BufferedInputStream(fis);

ByteArrayOutputStream baos=new ByteArrayOutputStream();

int c=bis.read();//讀取bis流中的下一個字節(jié)

while(c!=-1){

   baos.write(c);

   c=bis.read();

}

bis.close();

byte retArr[]=baos.toByteArray();

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論