java利用io流讀取txt文件

java利用io流讀取txt文件:
寫入txt文件(覆蓋原有、不覆蓋原有、創建文件夾)請下載資源
http://download.csdn.net/detail/lxm_csdn/9551203
以下爲源代碼
package com.gentlesoft.file;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;

public class FileTxtUtil {

/**
 * 讀取txt文件
 * @param path
 * @return
 */
public static String readtxt(String path){
    String result = "";
    File file = new File(path);
    try {
        InputStreamReader reader = new InputStreamReader(new FileInputStream(file),"gbk");
        BufferedReader br = new BufferedReader(reader);
        String s = null;
        while((s=br.readLine())!=null){
            result = result  + s;
        }
    } catch (UnsupportedEncodingException | FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    /*try {
        BufferedReader br = new BufferedReader(new FileReader(new File(path)));
        String s = null;
        while((s=br.readLine())!=null){
            result = result + "\n" + s;
        }
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }*/
    return result;
}

public static void main(String[] args) {
    String str = FileTxtUtil.readtxt("D://qqq.txt");
    System.out.println(str);
}

}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章