字節流
實例5:字節流的寫入
- import java.io.File;
- import java.io.FileOutputStream;
- import java.io.IOException;
- public class Demo {
- public static void main(String[] args ) {
- String path = File.separator + "home" + File.separator + "siu" +
- File.separator + "work" + File.separator + "demo.txt";
- FileOutputStream o = null;
- try {
- o = new FileOutputStream(path);
- String str = "Nerxious is a good boy\r\n";
- byte[] buf = str.getBytes();
- //也可以直接使用o.write("String".getBytes());
- //因爲字符串就是一個對象,能直接調用方法
- o.write(buf);
- } catch (IOException e) {
- e.printStackTrace();
- } finally {
- if(o != null) {
- try {
- o.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
- }
- }
編譯之後產生的文件,以上在字符串中加\r\n就是爲了便於終端顯示
其實在linux下面換行僅用\n即可
實例6:字節流的讀取
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.IOException;
- public class Demo {
- public static void main(String[] args ) {
- String path = File.separator + "home" + File.separator + "siu" +
- File.separator + "work" + File.separator + "demo.txt";
- FileInputStream i = null;
- try {
- i = new FileInputStream(path);
- //方式一:單個字符讀取
- //需要注意的是,此處我用英文文本測試效果良好
- //但中文就悲劇了,不過下面兩個方法效果良好
- int ch = 0;
- while((ch=i.read()) != -1){
- System.out.print((char)ch);
- }
- //方式二:數組循環讀取
- /*
- byte[] buf = new byte[1024];
- int len = 0;
- while((len = i.read(buf)) != -1) {
- System.out.println(new String(buf,0,len));
- }
- */
- //方式三:標準大小的數組讀取
- /*
- //定一個一個剛好大小的數組
- //available()方法返回文件的字節數
- //但是,如果文件過大,內存溢出,那就悲劇了
- //所以,親們要慎用!!!上面那個方法就不錯
- byte[] buf = new byte[i.available()];
- i.read(buf);
- //因爲數組大小剛好,所以轉換爲字符串時無需在構造函數中設置起始點
- System.out.println(new String(buf));
- */
- } catch (IOException e) {
- e.printStackTrace();
- } finally {
- if(i != null) {
- try {
- i.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
- }
- }
讀取文件到終端
實例7:二進制文件的複製
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.FileOutputStream;
- import java.io.IOException;
- public class Demo {
- public static void main(String[] args ) {
- String bin = File.separator + "home" + File.separator + "siu" +
- File.separator + "work" + File.separator + "一個人生活.mp3";
- String copy = File.separator + "home" + File.separator + "siu" +
- File.separator + "life" + File.separator + "一個人生活.mp3";
- FileInputStream i = null;
- FileOutputStream o = null;
- try {
- i = new FileInputStream(bin);
- o = new FileOutputStream(copy);
- //循環的方式讀入寫出文件,從而完成複製
- byte[] buf = new byte[1024];
- int temp = 0;
- while((temp = i.read(buf)) != -1) {
- o.write(buf, 0, temp);
- }
- } catch (IOException e) {
- e.printStackTrace();
- } finally {
- if(i != null) {
- try {
- i.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- if(o != null) {
- try {
- o.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
- }
- }
複製效果,如圖:
實例8:利用字節流的緩衝區進行二進制文件的複製
- import java.io.BufferedInputStream;
- import java.io.BufferedOutputStream;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.FileOutputStream;
- import java.io.IOException;
- public class Demo {
- public static void main(String[] args ) {
- String bin = File.separator + "home" + File.separator + "siu" +
- File.separator + "work" + File.separator + "一個人生活.mp3";
- String copy = File.separator + "home" + File.separator + "siu" +
- File.separator + "life" + File.separator + "一個人生活.mp3";
- FileInputStream i = null;
- FileOutputStream o = null;
- BufferedInputStream bi = null;
- BufferedOutputStream bo = null;
- try {
- i = new FileInputStream(bin);
- o = new FileOutputStream(copy);
- bi = new BufferedInputStream(i);
- bo = new BufferedOutputStream(o);
- byte[] buf = new byte[1024];
- int temp = 0;
- while((temp = bi.read(buf)) != -1) {
- bo.write(buf,0,temp);
- }
- } catch (IOException e) {
- e.printStackTrace();
- } finally {
- if(bi != null) {
- try {
- i.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- if(bo != null) {
- try {
- o.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
- }
- }
兩個目錄都有 “一個人生活.mp3”文件,順便說一下,這歌挺好聽的
初學者在學會使用字符流和字節流之後未免會產生疑問:什麼時候該使用字符流,什麼時候又該使用字節流呢?
其實仔細想想就應該知道,所謂字符流,肯定是用於操作類似文本文件或者帶有字符文件的場合比較多
而字節流則是操作那些無法直接獲取文本信息的二進制文件,比如圖片,mp3,視頻文件等
說白了在硬盤上都是以字節存儲的,只不過字符流在操作文本上面更方便一點而已
此外,爲什麼要利用緩衝區呢?
我們知道,像迅雷等下載軟件都有個緩存的功能,硬盤本身也有緩衝區
試想一下,如果一有數據,不論大小就開始讀寫,勢必會給硬盤造成很大負擔,它會感覺很不爽
人不也一樣,一頓飯不讓你一次吃完,每分鐘喂一勺,你怎麼想?
因此,採用緩衝區能夠在讀寫大文件的時候有效提高效率