1. 程式人生 > >四種讀寫方案IO流 (JAVA)

四種讀寫方案IO流 (JAVA)

File類用於訪問檔案或目錄的屬性

流:指一連串流動的字元,是以先進先出的方式傳送資訊的通道。程式和資料來源之間是通過流聯絡起來的。

第一套:位元組流讀取寫入方案

FileInputStream :位元組流方式讀取文字檔案

        FileInputStream fis=new FileInputStream("E:\\讀取檔案.txt");
        byte[]bytes=new byte[1024];
        int data;
        while((data=fis.read(bytes))!=-1)
        {
            String str=new String(bytes,0,data);
            System.out.println(str);
        }
        fis.close();
    }

FileOutputStream:位元組流寫入硬碟

    FileOutputStream fos=new FileOutputStream("E:\\1.txt");
        String word="高考是人生的分水嶺";
        byte[] bytes = word.getBytes();
        fos.write(bytes);
        fos.close();
        System.out.println("寫入成功!");        
    }
}

第二套:字元流讀取寫入方案

FileReader:字元流讀取文字

    FileReader fr=new FileReader("E:\\讀取檔案.txt");
    char[]chars=new char[1024];
    int data;
    while((data=fr.read(chars))!=-1)
    {        
        String str=new String(chars);
        System.out.println(str);
    }
}

FileWriter:字元流寫入文字

FileWriter fw=new FileWriter("E:\\2.txt");
        
        fw.write("新的6月");
        
        System.out.println("寫入成功!");

        fw.close();
    }
    ```

第三套:<BufferedReader、BufferedWriter>一般和FileReader和FileWriter結合使用

BufferedReader:自定義快取大小,讀取文字 8192個char
FileReader fr=new FileReader("E:\\讀取檔案.txt");
BufferedReader br=new BufferedReader(fr);
String line;
while((line=br.readLine())!=null)
{
    System.out.println(line);
}
br.close();
fr.close();

}

 

BufferedWriter:寫入文字
FileWriter fw=new FileWriter("E:\\5.txt");
    BufferedWriter bw=new BufferedWriter(fw);
    bw.write("OK!!");
    
    bw.close();
    fw.close();
    
    System.out.println("寫入成功!!");
}
第四套:可以讀取二進位制(img圖片等 )

DataInputStream:將本地的img載入到記憶體中

FileInputStream fis=new FileInputStream(“E:\5.txt”);
FileOutputStream fos=new FileOutputStream(“D:\55.txt”);

    DataInputStream dis=new DataInputStream(fis);
    DataOutputStream dos=null;
    
    byte[]bytes=new byte[1024];
    
    int data;
    
    while((data=dis.read(bytes))!=-1)
    {
        dos=new DataOutputStream(fos);
        dos.write(bytes);
    }
    
    dos.close();
    dis.close();
    fos.close();
    fis.close();
    
    System.out.println("copy succes!!!");
}
```

DataOutputStream:將記憶體中的二進位制資料寫入到硬碟上的某個檔案中

    DataOutputStream out=null;
        DataInputStream dis=null;
        try {
            //建立輸入流物件
            FileInputStream fis=new FileInputStream("c:\\範寧.jpg");
            dis=new DataInputStream(fis);
            //建立輸出流物件
            FileOutputStream outFile=new FileOutputStream("c:\\範寧小美女33.jpg");
            out=new DataOutputStream(outFile);
            int temp=dis.read();
            while (temp!=-1) {
                out.write(temp);
                temp=dis.read();
            }
            System.out.println("複製成功");
            fis.close();
            outFile.close();
        } catch (Exception e) {
            System.out.println("檔案不存在");
        }finally{
            try {
                if (dis!=null) {
                    dis.close();
                }
                if (out!=null) {
                    out.close();
                }
            } catch (Exception e2) {
                e2.printStackTrace();
            }
        }

注:在java中,byte陣列和String字串如何轉換?

1、string 轉 byte[]

String str = "Hello";
byte[] srtbyte = str.getBytes();
2、byte[] 轉 string

byte[] srtbyte;
String str = new String(srtbyte);
System.out.println(str);