日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 人文社科 > 生活经验 >内容正文

生活经验

java字节流

發(fā)布時間:2023/11/27 生活经验 39 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java字节流 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

一? 字節(jié)流

1.1字節(jié)輸出流OutputStream

OutputStream是一個抽象類,操作的數(shù)據(jù)都是字節(jié)。

輸出流中定義都是寫write方法,如下圖:

1.1.1 FileOutputStream類

OutputStream有很多子類,其中子類FileOutputStream可用來寫入數(shù)據(jù)到文件。FileOutputStream類,即文件輸出流,是用于將數(shù)據(jù)寫入 File的輸出流。

構(gòu)造方法:

將數(shù)據(jù)寫入文件中

    public static void method01() throws IOException{//創(chuàng)建字節(jié)輸出流對象//如果該文件有則覆蓋,如果沒有則覆蓋FileOutputStream fos=new FileOutputStream("E:\\java\\demo.txt");fos.write(100);//ASCII碼//釋放資源
        fos.close();}
    public static void method02() throws IOException{//創(chuàng)建字節(jié)輸出流對象//如果該文件有則覆蓋,如果沒有則覆蓋FileOutputStream fos=new FileOutputStream("E:\\java\\demo.txt");//byte[] bytes={97,98,99,100};//fos.write(bytes,1,1);//字符串轉(zhuǎn)字節(jié)數(shù)組fos.write("你好嗎,中國".getBytes());//釋放資源
        fos.close();}
public static void method03() throws IOException{//創(chuàng)建字節(jié)輸出流對象//如果該文件有則覆蓋,如果沒有則覆蓋FileOutputStream fos=new FileOutputStream("E:\\java\\demo.txt",true);//換行 \r\n//字符串轉(zhuǎn)字節(jié)數(shù)組fos.write("hello,java".getBytes());//釋放資源
        fos.close();}

給文件續(xù)寫和換行時,在FileOutputStream的構(gòu)造函數(shù)中,可以接受一個boolean類型的值,如果值true,就會在文件末位繼續(xù)添加。

1.2字節(jié)輸入流InputStream

InputStream是一個抽象類,定義了讀的方法

1.2.1 FileInputStream類

FileInputStream類是InputStream的實現(xiàn)類,用它來讀取文件內(nèi)容

構(gòu)造方法有

用它來讀取數(shù)據(jù)

1 單個字節(jié)讀

public static void method01() throws IOException{//明確數(shù)據(jù)源FileInputStream fis=new FileInputStream("E:\\java\\demo.txt");//從文件中讀取一個字節(jié)int len1=fis.read();//強轉(zhuǎn)字符型   加charSystem.out.println((char)len1);len1=fis.read();System.out.println((char)len1);len1=fis.read();System.out.println((char)len1);len1=fis.read();System.out.println((char)len1);len1=fis.read();System.out.println((char)len1);len1=fis.read();System.out.println(len1);//釋放資源
        fis.close();}

2.單個字節(jié)循環(huán)讀

public static void method02() throws IOException{FileInputStream fis=new FileInputStream("E:\\java\\demo.txt");int len=0;while((len=fis.read())!=-1){System.out.println((char)len);}//釋放資源
        fis.close();}

3.用數(shù)組的形式一個個讀

public static void method03() throws IOException{FileInputStream fis=new FileInputStream("E:\\java\\demo.txt");//創(chuàng)建數(shù)組byte[] bytes=new byte[2];int len=fis.read(bytes);System.out.println(new String(bytes));System.out.println(len);len=fis.read(bytes);System.out.println(new String(bytes));System.out.println(len);len=fis.read(bytes);System.out.println(new String(bytes));System.out.println(len);//釋放資源
        fis.close();}

4.用數(shù)組的形式循環(huán)讀

    public static void method04() throws IOException{FileInputStream fis=new FileInputStream("E:\\java\\demo.txt");//創(chuàng)建數(shù)組byte[] bytes=new byte[2];int len=0;while((len=fis.read(bytes))!=-1){System.out.println(new String(bytes,0,len));}//釋放資源
        fis.close();}

字節(jié)流的練習:進行文件的復(fù)制

//文件的復(fù)制public static void method05() throws IOException{//數(shù)據(jù)源FileInputStream fis=new FileInputStream("E:\\java\\demo.txt");//目的地FileOutputStream fos=new FileOutputStream("F:\\demo.txt");//開始復(fù)制int len=0;while((len=fis.read())!=-1){fos.write(len);}//釋放資源
        fis.close();fos.close();}

?

轉(zhuǎn)載于:https://www.cnblogs.com/zzq123/p/10219142.html

總結(jié)

以上是生活随笔為你收集整理的java字节流的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。