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"));
}
}
Java读取txt文件内容
最新推荐文章于 2024-05-13 09:40:30 发布