最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Java細(xì)數(shù)IO流底層原理到方法使用

 更新時(shí)間:2022年05月17日 09:14:46   作者:程序員飛鳥(niǎo)  
IO主要用于設(shè)備之間的數(shù)據(jù)傳輸,Java將操作數(shù)據(jù)流的功能封裝到了IO包中,這篇文章主要給大家介紹了關(guān)于Java新手學(xué)習(xí)之IO流簡(jiǎn)單使用的相關(guān)資料,需要的朋友可以參考下

一、什么是IO流

輸入流和輸出流。

  • 輸入流:數(shù)據(jù)從數(shù)據(jù)源(文件)到程序(內(nèi)存)的路徑
  • 輸出流:數(shù)據(jù)從程序(內(nèi)存)到數(shù)據(jù)源(文件)的路徑

二、常用的文件操作

學(xué)習(xí)目標(biāo):創(chuàng)建文件對(duì)象相關(guān)構(gòu)造器和方法

new File(String pathname)//根據(jù)路徑構(gòu)建一個(gè)File對(duì)象

new File(File parent,String child)//根據(jù)父目錄文件+子路徑構(gòu)建

new File(String parent,String child)//根據(jù)父目錄+子路徑構(gòu)建

學(xué)習(xí)任務(wù):在e盤下,創(chuàng)建文件news1.txt、news2.txt、news3.txt用三種不同方式創(chuàng)建。

三種方式簡(jiǎn)單看一下就行,后面會(huì)經(jīng)常遇到。

new File(String pathname)//根據(jù)路徑構(gòu)建一個(gè)File對(duì)象

package com.file;
import java.io.*;
public class FileCreate {
    public static void main(String[] args) {
            //方式 1 new File(String pathname)
            String filePath = "e:\\news1.txt";
            File file = new File(filePath);
            try {
                //創(chuàng)建新文件
                file.createNewFile();
                System.out.println("文件創(chuàng)建成功");
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
}

new File(File parent,String child)//根據(jù)父目錄文件+子路徑構(gòu)建

package com.file;
import java.io.*;
public class FileCreate {
    public static void main(String[] args) {
        //方式 2 new File(File parent,String child) //根據(jù)父目錄文件+子路徑構(gòu)建
        //e:\\news2.txt
            File parentFile = new File("e:\\");
            String fileName = "news2.txt";
           //這里的 file 對(duì)象,在 java 程序中,只是一個(gè)對(duì)象
           //只有執(zhí)行了 createNewFile 方法,才會(huì)真正的,在磁盤創(chuàng)建該文件
            File file = new File(parentFile, fileName);
            try {
                file.createNewFile();
                System.out.println("創(chuàng)建成功~");
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
}

new File(String parent,String child)//根據(jù)父目錄+子路徑構(gòu)建

package com.file;
import java.io.*;
public class FileCreate {
    public static void main(String[] args) {
        //方式 3 new File(String parent,String child) //根據(jù)父目錄+子路徑構(gòu)建
            String parentPath = "e:\\";
            String fileName = "news3.txt";
            File file = new File(parentPath, fileName);
            try {
                file.createNewFile();
                System.out.println("創(chuàng)建成功~");
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
}

三、獲取文件的相關(guān)信息

getName、getAbsolutePath、getParent、length、exists、isFile、isDirectory

學(xué)習(xí)任務(wù):獲取文件的大小、文件名、路徑、父File,是文件還是目錄(目錄本質(zhì)也是文件,一種特殊的文件),是否存在

四、目錄的操作和文件刪除

學(xué)習(xí)任務(wù):

  • 判斷e:\news1.txt是否存在,如果存在就刪除
  • 判斷e:\\demo02是否存在,存在就刪除,否則提示不存在
  • 判斷e:\\demo\a\b\c目錄是否存在,如果存在就提示已經(jīng)存在,否則就創(chuàng)建
package com.collection;
import java.io.File;
public class Delete {
    public static void main(String[] args) {
        String filePath="e:\\news1.txt";
        File file=new File(filePath);
        if(file.exists()){
            file.delete();
        }else {
            System.out.println("否則文件不存在~");
        }
    }
}
package com.collection;
import java.io.File;
public class Delete {
    public static void main(String[] args) {
        String filePath="e:\\demo02";
        File file=new File(filePath);
        if(file.exists()){
            if(file.delete()){
                System.out.println(filePath+"刪除成功");
            }
            else{
                System.out.println(filePath+"刪除失敗");
            }
        }else {
            System.out.println("否則目錄不存在~");
        }
    }
}
package com.collection;
import java.io.*;
public class Delete {
    public static void main(String[] args) {
        String directoryPath="e:\\demo\\a\\b\\c";
        File file=new File(directoryPath);
        if(file.exists()) {
            System.out.println(directoryPath + "存在。。");
        }
        else {
            if (file.mkdirs()){
                System.out.println(directoryPath+"創(chuàng)建成功。。");
            }else{
                System.out.println(directoryPath+"創(chuàng)建失敗。。");
            }
        }
    }
}

按操作數(shù)據(jù)單位不同分為:字節(jié)流(8bit)二進(jìn)制文件,字符流(按字符)文本文件

按數(shù)據(jù)流的流向不同分為:輸入流,輸出流

按流的角色的不同分為:節(jié)點(diǎn)流,處理流/包裝流

字節(jié)流:InputStream,OutputStream

字符流:Reader,Writer

五、IO流體系圖-常用的類

六、FileInputStream常用方法

學(xué)習(xí)任務(wù):請(qǐng)使用 FileInputStream 讀取 hello.txt 文件,并將文件內(nèi)容顯示

先在e盤下創(chuàng)建hello.txt輸入內(nèi)容hello world

package com.FileInputStream;
import java.io.FileInputStream;
import java.io.IOException;
//字節(jié)流文件的輸入程序
public class FileInputStream_ {
    public static void main(String[] args) {
        String filePath="e:\\hello.txt";
        int readData=0;
        FileInputStream fileInputStream=null;
        try {
            //創(chuàng)建 FileInputStream 對(duì)象,用于讀取文件
            fileInputStream=new FileInputStream(filePath);
            //從該輸入流讀取一個(gè)字節(jié)的數(shù)據(jù)。 如果沒(méi)有輸入可用,此方法將阻止。
            //如果返回-1 , 表示讀取完畢
            while ((readData = fileInputStream.read()) != -1) {
                System.out.print((char)readData);//轉(zhuǎn)成 char 顯示
            }
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                //關(guān)閉文件流
                fileInputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

七、FileOutputStream常用方法

學(xué)習(xí)任務(wù):請(qǐng)使用 FileOutputStream 在 abc.txt 文件,中寫(xiě)入 “hello,world”. 如果文件不存在,會(huì)創(chuàng)建 文件(注意:前提是目錄已經(jīng)存在.)

package com.hspedu.outputstream_;
import java.io.*;
public class FileOutputStream01 {
    public static void main(String[] args) {
        String filePath = "D:\\abc.txt";
        FileOutputStream fileOutputStream = null;
        try {
            //得到 FileOutputStream 對(duì)象 對(duì)象
            //1. new FileOutputStream(filePath) 創(chuàng)建方式,當(dāng)寫(xiě)入內(nèi)容是,會(huì)覆蓋原來(lái)的內(nèi)容
            // 2. new FileOutputStream(filePath, true) 創(chuàng)建方式,當(dāng)寫(xiě)入內(nèi)容是,是追加到文件后面
            fileOutputStream = new FileOutputStream(filePath);
            //寫(xiě)入一個(gè)字節(jié)
            //fileOutputStream.write('H');
            //寫(xiě)入字符串
            String str = "hello,world!";
            //str.getBytes() 可以把 字符串-> 字節(jié)數(shù)組
            //fileOutputStream.write(str.getBytes());
            /*
             write(byte[] b, int off, int len) 將 len 字節(jié)從位于偏移量 off 的指定字節(jié)數(shù)組寫(xiě)入此文件輸出流
             */
            fileOutputStream.write(str.getBytes(), 0, 3);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                fileOutputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

學(xué)習(xí)任務(wù):編程完成圖片/音樂(lè) 的拷貝

package com.hspedu.outputstream_;
import java.io.*;
public class FileCopy {
    public static void main(String[] args) {
        String srcFilePath="e:\\9030.jpg";
        String destFilePath="e:\\9031.jpg";
        FileInputStream fileInputStream=null;
        FileOutputStream fileOutputStream=null;
        try {
            fileInputStream=new FileInputStream(srcFilePath);
            fileOutputStream=new FileOutputStream(destFilePath);
            //定義一個(gè)字節(jié)數(shù)組,提高讀取效果
            byte[] buf=new byte[1024];
            int readLen=0;
            while ((readLen=fileInputStream.read(buf))!=-1){
                fileOutputStream.write(buf,0,readLen);
            }
            System.out.println("拷貝成功!");
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try{
                if(fileInputStream!=null){
                    fileInputStream.close();
                }
                if(fileOutputStream!=null){
                    fileInputStream.close();
                }
            }catch (IOException e){
                e.printStackTrace();
            }
        }
    }
}

八、FileReader常用方法

  • new FileReader(File/String)
  • read:每次讀取單個(gè)字符,返回該字符,如果到文件末尾返回-1
  • read(char[]):批量讀取多個(gè)字符到數(shù)組,返回讀取到的字符數(shù),如果到文件末尾返回-1
  • 相關(guān)API:
  • new String(char[]):將char[]轉(zhuǎn)換成String
  • new String(char[],off,len):將char[]的指定部分轉(zhuǎn)換成String

九、FileWriter常用方法

  • new FileWriter(File/String):覆蓋模式,相當(dāng)于流的指針在首端
  • new FileWriter(File/String,true):追加模式,相當(dāng)于流的指針在尾端
  • write(int):寫(xiě)入單個(gè)字符
  • write(char[]):寫(xiě)入指定數(shù)組
  • write(char[],off,len):寫(xiě)入指定數(shù)組的指定部分
  • write(string):寫(xiě)入單個(gè)字符
  • write(string[],off,len):寫(xiě)入字符串的指定部分
  • 相關(guān)API:String類:toCharArray:將String轉(zhuǎn)換成char[]
  • FileWriter使用后,必須要關(guān)閉(close)或刷新(flush), 否則寫(xiě)入不到指定的文件!

學(xué)習(xí)任務(wù):使用 FileReader 從 story.txt ,這一步先在story.txt存在數(shù)據(jù),然后在端口輸出數(shù)據(jù)顯示出來(lái)

package com.reader_;
import java.io.FileReader;
import java.io.IOException;
public class ReadFile01 {
    public static void main(String[] args) {
        String filePath="e:\\story.txt";
        FileReader fileReader=null;
        int data=0;
        //創(chuàng)建FileReader對(duì)象
        try{
            fileReader =new FileReader(filePath);
            //循環(huán)讀取 使用 read, 單個(gè)字符讀取
            while((data=fileReader.read())!=-1){
                //data數(shù)值為整數(shù)型,強(qiáng)制轉(zhuǎn)換為字符
                System.out.print((char)data);
            }
        }catch(
                IOException e){
            e.printStackTrace();
        }finally {
            try{
                if(fileReader!=null){
                    //關(guān)閉文件流
                    fileReader.close();
                }
            }catch(IOException e){
                e.printStackTrace();
            }
        }
    }
}

學(xué)習(xí)任務(wù):字符數(shù)組讀取文件

package com.reader_;
import java.io.FileReader;
import java.io.IOException;
public  class ReadFile02 {
    public static void main(String[] args) {
        String filePath="e:\\story.txt";
        FileReader fileReader=null;
        int readLen=0;
        char[] buf=new char[8];
        //創(chuàng)建FileReader對(duì)象
        try{
            fileReader =new FileReader(filePath);
            //循環(huán)讀取 使用 read(buf), 返回的是實(shí)際讀取到的字符數(shù)
            //如果返回-1, 說(shuō)明到文件結(jié)束
            while((readLen=fileReader.read(buf))!=-1){
                System.out.print(new String(buf,0,readLen));
            }
        }catch(
                IOException e){
            e.printStackTrace();
        }finally {
            try{
                if(fileReader!=null){
                    fileReader.close();
                }
            }catch(IOException e){
                e.printStackTrace();
            }
        }
    }
}

學(xué)習(xí)任務(wù):使用 FileWriter 將 “風(fēng)雨之后,定見(jiàn)彩虹” 寫(xiě)入到 note.txt 文件中

package com.hspedu.writer_;
import java.io.FileWriter;
import java.io.IOException;
public class FileWriter_ {
    public static void main(String[] args) {
        String filePath="e:\\noth.txt";
        FileWriter fileWriter=null;
        char[] chars={'a','b','c'};
        try {
            fileWriter = new FileWriter(filePath);
            //3) write(int):寫(xiě)入單個(gè)字符
            fileWriter.write('H');
            // 4) write(char[]):寫(xiě)入指定數(shù)組
            fileWriter.write(chars);
            // 5) write(char[],off,len):寫(xiě)入指定數(shù)組的指定部分
            fileWriter.write("程序員飛鳥(niǎo)".toCharArray(), 0, 3);
            // 6) write(string):寫(xiě)入整個(gè)字符串
            fileWriter.write(" 你好廣州");
            fileWriter.write("風(fēng)雨之后,定見(jiàn)彩虹");
            // 7) write(string,off,len):寫(xiě)入字符串的指定部分
            fileWriter.write("上海天津", 0, 2);
        } catch (IOException e) {
            e.printStackTrace();
        }finally{
            try {
                //對(duì)應(yīng) FileWriter , 一定要關(guān)閉流,或者 flush 才能真正的把數(shù)據(jù)寫(xiě)入文件里面
                fileWriter.flush();//關(guān)閉文件流
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        System.out.println("程序結(jié)束~");
    }
}

到此這篇關(guān)于Java細(xì)數(shù)IO流基礎(chǔ)到方法使用的文章就介紹到這了,更多相關(guān)Java IO流內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • 詳解如何獨(dú)立使用ribbon實(shí)現(xiàn)業(yè)務(wù)客戶端負(fù)載均衡

    詳解如何獨(dú)立使用ribbon實(shí)現(xiàn)業(yè)務(wù)客戶端負(fù)載均衡

    這篇文章主要為大家介紹了詳解如何獨(dú)立使用ribbon實(shí)現(xiàn)業(yè)務(wù)客戶端負(fù)載均衡,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-06-06
  • mybatis?實(shí)現(xiàn)多層級(jí)collection嵌套

    mybatis?實(shí)現(xiàn)多層級(jí)collection嵌套

    這篇文章主要介紹了mybatis?實(shí)現(xiàn)多層級(jí)collection嵌套,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-03-03
  • 2020年支持java8的Java反編譯工具匯總(推薦)

    2020年支持java8的Java反編譯工具匯總(推薦)

    這篇文章主要介紹了2020年支持java8的Java反編譯工具匯總,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-06-06
  • Spring中的事件監(jiān)聽(tīng)器使用學(xué)習(xí)記錄

    Spring中的事件監(jiān)聽(tīng)器使用學(xué)習(xí)記錄

    Spring框架中的事件監(jiān)聽(tīng)機(jī)制是一種設(shè)計(jì)模式,它允許你定義和觸發(fā)事件,同時(shí)允許其他組件監(jiān)聽(tīng)這些事件并在事件發(fā)生時(shí)作出響應(yīng),這篇文章主要介紹了Spring中的事件監(jiān)聽(tīng)器使用學(xué)習(xí),需要的朋友可以參考下
    2024-07-07
  • java生成驗(yàn)證碼圖片的方法

    java生成驗(yàn)證碼圖片的方法

    這篇文章主要為大家詳細(xì)介紹了java生成驗(yàn)證碼圖片的方法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-02-02
  • SpringBoot?AOP?Redis實(shí)現(xiàn)延時(shí)雙刪功能實(shí)戰(zhàn)

    SpringBoot?AOP?Redis實(shí)現(xiàn)延時(shí)雙刪功能實(shí)戰(zhàn)

    本文主要介紹了SpringBoot?AOP?Redis實(shí)現(xiàn)延時(shí)雙刪功能實(shí)戰(zhàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2022-08-08
  • 解讀Spring-boot的debug調(diào)試

    解讀Spring-boot的debug調(diào)試

    這篇文章主要介紹了解讀Spring-boot的debug調(diào)試,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-12-12
  • Java日常練習(xí)題,每天進(jìn)步一點(diǎn)點(diǎn)(52)

    Java日常練習(xí)題,每天進(jìn)步一點(diǎn)點(diǎn)(52)

    下面小編就為大家?guī)?lái)一篇Java基礎(chǔ)的幾道練習(xí)題(分享)。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧,希望可以幫到你
    2021-08-08
  • JAVA十大排序算法之桶排序詳解

    JAVA十大排序算法之桶排序詳解

    這篇文章主要介紹了java中的桶排序,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2021-08-08
  • Java創(chuàng)建對(duì)象的幾種方法

    Java創(chuàng)建對(duì)象的幾種方法

    這篇文章主要為大家詳細(xì)介紹了Java創(chuàng)建對(duì)象的幾種方法,使用new創(chuàng)建、使用object.clone()創(chuàng)建、使用反序列化創(chuàng)建等,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2016-12-12

最新評(píng)論

徐汇区| 千阳县| 右玉县| 琼结县| 昌邑市| 孟连| 娄底市| 炎陵县| 中宁县| 阿拉善左旗| 石泉县| 宁乡县| 马公市| 建湖县| 岳阳市| 辽中县| 婺源县| 从江县| 麻栗坡县| 江陵县| 察哈| 冕宁县| 内丘县| 枣强县| 宁武县| 静海县| 临城县| 佛学| 沁水县| 宜昌市| 大洼县| 成安县| 望都县| 通海县| 裕民县| 会理县| 英超| 手游| 漳浦县| 阳城县| 名山县|