close

讀取Read的各種方法:

1. 使用 FileInputStream 類別:

(1)使用建構式: FileInputStream (String name)

FileInputStream 01.jpg

-------------------------------------------------------------------------------------------------------------

(2)使用 FileInputStream 的 available() 方法取得檔案位元組的數量

        因為FileInputStream 的 read方法,參數的資料型別是 byte[],

        在建立 byte[]時,需要設定陣列的數量。

        可以用以下的方式來達成:

        FileInputStream fis = new FileInputStream(fileName); 

        byte[] buffer=new byte[fis.available()];    

FileInputStream 02.jpg

-------------------------------------------------------------------------------------------------------------

(3)使用FileInputStream 的 read ( byte[] b ) 方法來讀取資料:

        //用while迴圈讀取檔案,
        //若傳回
-1,表示已檔案結尾。
        while(fis.read(buffer)!=-1){
            
            //byte[]型態的buffer轉為String
            String temp=new String(buffer);
            
            //將每筆讀出的資料都存給strData
            strData+=temp;
        }

FileInputStream 03.jpg

FileInputStream 04.jpg

-------------------------------------------------------------------------------------------------------------

(4)程式碼範例:

    public static void main(String[] args) {

        String fileName="ptt.txt";    //要讀取的檔案名稱
        String showData=""; //用來存讀取到的資料
        
        try {
            //自訂方法:讀取資料
            showData = readData(fileName);
            System.out.println(showData);
            
        } catch (IOException e) {
            e.printStackTrace();
        }
        
    }//main() end
    /*************************************************************************/
    //取得檔案位元祖的數量
    private static int getFileByteSum(String fileName)
                                    throws IOException{        
        int totalBytes=0;    //記錄檔案有多少位元組
        
        //宣告並建立 FileInputStream 物件
        FileInputStream fis = new FileInputStream(fileName);        
        
        //FileInputStream的available方法,
        //可以估算檔案的位元組數量,傳回值int

        totalBytes =fis.available();
        
        //最後要記得close
        fis.close();
        
        return totalBytes;        
    }    
    /*************************************************************************/
    //讀取檔案
    private static String readData(String fileName) 
                                throws IOException{
        
        //用來裝讀出的資料的String變數
        String strData=""; 
        
        //用自訂方法取得位元組的數量
        int totalBytes=getFileByteSum(fileName);    
        
        //要暫放檔案的位元組資料
        //FileInputStream 的 read方法,
        //參數的資料型別是 byte[]

        byte[] buffer=new byte[totalBytes];    
        
        //宣告並建立 FileInputStream 物件
        FileInputStream fis = new FileInputStream(fileName); 
        
        //用while迴圈讀取檔案,
        //若傳回-1,表示已檔案結尾。

        while(fis.read(buffer)!=-1){
            
            //byte[]型態的buffer轉為String
            String temp=new String(buffer);
            
            //將每筆讀出的資料都存給strData
            strData+=temp;
        }
        //最後要記得close
        fis.close();
        
        return strData;        
    }

================================================================

2. 使用 FileReader 類別:

(1)使用建構式: FileReader(String fileName)

FileReader01.jpg

-------------------------------------------------------------------------------------------------------------

(2)使用FileReader 繼承自Reader類別的 read(char[] cbuf) 方法來讀取資料:

        因為FileReader本身沒有方法,

       所以這裡用的 read 方法是繼承自上上類別 Reader 的方法,

       使用的參數資料型別是 char[]

FileReader02.jpg

FileReader03.jpg

-------------------------------------------------------------------------------------------------------------

(3)程式碼範例:

    public static void main(String[] args) {        
        
        String fileName="ptt.txt"; //指定檔名
        String showData=""; //用來存讀取到的資料
        try {        
            //自訂方法:讀取資料
            showData = readData(fileName); 
            System.out.println(showData);
            
        } catch (IOException e) {
            e.printStackTrace();
        }

    }//main() end
    /*************************************************************/
    //自訂方法:讀取資料
    private static String readData(String fileName) 
                                    throws IOException{
        
        //用來存放讀出的資料的String變數
        String strData=""; 
        
        //宣告一個資料buffer
        //FileReader本身沒有方法,
        //這裡用的read是繼承自上上類別Reader的方法
        //使用的參數是char[]陣列

        char[] buffer= new char[1];
        
        //宣告並建立FileReader
        FileReader fr = new FileReader(fileName);
        
        //用while迴圈讀取資料
        //讀完時會傳回-1

        while(fr.read(buffer)!=-1){ 
            
            //char[]型態的buffer轉為String
            String temp = new String(buffer);
            
            //將每筆讀出的資料都存給strData
            strData+=temp;
        }        
        fr.close(); //用完記得close
        
        return strData;        
    }

================================================================

3.  使用 FileInputStream InputStreamReader BufferedReader

(1)說明:

BufferedReader 的 readLine 方法可以一次讀取一行字串,資料讀完時會傳回null

        //用while迴圈讀取資料
        //資料讀完時會傳回null

        String temp="";
        while((temp=br.readLine())!=null){
            strData+=temp+"\n";
        }

-------------------------------------------------------------------------------------------------------------

(2)程式碼範例:

    public static void main(String[] args) {
        
        String fileName="ptt.txt";    //指定檔名
        String showData=""; //用來存讀取到的資料
        
        //使用自訂方法
        if(isFileExists(fileName)){ //如果檔案存在
            try {
                //讀取資料
                showData = readData(fileName); 
                System.out.println(showData);
                
            } catch (IOException e) {    
                e.printStackTrace();
            }
        }

    }//main() end
    /*********************************************************/    
    //自訂方法:判斷檔案是否存在
    private static boolean isFileExists(String fileName){
        
        //布林變數:檔案是否已存在
        boolean fileExists=false; 
        
        //建立File物件
        File file=new File(fileName);
        
        //判斷檔案是否存在
        if(file.exists()) fileExists=true;
        else fileExists=false;
        
        return fileExists;        
    }
    /*********************************************************/    
    //自訂方法:讀取資料
    private static String readData(String fileName) 
                                    throws IOException{        
        //用來存放讀取的資料的String變數
        String strData=""; 
        
        //建立File物件
        File file=new File(fileName);
        
        //接水管
        FileInputStream fis = new FileInputStream(file);
        
        //裝馬達,解碼,將byte stream 變成 characters  stream
        InputStreamReader isr = new InputStreamReader(fis);
        
        //接水桶
        BufferedReader br = new BufferedReader(isr);        
        
        //用while迴圈讀取資料
        //資料讀完時會傳回null

        String temp="";
        while((temp=br.readLine())!=null){
            strData+=temp+"\n";
        }
        
        //要注意close的順序
        br.close();         //拿走水桶
        isr.close();    //關馬達
        fis.close();    //抽水管        
                                        
        return strData;
    }

================================================================

【參考資料】:JAVA 官方文檔

arrow
arrow
    全站熱搜
    創作者介紹
    創作者 蕭小牛 的頭像
    蕭小牛

    蕭小牛的部落格

    蕭小牛 發表在 痞客邦 留言(0) 人氣()