IO文件的讀取,以及寫入文件內容


package zxc;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class IO {
    
    public static void main(String[] args) {
        IO  a = new IO();
        //設置讀取路徑
        String filePath = "F:/abc.txt";
        //調用讀取方法
        String input = a.readeFile(filePath);
        //打印abc.txt文件的內容
        System.out.println(input);
        
        //寫入到文件里的內容
        String content = "今天2018/03/20,星期二";
        //調用寫入方法
        a.writeFile(filePath,content);
        
        //在修改文件內容后,再調用讀取方法
        String b = a.readeFile(filePath);
        ////打印修改后的abc.txt文件的內容
        System.out.println(b);
        
    }
    /**
     * FileInputStream類的使用:讀取文件內容
     * @param filePath
     * @return
     */
    private String readeFile(String filePath) {
        FileInputStream input = null;
        String result = "";
        try {
            //1.根據path實例化一個輸入流的對象
            input = new FileInputStream(filePath);
            //2.返回這個輸入流中可以被讀的剩下的bytes字節的估計值;
            int size = input.available();
            //3.根據輸入流的字節創建一個byte數組
            byte[] array = new byte[size];
            //4.把數據讀取到byte數組中
            input.read(array);
            //5.根據獲取的byte數組新建一個字符串,然后輸出
            result = new String(array);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if(input != null){
                try {
                    //關閉
                    input.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return result;
    }
    /**
     * FileOutputStream類的使用:內容寫入到文件中
     * @param filePath
     * @return
     */
    private void writeFile(String filePath,String content) {
        FileOutputStream out = null;
        try {
            //1.根據路徑創建輸出流對象
            out = new FileOutputStream(filePath) ;
            //2.把String字符串轉換成byte數組;
            byte[] b = content.getBytes(); 
            //3.把byte數組輸出
            out.write(b);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if(out != null) {
                try {
                    out.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

F:/abc.txt 文件修改前的內容

執行后,控制台打印的內容

F:/abc.txt 文件修改后的內容

注意:

  1. 在實際的項目中,所有的IO操作都應該放到子線程中操作,避免堵住主線程。
  2. FileInputStream在讀取文件內容的時候,我們傳入文件的路徑("F:/abcz.txt"), 如果這個路徑下的文件不存在,那么在執行readFile()方法時會報FileNotFoundException異常。
  3. FileOutputStream在寫入文件的時候,我們傳入文件的路徑("F:/abcz.txt"), 如果這個路徑下的文件不存在,那么在執行writeFile()方法時, 會默認給我們創建一個新的文件。還有重要的一點,不會報異常。

 

執行之后:

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM