Java讀取txt文件內容

package com.achk.utils.common;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;

/**
 * 讀取txt文件內容
 * @author wangzewen
 *
 */
public class ReadTxt {
    
    /**
     * 讀取txt文件的內容
     * @param fileName 想要讀取的文件對象
     * @return 返回文件內容
     */
	public static String read(String fileName) {
		String fileContent = "";
		try {
			File f = new File(fileName);
			if (f.isFile() && f.exists()) {
				InputStreamReader read = new InputStreamReader(
						new FileInputStream(f), "gbk");
				BufferedReader reader = new BufferedReader(read);
				String line;
				while ((line = reader.readLine()) != null) {
					fileContent += line;
				}
				read.close();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return fileContent;
	}
	
    public static void main(String[] args){
        System.out.println(read("E:/achk_project/trainSys/apache-tomcat-8.0.47/webapps/course/Java2018/線程/課程介紹.txt"));
    }
	
}

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