国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發(fā)設計 > 正文

IO流的【字節(jié)流】

2019-11-10 18:34:19
字體:
來源:轉載
供稿:網(wǎng)友

IO流的【字節(jié)流】

一、InputStream中的讀取數(shù)據(jù)的方法如下:

  1 、int read()

  功能:讀取一個字節(jié)的數(shù)據(jù),并且返回讀到得數(shù)據(jù),如果返回-1,則表示讀到輸入流的末尾。

  2、int read(byte[] b)

  功能:從輸入流中讀取一定量的字節(jié),并將其存儲在字節(jié)數(shù)組b中,返回實際讀取的字節(jié)數(shù),如果返回-1,則表示讀到輸入流的末尾。

  3、int read(byte[] b, int off, int len)

  功能:將數(shù)據(jù)讀入一個字節(jié)數(shù)組,同時返回讀取的實際字節(jié)數(shù),如果返回-1,則表示讀到輸入流的末尾。off指定在數(shù)組b中存放數(shù)據(jù)的起始偏移位置,len指定讀取的最大字節(jié)數(shù)。

  4、available()

  功能:返回此輸入流下一個方法調用可以不受阻塞地從此輸入流讀取或跳過的估計字節(jié)數(shù)。

  5、close()

  功能:關閉輸入流,釋放這個流的相關資源。

二、OutputStream中寫入數(shù)據(jù)的方法如下:

  1 、int write(int b)

  功能:將b的最低的一個字節(jié)寫入此輸入流,其他三個字節(jié)丟棄。

  2、int write(byte[] b)

  功能:將指定的字節(jié)數(shù)組b寫入此輸入流。

  3、int write(byte[] b, int off, int len)

  功能:將指定byte數(shù)組中從偏移量off開始的len個字節(jié)寫入輸入流。

  4、flush()

  功能:刷新此輸入流并強制寫出所有緩沖的輸出字節(jié)數(shù)。

  5、close()

  功能:關閉輸出流,釋放這個流的相關資源。

①字節(jié)數(shù)組輸入流:

     

package com.iotest;import java.io.ByteArrayInputStream;import java.io.IOException;public class ByteArryInputStreamDemo {    public static void main(String[] args) throws IOException {        String str = "abcdefghijk";        byte[] strBuf = str.getBytes();  //字符串轉換成字節(jié)數(shù)組        ByteArrayInputStream bais = new ByteArrayInputStream(strBuf);        int data = bais.read();          //從字節(jié)數(shù)組輸入流讀取字節(jié)        while(data!=-1){            char upper = Character.toUpperCase((char)data);            System.out.PRint(upper+" ");            data = bais.read();        }        bais.close();    }}

程序運行結果:A B C D E F G H I J K

②字節(jié)數(shù)組輸出流:

package com.iotest;import java.io.ByteArrayOutputStream;import java.io.IOException;public class ByteArrayOutputStreamDemo {    public static void main(String[] args) throws IOException {        ByteArrayOutputStream baos = new ByteArrayOutputStream();        String s = "welcome to use ByteArrayOutputStreamDemo";        byte[] buf = s.getBytes();          baos.write(buf); //將指定的byte數(shù)組寫到字節(jié)數(shù)組輸出流中        System.out.println(baos.toString());  //將字節(jié)數(shù)組輸出流內容轉換成字符串輸出        //將字節(jié)數(shù)組輸出流中的內容復制到字節(jié)數(shù)組中        byte[] b = baos.toByteArray();        for (int i = 0; i < b.length; i++) {            System.out.print((char)b[i]);        }        baos.close();    }}程序運行結果:welcome to use ByteArrayOutputStreamDemowelcome to use ByteArrayOutputStreamDemo

③文件輸入輸出流的使用

package com.iotest;import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;//復制圖片public class FileInputStreamDemo {    public static void main(String[] args) throws IOException {        File file = new File("F://shar//test//logo17.gif");        FileInputStream fis = new FileInputStream(file); //創(chuàng)建一個輸入流        //創(chuàng)建一個輸出流,后面一個參數(shù)true表示追加,原有內容不會被清除,默認為false        FileOutputStream fos = new FileOutputStream("F://shar//test//logo18.gif",false);        int ch = 0;        //方式一        /*while((ch=fis.read()) != -1){            fos.write(ch);        }*/        //方式二        /*byte[] b = new byte[1024];        while((ch=fis.read(b)) != -1){            fos.write(b,0,ch);        }*/        //方式三        byte[] b = new byte[fis.available()];        fis.read(b); //首先把fis的內容讀到字節(jié)數(shù)組b里面        fos.write(b);//再把字節(jié)數(shù)組b的內容通過輸出流寫到指定文件        //關閉流        fos.close();        fis.close();    }}④管道流的使用:  一個PipedInputStream對象必須和一個PipedOutputStream對象進行連接從而產(chǎn)生一個通信管道。通常一個線程從管道輸出流寫入數(shù)據(jù),另一個線程從管道輸入流中讀取數(shù)據(jù)。當線程A執(zhí)行管道輸入流的read()方法時,如果暫時沒有數(shù)據(jù),這個線程就會被阻塞,只有當線程B想管道輸出流寫了數(shù)據(jù)后,線程A才會恢復運行。

package com.iotest;import java.io.IOException;import java.io.PipedInputStream;import java.io.PipedOutputStream;/* * 管道流 */class Sender extends Thread{    private PipedOutputStream out = new PipedOutputStream();    public PipedOutputStream getOut() {        return out;    }    @Override    public void run() {        String s = "hello world";        try {            out.write(s.getBytes());            out.close();        } catch (Exception e) {            // TODO: handle exception        }    }}public class Receiver extends Thread{    private PipedInputStream in;    public Receiver(Sender sender) throws IOException {        in = new PipedInputStream(sender.getOut());    }    @Override    public void run() {        try {            int data;            while((data=in.read())!=-1){                System.out.print((char)data);            }            in.close();        } catch (Exception e) {            // TODO: handle exception        }    }    public static void main(String[] args) throws IOException {        Sender sender = new Sender();        Receiver r = new Receiver(sender);        sender.start();        r.start();    }}⑤緩沖流的使用:

package com.iotest;import java.io.BufferedInputStream;import java.io.BufferedOutputStream;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;public class TestPrime {    private BufferedInputStream bis = null;    private BufferedOutputStream bos = null;    String fileName = "F://shar//test//test2.txt";    static int s,p;    //判斷是否是質數(shù)    public boolean isPrime(int n){        for(int i=2;i<=n/2;i++){            if(n%i == 0){                return false;            }        }        return true;    }    void printPrime(int m) throws IOException{        //將字節(jié)流轉緩沖流        bos = new BufferedOutputStream(new FileOutputStream(fileName));        int j = 0;        for (int i = 2; i < m; i++) {            if(isPrime(i)){                j++;                if(j%s == 0){                    String s = String.valueOf(i)+" ";                    bos.write(s.getBytes());                    bos.write("/r/n".getBytes());                }else{                    String s = String.valueOf(i)+" ";                    bos.write(s.getBytes());                }            }        }        bos.flush();        bos.close();    }    void getPrime() throws IOException{        //將字節(jié)流轉緩沖流        bis = new BufferedInputStream(new FileInputStream(fileName));        int c = bis.read();        while(c != -1){            char ch = (char)c;            System.out.print(ch);            c = bis.read();        }    }    /**     * @param args     * @throws IOException      */    public static void main(String[] args) throws IOException {        TestPrime t = new TestPrime();        p = 100;        s = 10;        t.printPrime(p);        t.getPrime();    }}

如果不用緩沖流的話,程序是讀一個數(shù)據(jù),寫一個數(shù)據(jù)。這樣在數(shù)據(jù)量大的程序中非常影響效率。 緩沖流作用是把數(shù)據(jù)先寫入緩沖區(qū),等緩沖區(qū)滿了,再把數(shù)據(jù)寫到文件里。這樣效率就大大提高了。


發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 华容县| 石屏县| 马山县| 桃园县| 开平市| 林口县| 襄城县| 贡山| 呼和浩特市| 普定县| 宜兴市| 丰顺县| 广水市| 义乌市| 青河县| 黄浦区| 临清市| 潢川县| 松潘县| 县级市| 霍邱县| 昭苏县| 达日县| 壶关县| 大洼县| 瑞丽市| 韶关市| 奈曼旗| 冕宁县| 秦皇岛市| 沾益县| 旌德县| 若尔盖县| 玉树县| 蓝山县| 淮南市| 佛学| 修文县| 固安县| 博爱县| 措勤县|