在线不卡日本ⅴ一区v二区_精品一区二区中文字幕_天堂v在线视频_亚洲五月天婷婷中文网站

  • <menu id="lky3g"></menu>
  • <style id="lky3g"></style>
    <pre id="lky3g"><tt id="lky3g"></tt></pre>

    Java中的File類和IO流

    Flie類

    1.什么是File類

    File類就是當前系統中,文件或文件夾的抽象表示。

    通過使用File類的實例對象,我們就可以通過代碼實現計算機的文件控制,即文件的增刪改查等操作。

    2.File類的使用

    首先為了保證其他文件的安全性,建議先建立一個Test文件夾專門用于File對象的聯系。

    public static void main(String[] args) throws IOException {

    File f1 = new File(“D:/AAAkejian/作業(yè)/test”);

    File f2 = new File(“D:AAAkejian作業(yè)test1”);

    f1.mkdir();

    f2.mkdir();

    f1 = new File(“D:/AAAkejian/作業(yè)/test/test.txt”);

    f1.createNewFile();

    File f3 = new File(“D:”+File.separator+

    “AAAkejian”+File.separator+

    “作業(yè)”+File.separator+”test1”);

    }

    其中:

    D:/AAAkejian/作業(yè)/test,代表路徑名,“ / ”和“”都表示路徑分隔符;

    在windows系統下,兩種分隔符都支持,在Linux和Mac系統下只支持“ / ”

    使用File.separator,表示動態(tài)分隔符。但在日常開發(fā)中,由于windows系統中兩者都支持,而且在使用“ ”時,還需要考慮轉義字符,因此我們常用的路徑分隔符為“ / ”;

    3.File對象的使用

    1)創(chuàng)建目錄或文件

    File f1 = new File(“D:/AAAkejian/作業(yè)/test”);

    //創(chuàng)建目錄

    f1.mkdir();

    f1 = new File(“D:/AAAkejian/作業(yè)/test/test.txt”);

    //創(chuàng)建文件

    f1.createNewFile();

    f1 = new File(“D:/AAAkejian/作業(yè)/test/test2”);

    //創(chuàng)建多級目錄

    f1.mkdirs();

    2)刪除目錄或文件

    //刪除目錄或文件

    f1.delete();

    //當程序結束時再進行刪除

    f2.deleteOnExit();

    3)修改目錄或文件

    IO流

    1.什么是IO流

    IO分別是兩個單詞的首字母縮寫,I:Inputstream輸入流 O:Outputstream輸出流

    2.IO流的作用:對文件中的內容進行操作;

    輸入:讀操作(讀取文件的內容) 輸出:寫操作(向文件中寫內容)

    這里的輸入輸出是針對java程序而言,從文件中讀取內容到Java程序即為輸入;

    同理,從Java程序向文件中寫入內容就叫輸出。

    3.IO流的方向

    1)根據流的方向

    輸入流:程序可以從中讀取數據的流

    輸出流:程序可以從中寫入數據的流

    2)根據流的單位

    字節(jié)流:以字節(jié)為單位傳輸數據的流

    字符流:以字符為單位傳輸數據的流

    3)根據功能

    節(jié)點流:直接和文件進行交互

    處理流:不直接作用在文件上

    IO流中有(字節(jié)輸入/輸出流、字符輸入/輸出流)四個基本流,其他的流都是再這四個流的基礎上進行拓展的

    4.Writer字符輸出流

    1)Writer類是所有字符輸出流的跟類

    2)使用Writer向文件中添加內容

    public static void main(String[] args) throws IOException {

    Writer w1 = new FileWriter(“D:/AAAkejian/WriterTest/test2.txt”);

    String str = “這是第二遍練習”;

    w1.write(str);

    //該方式則可以進行添加而不覆蓋

    Writer w2 = new FileWriter(“D:/AAAkejian/WriterTest/test2.txt”,true);

    str = “這是添加的內容”;

    w2.write(str);

    //append也可用于添加

    w1.append(str);

    //刷新流

    w1.flush();

    //關閉流

    w1.close();

    w2.flush();

    w2.close();

    }

    3)使用Writer直接向文件內添加內容時,后添加的會覆蓋先添加的內容,那么在進行內容追加時就需要添加上一個true,表示允許追加內容到文件中。見上圖

    5.Reader字符輸入流

    1)Reader類是所有字符輸入流的跟類

    2)使用FileReader實現類進行文件的讀取操作:

    public static void main(String[] args) throws IOException {

    Reader reader = new FileReader(“D:/AAAkejian/ReaderTest/test1.txt”);

    int count = 0;

    char[] cList = new char[10];

    while( (count=reader.read(cList)) !=-1 ){

    String str=new String(cList,0,count);

    System.out.print(str);

    }

    }

    6,結合輸入輸出流可以實現文件的復制功能

    public void test1() throws IOException {

    //創(chuàng)建字符輸入流

    FileReader fr = new FileReader(“D:/AAAkejian/Test/test1.txt”);

    //創(chuàng)建字符輸出流

    FileWriter fw = new FileWriter(“D:/AAAkejian/Test/test2.txt”);

    //記錄讀取到的個數

    int count = 0;

    //每次讀取的內容放入這個數組中

    char[] cList = new char[10];

    while ((count = fr.read(cList))!=-1){

    fw.write(cList,0,count);

    fw.flush();

    }

    fw.close();

    fr.close();

    }

    這里需要注意的是,字符流只適用于文本的輸入輸出,對于圖片,視頻等二進制文件則無法使用字符流進行操作。

    7.字節(jié)流

    1)字節(jié)輸出流——OutputStream(所有字節(jié)輸出流的父類)

    字節(jié)輸出流的使用,以FileOutputStream為例

    public void test1() throws Exception {

    //定義字節(jié)輸出流對象

    OutputStream osp = new FileOutputStream(“D:/AAAkejian/Test/test1.txt”);

    String str = “Add abcd”;

    //將字符串轉化為字節(jié)存入字節(jié)數組中

    byte[] bList = str.getBytes();

    //寫入文件

    osp.write(bList);

    //刷新流

    osp.flush();

    //關閉流

    osp.close();

    }

    2)字節(jié)輸入流——InputStream(所有字節(jié)輸入流的父類)

    字節(jié)輸入流的使用,以FileInputStream為例,這里的讀取規(guī)則,與字符輸入流類似,利用循環(huán)進行循環(huán)讀取文件內容。

    public void test() throws Exception{

    //創(chuàng)建字節(jié)輸入流對象

    InputStream ips = new FileInputStream(“D:/AAAkejian/Test/test1.txt”);

    byte[] bList = new byte[3000];

    int count = 0;

    while( (count=ips.read(bList))!=-1 ){

    //把byte數組轉換為字符串

    String str=new String(bList,0,count);

    System.out.println(str);

    }

    ips.close();

    }

    3)利用字節(jié)輸入輸出流完成圖片的復制功能

    public void test()throws Exception{

    //定義字節(jié)輸入流對象

    InputStream ips = new FileInputStream(“D:/AAAkejian/Test/test3.png”);

    //定義字節(jié)輸出流對象

    OutputStream ops = new FileOutputStream(“D:/AAAkejian/Test/test1.png”);

    //定義字節(jié)數組,用于存儲所讀取的內容

    byte[] bList = new byte[100];

    int count =0;

    while ((count = ips.read(bList))!=-1){

    ops.write(bList,0,count);

    ops.flush();

    }

    ops.close();

    ips.close();

    }

    8.緩存流

    緩存流是在基礎流(InputStream OutputStream Reader Writer)之上,添加了一個緩沖池的功能,用于提高IO效率,降低IO次數

    緩沖流的使用:

    public void test()throws Exception{

    //定義字節(jié)輸出流

    OutputStream ops = new FileOutputStream(“D:/AAAkejian/Test/test1.txt”);

    //定義緩存流對象

    BufferedOutputStream bfops = new BufferedOutputStream(ops);

    String str = “new content”;

    byte[] bList = str.getBytes();

    //此時的內容在緩沖池中,并未寫入文件

    bfops.write(bList);

    //刷新緩沖池,將緩沖池中的內容寫入文件中

    //bfops.flush();

    //h緩存流中的close方法,會先執(zhí)行flush方法

    bfops.close();

    }

    9.對象流

    對象流的意義在于數據的持久化,例如游戲存到,就是一種對象流的使用。

    在日常程序運行中,內容都是存儲在內存中的,而將內存中的數據,存儲到磁盤中,就實現了數據的持久化。

    1)對象流輸出的使用——ObjectOutputStream–序列化(存檔):

    public class Role implements Serializable {

    private String name;

    private int level;

    private String power;

    public Role(String name, int level, String power) {

    this.name = name;

    this.level = level;

    this.power = power;

    }

    public String getName() {

    return name;

    }

    public void setName(String name) {

    this.name = name;

    }

    public int getLevel() {

    return level;

    }

    public void setLevel(int level) {

    this.level = level;

    }

    public String getPower() {

    return power;

    }

    public void setPower(String power) {

    this.power = power;

    }

    }

    public void test()throws Exception{

    OutputStream ops = new FileOutputStream(“d:/AAAkejian/Test/test3.txt”);

    ObjectOutputStream oops = new ObjectOutputStream(ops);

    //使用對象流調用輸出流的輸出方法,被輸出的對象的類必須實現Serializable接口

    Role r1 = new Role(“gjx”,55,”撒潑打滾”);

    oops.writeObject(r1);

    oops.close();

    }

    2)對象輸入流的使用——ObjectInputStream–反序列化(讀檔):

    public void test()throws Exception{

    InputStream ips = new FileInputStream(“D:/AAAkejian/Test/test3.txt”);

    ObjectInputStream oips = new ObjectInputStream(ips);

    Object o = oips.readObject();

    System.out.println(o);

    oips.close();

    }

    鄭重聲明:本文內容及圖片均整理自互聯網,不代表本站立場,版權歸原作者所有,如有侵權請聯系管理員(admin#wlmqw.com)刪除。
    上一篇 2022年6月22日 15:34
    下一篇 2022年6月22日 18:09

    相關推薦

    聯系我們

    聯系郵箱:admin#wlmqw.com
    工作時間:周一至周五,10:30-18:30,節(jié)假日休息