读取txt单行内容

来源:互联网 发布:有哪些类似于知乎 编辑:程序博客网 时间:2024/04/30 13:47
/**
* 读取web项目txt里的单行内容
* @param fileP 文件名称
*/
public static String readTxtFile(String fileP) {
try {

String filePath = String.valueOf(Thread.currentThread().getContextClassLoader().getResource(""));//项目路径
filePath = filePath.replaceAll("file:/", "");
filePath = filePath.replaceAll("%20", " ");
filePath = filePath.trim() + fileP.trim();
if(filePath.indexOf(":") != 1){
filePath = File.separator + filePath;
}
String encoding = "utf-8";
File file = new File(filePath);
if (file.isFile() && file.exists()) { // 判断文件是否存在
InputStreamReader read = new InputStreamReader(
new FileInputStream(file), encoding);// 考虑到编码格式
BufferedReader bufferedReader = new BufferedReader(read);
String lineTxt = null;
while ((lineTxt = bufferedReader.readLine()) != null) {
System.out.println(lineTxt);
return lineTxt;
}
read.close();
}else{
System.out.println("找不到指定的文件,查看此路径是否正确:"+filePath);
}
} catch (Exception e) {
System.out.println("读取文件内容出错");
}
return "";
}
原创粉丝点击