Java讀寫txt文件

一、Java讀取txt文件

1.一、使用FileInputStream:

public static String readFile(File file, String charset){
        //設置默認編碼
        if(charset == null){
            charset = "UTF-8";
        }
        
        if(file.isFile() && file.exists()){
            try {
                FileInputStream fileInputStream = new FileInputStream(file);
                InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream, charset);
                BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
                
                StringBuffer sb = new StringBuffer();
                String text = null;
                while((text = bufferedReader.readLine()) != null){
                    sb.append(text);
                }
                return sb.toString();
            } catch (Exception e) {
                // TODO: handle exception
            }
        }
        return null;
    }

最後此函數將會返回讀取到的內容。固然,也能夠在讀取的過程當中進行逐行處理,沒必要要一次性讀出再進行處理。java

並且,bufferedReader還有read方法,知足更多的須要。下去我本身能夠試一試,再補充。app


 

二、Java寫入txt文件

2.一、使用FileWriter方式:

/**
 * 以FileWriter方式寫入txt文件。
 * @param File file:要寫入的文件
 * @param String content: 要寫入的內容
 * @param String charset:要寫入內容的編碼方式
 */
public static void writeToFile1(){
    
    try {
        String content = "測試使用字符串";
        File file = new File("./File/test1.txt");
        if(file.exists()){
            FileWriter fw = new FileWriter(file,false);
            BufferedWriter bw = new BufferedWriter(fw);
            bw.write(content);
            bw.close(); fw.close();
            System.out.println("test1 done!");
        }
        
    } catch (Exception e) {
        // TODO: handle exception
    }
}

這種方式簡單方便,代碼簡單。順帶一提的是:上述代碼是清空文件重寫,要想追加寫入,則將FileWriter構造函數中第二個參數變爲true。函數

 

2.二、文件不存在時候,主動建立文件。

public static void writeToFile2(){    
    try {
        String content = "測試使用字符串";
        File file = new File("./File/test2.txt");
        //文件不存在時候,主動穿件文件。
        if(!file.exists()){
            file.createNewFile();
        }
        FileWriter fw = new FileWriter(file,false);
        BufferedWriter bw = new BufferedWriter(fw);
        bw.write(content);
        bw.close(); fw.close();
        System.out.println("test2 done!");
        
    } catch (Exception e) {
        // TODO: handle exception
    }
}

 關鍵性的話語在於file.createNewFile();學習

2.三、使用FileOutputStream來寫入txt文件。

public static void writeToFile3(){    
   String content = "測試使用字符串";
   FileOutputStream fileOutputStream = null;
   File file = new File("./File/test3.txt");
   
   try {
       if(file.exists()){
           file.createNewFile();
       }
       
       fileOutputStream = new FileOutputStream(file);
       fileOutputStream.write(content.getBytes());
       fileOutputStream.flush();
       fileOutputStream.close();
   } catch (Exception e) {
       // TODO: handle exception
   }
   System.out.println("test3 done");

}

使用輸出流的方式寫入文件,要將txt文本轉換爲bytes寫入。測試


三、總結

一、對於寫入文件路徑的問題,我發現文件路徑是以本項目爲根目錄的,也就是說:寫文件路徑的時候,要麼寫成絕對路徑,防止錯誤;要麼 以本項目爲根目錄寫相對路徑。編碼

舉一個例子:spa

|project/字符串

|----src/get

|       |----test.javainput

|----File

|       |----test1.txt

|       |----test2.txt

|       |----test3.txt

要想訪問到test1.txt,路徑要寫做絕對路徑,或者相對路徑:./File/test1.txt

二、對於輸入輸出流讀寫文件,操做完成後要關閉流。

 

 

之後再進行補充學習,暫時就這樣記錄下來。

相關文章
相關標籤/搜索