Java輸入輸出流實(shí)例(字節(jié)流輸入、輸出)-創(chuàng)新互聯(lián)

字節(jié)流常用方法

成都創(chuàng)新互聯(lián)于2013年成立,先為永勝等服務(wù)建站,永勝等地企業(yè),進(jìn)行企業(yè)商務(wù)咨詢服務(wù)。為永勝企業(yè)網(wǎng)站制作PC+手機(jī)+微官網(wǎng)三網(wǎng)同步一站式服務(wù)解決您的所有建站問題。

輸入、輸出:

read()、write()? ?:? 返回值就是對(duì)應(yīng)的字節(jié)

read(byte[] bytes)、write(byte[] bytes)? :? 返回值就是讀取對(duì)應(yīng)的數(shù)組,如果需要查看需要便利

int read(byte【】 b,int off,int len):? off為讀取起始位置,len為讀取長(zhǎng)度

int available():可以從輸入流中讀取的字節(jié)數(shù)目

void close()? ?:? ?關(guān)閉文檔流

測(cè)試類

public static void main(String[] args) throws FileNotFoundException {
        //新建一個(gè)文件對(duì)象
        File file = new File("src/byteOut/test.txt");
        try {
            //新建此對(duì)象的txt文件
            file.createNewFile();
        } catch (IOException e) {
            e.printStackTrace();
        }
        //判斷是否創(chuàng)建成功
        if (file.exists()) {
            //獲取文件地址
            String uri=file.getPath();
            Test test=new Test();
            test.write(file,"1a");
            test.write(file,"1a",true);
            test.read(file);
            test.write(file,"2b");
            test.write(file,"2b");
            test.read(uri);
            test.write(file,"abcdefgh",2,3);
            test.read(uri,2,4);
        }else{
            System.out.println("不存在");
        }
    }
//輸入類方法,傳入文件
public void read(File file){
        //新建一個(gè)空文件流,防止關(guān)閉輸入流時(shí)作用域報(bào)錯(cuò)
        InputStream inputStream = null;
        try {
            //導(dǎo)入文件
            inputStream=new FileInputStream(file);
            //打印文件大?。鹤止?jié)
//            System.out.println(inputStream.available());
            int leng = 0;
            //判斷文件內(nèi)容是否為空,-1為空
            //leng得到的是read方法獲得的字節(jié)數(shù)量,如果沒有則為-1
            while ((leng = inputStream.read()) != -1) {
                //打印輸入到內(nèi)存的內(nèi)容,強(qiáng)轉(zhuǎn)換為字符串類型
                System.out.println((char) leng);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                //關(guān)閉輸入流
                inputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
//輸入類方法,傳入文件地址
public void read(String uri){
        //新建一個(gè)空文件流,防止關(guān)閉輸入流時(shí)作用域報(bào)錯(cuò)
        InputStream inputStream = null;
        try {
            //導(dǎo)入文件
            inputStream = new FileInputStream(uri);
            //打印文件大?。鹤止?jié)
//            System.out.println(inputStream.available());
            //定義一個(gè)長(zhǎng)度為文件字節(jié)大小的數(shù)組,一次性讀取出來
            byte[] bytes=new byte[inputStream.available()];
            //inputStream.read(bytes)得到的是read方法獲得的字節(jié)數(shù)量,如果沒有則為-1
            while ((inputStream.read(bytes)) != -1) {
                //定義字符遍歷數(shù)組
                String str=new String(bytes);
                System.out.println(str);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                inputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
//輸入類方法,傳入文件地址,start為讀取起始位置,len為讀取長(zhǎng)度
public void read(String uri,int start,int len){
        InputStream inputStream =null;
        try {
            inputStream = new FileInputStream(uri);
//            System.out.println(inputStream.available());
            //定義一個(gè)1024長(zhǎng)度的數(shù)組,每次讀取1024字節(jié)
            byte[] bytes=new byte[1024];
            //判斷讀取字節(jié)數(shù)量是否為空,為空則為-1
            while ((inputStream.read(bytes,start,len)) != -1) {
                //使用循環(huán)遍歷數(shù)組,打印輸入的文檔
                for (int i = 0; i< bytes.length; i++) {
                    //判斷是否為空
                    if (bytes[i]==0){
                        continue;
                    }
                    System.out.println((char)bytes[i]);
                }
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                inputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
//寫入類方法,傳入文件地址,和寫入字符串
public void write(File file,String string){
        OutputStream outputStream=null;
        try {
            outputStream=new FileOutputStream(file);
            //將想要輸入的字符寫入bytes數(shù)組,文件編碼為UTF-8
            byte[] bytes=string.getBytes(StandardCharsets.UTF_8);
            //寫入數(shù)組
            outputStream.write(bytes);
            //刷新緩存
            outputStream.flush();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                outputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }
//寫入類方法,傳入文件地址,和寫入字符串,是否覆蓋:true為不覆蓋
public void write(File file,String string,boolean choose){
        OutputStream outputStream=null;
        try {
            outputStream=new FileOutputStream(file,choose);
            byte[] bytes=string.getBytes(StandardCharsets.UTF_8);
            outputStream.write(bytes);
            outputStream.flush();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                outputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }
//寫入類方法,寫入文件地址,寫入字符,start為寫入起始位置,len為寫入長(zhǎng)度
public void write(File file,String string,int off,int len){
        OutputStream outputStream=null;
        try {
            outputStream=new FileOutputStream(file);
            byte[] bytes=string.getBytes(StandardCharsets.UTF_8);
            outputStream.write(bytes,off,len);
            outputStream.flush();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                outputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }
//文檔復(fù)制方法,file為源文件,file1為被覆蓋文件
public void copy(File file,File file1){
        InputStream inputStream=null;
        OutputStream outputStream=null;
        try {
            inputStream=new FileInputStream(file);
            outputStream=new FileOutputStream(file1);
            //單字節(jié)復(fù)制效率較慢
//            int leng=0;
//            while ((leng=inputStream.read())!=-1){
//                outputStream.write(leng);
//            }
            //整體復(fù)制
            byte[] bytes=new byte[inputStream.available()];
            inputStream.read(bytes);
            outputStream.write(bytes);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                outputStream.close();
                inputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

你是否還在尋找穩(wěn)定的海外服務(wù)器提供商?創(chuàng)新互聯(lián)www.cdcxhl.cn海外機(jī)房具備T級(jí)流量清洗系統(tǒng)配攻擊溯源,準(zhǔn)確流量調(diào)度確保服務(wù)器高可用性,企業(yè)級(jí)服務(wù)器適合批量采購,新人活動(dòng)首月15元起,快前往官網(wǎng)查看詳情吧

網(wǎng)頁名稱:Java輸入輸出流實(shí)例(字節(jié)流輸入、輸出)-創(chuàng)新互聯(lián)
文章URL:http://muchs.cn/article20/hieco.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供自適應(yīng)網(wǎng)站、Google、微信公眾號(hào)、網(wǎng)站排名、關(guān)鍵詞優(yōu)化、靜態(tài)網(wǎng)站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

成都定制網(wǎng)站建設(shè)