【總】Android之IO流/文件導(dǎo)航

注意:本篇文章是本人閱讀相關(guān)文章的總結(jié),方便以后查閱,所有內(nèi)容非原創(chuàng),侵權(quán)刪。

本篇文章內(nèi)容來自于
1.Android基礎(chǔ)之IO流

目錄

一、File類
--1.1 File類的構(gòu)造方法
--1.2 File類的創(chuàng)建方法
--1.3 File類的常用方法

二、IO流
1.IO流分類
2.InputStream/OutputStream(字節(jié)流基類 拷貝用這個(gè))
3.Reader/Writer(字符流基類 只讀或者只寫用這個(gè))
4.FileInputStream/FileOutputStream(文件輸入輸出流,一般都用這個(gè))
5.BufferedInputStream/BufferedOutputStream(字節(jié)緩沖流,減少與硬盤的交流次數(shù),加快速度,需要flush()才可寫入)
6.IO流如何處理異常
7.FileReader/FileWriter(文件字符流)
8.BufferedReader/BufferedWriter(緩沖字符流,需要flush()才可寫入)
9.InputStreamReader/OutputStreamWriter轉(zhuǎn)換流(將字節(jié)流轉(zhuǎn)換成字符流)
10.ByteArrayInputStream/ByteArrayOutputStream(數(shù)組字符流,可以將流寫入到內(nèi)存中,然后獲取所有結(jié)果)
11.DataInputStream/DataOutputStream(基本數(shù)據(jù)流,可以以基本數(shù)據(jù)的形式寫入和讀取)
12.ObjectInputStream/ObjectOutputStream(對(duì)象操作流,可以序列化或者反序列化)

三、IO流應(yīng)用
1.處理流(BufferedXXX)配合節(jié)點(diǎn)流(XXXInputStream和XXXWriter/Reader)
2.輸入流(FileInputStream等)配合使用ByteArrayOutputStream(內(nèi)存數(shù)組流),將輸入流內(nèi)容一次性輸出
3.使用ObjectOutputStream將得到的密鑰Key對(duì)象存儲(chǔ)

一、File類

1.1 File類的構(gòu)造方法

public File(String pathname)
public File(String parent, String child)
public File(File parent, String child)
public File(URI uri)

1.2 File類的創(chuàng)建方法

文件有無后綴都會(huì)創(chuàng)建

public boolean createNewFile() //創(chuàng)建文件,文件存在則不創(chuàng)建
public boolean mkdir() //創(chuàng)建文件夾,存在則不創(chuàng)建
public boolean mkdirs() //創(chuàng)建文件夾,父文件夾不存在也會(huì)創(chuàng)建

1.3 File類的常用方法

//重命名功能
//把文件重命名為指定的文件路徑。如果路徑相同,則改名即可;如果路徑不同,則改名且將文件移動(dòng)到指定目錄下。
public boolean renameTo(File dest)

//刪除功能
//刪除文件或者文件夾(其中文件夾中不能有其他文件或文件夾)
public boolean delete() 

//判斷功能
public boolean isDirectory()//判斷是否是目錄
public boolean isFile()//判斷是否是文件
public boolean exists()//判斷是否存在

//獲取功能
public String getAbsolutePath()//獲取絕對(duì)路徑
public String getPath()//獲取路徑 指在new File(xxx)時(shí)傳的參數(shù)
public String getName()//獲取文件/文件夾名
public long length()//獲取長度 字節(jié)數(shù)
public long lastModified()//獲取最后一次的修改時(shí)間 ms值
public String[] list()//獲指定目錄下的所有文件/文件夾的名稱
public File[] listFiles()//獲指定目錄下的所有文件/文件夾

二、 IO流

IO程序書寫規(guī)范:
使用前要導(dǎo)入IO包中的類
使用時(shí)要進(jìn)行IO異常處理
使用后要釋放資源

1. IO流分類

輸入流/輸出流(按流向分)
輸入流是寫入到內(nèi)存 InputStream、Reader
輸出流是寫出到存儲(chǔ)設(shè)備 OutputStream、Writer

字節(jié)流/字符流(按操作類型分)
字節(jié)流可操作任何數(shù)據(jù) InputStream、OutputStream
字符流只能操作純字符數(shù)據(jù) Reader、Writer

什么時(shí)候用字符流?什么時(shí)候用字節(jié)流?
字符流->用于只讀只寫,這樣就不會(huì)出現(xiàn)半個(gè)中文的情況
字節(jié)流->用于拷貝(如果用字符流,還要進(jìn)行字節(jié)到字符到字節(jié)的轉(zhuǎn)換,還會(huì)亂碼)

節(jié)點(diǎn)流/處理流
節(jié)點(diǎn)流的的構(gòu)造參數(shù)是物理IO節(jié)點(diǎn),比如public FileInputStream(File file)
處理流的構(gòu)造參數(shù)是已經(jīng)存在的流(裝飾設(shè)計(jì)模式),比如public BufferedOutputStream(OutputStream out)

常用節(jié)點(diǎn)流


常用處理流

2. InputStream/OutputStream 字節(jié)流

InputStream是字節(jié)輸入流的抽象基類

//InputStream的三個(gè)重要方法
//都是將內(nèi)容以字節(jié)(byte)的形式讀取到輸入流中。
int read()
int read(byte[] b)
int read(byte[] b , int off ,int len)

OutputStream是字節(jié)輸入流的抽象基類

//OutputStream的三個(gè)重要方法
void write(byte[] b)
void write(byte[] b, int off, int len)
void write(int b)

3. Reader/Writer 字符流

Reader是字符輸入流的抽象基類

//Reader的重要方法
//都是將內(nèi)容以字符(char)的形式讀取到輸入流中。
int read()
int read(char[] b)
int read(char[] b , int off ,int len)

Writer是字符輸出流的抽象基類

void write(char[] b)
void write(char[] b, int off, int len)
void write(int b)

void write(String str)
void write(String str, int off, int len)
Writer append(char c)
Writer append(CharSequence csq)
Writer append(CharSequence csq, int start, int end)

4.FileInputStream/FileOutputStream

Android-IO流之文件輸入輸出字節(jié)流FileInputStream/FileOutputStream

5.BufferedInputStream/BufferedOutputStream

Android-IO流之緩沖字節(jié)流BufferedInputStream/BufferedOutputStream

6.IO流如何處理異常

處理方式一:

File dir = Environment.getExternalStorageDirectory();
File file = new File(dir, "abc.txt");
File dst = new File(dir, "dst1.text");

FileInputStream fis = null;
FileOutputStream fos = null;
try {
    fis= new FileInputStream(file);
    fos= new FileOutputStream(dst);

    //讀寫步驟 
    ...
} catch (Exception e) {
    e.printStackTrace();
}finally {
    if(fis!=null){
         try {
               fis.close();
         } catch (IOException e) {
               e.printStackTrace();
               fis = null; //手動(dòng)關(guān)流
         }
     }
     if(fos!=null){
         try {
              fos.close();
         } catch (IOException e) {
              e.printStackTrace();
              fos = null; //手動(dòng)關(guān)流
         }
    }
}

處理方式二:

File dir = Environment.getExternalStorageDirectory();
File file = new File(dir, "abc.txt");
File dst = new File(dir, "dst1.text");

FileInputStream fis = null;
FileOutputStream fos = null;
try {
    fis= new FileInputStream(file);
    fos= new FileOutputStream(dst);

    //讀寫步驟 
    ...
} catch (Exception e) {
    e.printStackTrace();
}finally { 

          //能關(guān)一個(gè)是一個(gè)
            try {
                if (fis != null) {
                    fis.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                if (fos != null) {
                    try {
                        fos.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
}

7.FileReader/FileWriter

Android-IO流之文件字符流FileReader/FileWriter

8. BufferedReader/BufferedWriter

Android-IO流之緩沖字符流BufferedReader/BufferedWriter

9.InputStreamReader/OutputStreamWriter轉(zhuǎn)換流(extends Reader)

Android-IO流之轉(zhuǎn)換流InputStreamReader/OutputStreamWriter

10.ByteArrayInputStream/ByteArrayOutputStream

Android-IO流之?dāng)?shù)組內(nèi)存字節(jié)流ByteArrayInputStream/ByteArrayOutputStream

11.DataInputStream/DataOutputStream數(shù)據(jù)流

Android-IO流之?dāng)?shù)據(jù)流DataInputStream/DataOutputStream

12.ObjectInputStream/ObjectOutputStream對(duì)象操作流

Android-IO流之對(duì)象操作流ObjectInputStream/ObjectOutputStream

三、IO流應(yīng)用

1.處理流(BufferedXXX)配合節(jié)點(diǎn)流(XXXInputStream/OutputStream和XXXWriter/Reader)

實(shí)例:當(dāng)系統(tǒng)提供的方法返回的是FileOutputStream時(shí),想寫入字符串,則配合使用緩存流BufferedWriter和轉(zhuǎn)換流OutputStreamWriter

        String data = "hello my life";
        FileOutputStream fileOutputStream = openFileOutput("hello.txt", Context.MODE_PRIVATE);
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(fileOutputStream));
        bw.write(data);
        bw.close();

實(shí)例:當(dāng)系統(tǒng)提供的方法返回是FileInputStream時(shí),想讀出字符串信息。則配合使用BufferedReader和InputStreamReader

        FileInputStream fileInputStream = openFileInput("hello.txt");
        BufferedReader br = new BufferedReader(new InputStreamReader(fileInputStream));
        StringBuilder sb = new StringBuilder();
        String line = null;
        while ((line = br.readLine()) != null) {
            sb.append(line);
        }
        Log.d("xl", sb.toString());
        br.close();

2. 輸入流(FileInputStream等)配合使用ByteArrayOutputStream(內(nèi)存數(shù)組流),將輸入流內(nèi)容一次性輸出

實(shí)例:將文件中的內(nèi)容讀出顯示

            String fileName = "sdcontent.txt";
            File dir = Environment.getExternalStorageDirectory();
            File file = new File(dir, fileName);

            FileInputStream fis = new FileInputStream(file);
            ByteArrayOutputStream baos = new ByteArrayOutputStream();

            byte[] buffer = new byte[1024];
            int len = -1;
            while ((len = fis.read(buffer)) != -1) {
                baos.write(buffer,0,len);
            }

            Log.d("xl", baos.toString());
            fis.close();

3.使用ObjectOutputStream將得到的密鑰Key對(duì)象存儲(chǔ)

//生成隨機(jī)秘鑰
SecretKey secretKey = KeyGenerator.getInstance("AES").generateKey();
//序列化秘鑰到磁盤上
FileOutputStream fos = new FileOutputStream(new File("heima.key"));
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(secretKey);

//從磁盤里讀取秘鑰
FileInputStream fis = new FileInputStream(new File("heima.key"));
ObjectInputStream ois = new ObjectInputStream(fis);
Key key = (Key) ois.readObject();
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 一、流的概念和作用。 流是一種有順序的,有起點(diǎn)和終點(diǎn)的字節(jié)集合,是對(duì)數(shù)據(jù)傳輸?shù)目偝苫虺橄?。即?shù)據(jù)在兩設(shè)備之間的傳輸...
    布魯斯不吐絲閱讀 10,320評(píng)論 2 95
  • 在經(jīng)過一次沒有準(zhǔn)備的面試后,發(fā)現(xiàn)自己雖然寫了兩年的android代碼,基礎(chǔ)知識(shí)卻忘的差不多了。這是程序員的大忌,沒...
    猿來如癡閱讀 3,122評(píng)論 3 10
  • # 3.1 File # ## 3.1.1 File基本概念 ## 1.基本概念 -File類用于表示文件(目錄)...
    閆子揚(yáng)閱讀 561評(píng)論 0 0
  • 阿蘭德波頓真是很好玩,他說成熟的愛就是一種有性關(guān)系的友誼,大家相處和睦,令人愉悅,彼此回應(yīng),而不成熟的愛則是一場溺...
    翻譯小垃圾閱讀 167評(píng)論 0 0
  • 我很確信啊。因?yàn)槲乙淮我淮蔚挠∽C到,當(dāng)我內(nèi)心平靜的時(shí)候,我能感受到這個(gè)世界上很多很多快樂的東西。這種內(nèi)心平靜就是當(dāng)...
    weeklybright閱讀 69評(píng)論 0 0

友情鏈接更多精彩內(nèi)容