读取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 "";
}
* 读取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 "";
}
阅读全文
0 0
- 读取txt单行内容
- 读取txt中的内容!!!
- 读取txt文件内容
- 从TXT读取内容
- AJAX 读取TXT内容
- 读取txt文件内容
- 读取txt文件内容
- Java读取TXT内容
- 从txt中读取内容
- c# 读取txt文件内容
- asp 读取txt文件内容
- ajax读取txt文本内容
- ajax读取txt文本内容
- C# 读取Txt文件内容
- 如何读取txt文件内容
- 1.读取TXT文本内容
- Python教程:读取txt内容
- java读取txt文件内容
- sublime个人配置
- 方法的重载
- Android InputMethodManager 导致的内存泄露及解决方案
- PHP与memcache数据交互
- 1 ubuntu server 14.04 LTS环境配置
- 读取txt单行内容
- OpenGL-渲染光照球体
- [leetcode]553. Optimal Division
- python数据挖掘与入门实践(2.2)用sciket-learn估计器分类
- AMS-启动Activity之二
- 谁能举个通俗易懂的例子告诉我IAAS,SAAS,PAAS的区别?
- 电路基础-1-电路模型和电路定律
- 使用u盘量产工具修复写保护的u盘
- 第七章 Java中的13个原子操作类(atomic包)