62,628
社区成员
发帖
与我相关
我的任务
分享



/**
* 读取文件中所有的数据
* @param path 文件路径
* @return 返回 ArrayList<String> 变量
* @exception 如果文件路径非法, 返回null
*/
public static ArrayList<StringBuffer> readData(StringBuffer path) {
// 检测文件是否存在
if (FindFile.isExists(path) == false) {
return null;
}
ArrayList<StringBuffer> data = new ArrayList<StringBuffer>(); /*储存读取的文件数据*/
File file = new File(path.toString());
BufferedReader fr = null;
try {
fr = new BufferedReader(new FileReader(file));
}
catch (FileNotFoundException e) {
e.printStackTrace();
return null;
}
String line = "";
for (;;) {
try {
line = fr.readLine(); //读取一行数据
}
catch (IOException e) {
e.printStackTrace();
}
if (line == null) {
break; //结束读取
}
else {
data.add(new StringBuffer(line)); //记录到data变量里
}
}
try {
fr.close(); //关闭文件
}
catch (IOException e) {
e.printStackTrace();
}
return data;
}[/quote]
19行改为new String(Files.readAllBytes(file.toPath), StandardCharsets.UTF_8)直接就可以读取, 或者改成fr = new BufferedReader(new InputStreamReader(new FileInputStream(file), StandardCharsets.UTF_8))[/quote]
真的成功了!感谢! /**
* 读取文件中所有的数据
* @param path 文件路径
* @return 返回 ArrayList<String> 变量
* @exception 如果文件路径非法, 返回null
*/
public static ArrayList<StringBuffer> readData(StringBuffer path) {
// 检测文件是否存在
if (FindFile.isExists(path) == false) {
return null;
}
ArrayList<StringBuffer> data = new ArrayList<StringBuffer>(); /*储存读取的文件数据*/
File file = new File(path.toString());
BufferedReader fr = null;
try {
fr = new BufferedReader(new FileReader(file));
}
catch (FileNotFoundException e) {
e.printStackTrace();
return null;
}
String line = "";
for (;;) {
try {
line = fr.readLine(); //读取一行数据
}
catch (IOException e) {
e.printStackTrace();
}
if (line == null) {
break; //结束读取
}
else {
data.add(new StringBuffer(line)); //记录到data变量里
}
}
try {
fr.close(); //关闭文件
}
catch (IOException e) {
e.printStackTrace();
}
return data;
}[/quote]
19行改为new String(Files.readAllBytes(file.toPath), StandardCharsets.UTF_8)直接就可以读取, 或者改成fr = new BufferedReader(new InputStreamReader(new FileInputStream(file), StandardCharsets.UTF_8)) /**
* 读取文件中所有的数据
* @param path 文件路径
* @return 返回 ArrayList<String> 变量
* @exception 如果文件路径非法, 返回null
*/
public static ArrayList<StringBuffer> readData(StringBuffer path) {
// 检测文件是否存在
if (FindFile.isExists(path) == false) {
return null;
}
ArrayList<StringBuffer> data = new ArrayList<StringBuffer>(); /*储存读取的文件数据*/
File file = new File(path.toString());
BufferedReader fr = null;
try {
fr = new BufferedReader(new FileReader(file));
}
catch (FileNotFoundException e) {
e.printStackTrace();
return null;
}
String line = "";
for (;;) {
try {
line = fr.readLine(); //读取一行数据
}
catch (IOException e) {
e.printStackTrace();
}
if (line == null) {
break; //结束读取
}
else {
data.add(new StringBuffer(line)); //记录到data变量里
}
}
try {
fr.close(); //关闭文件
}
catch (IOException e) {
e.printStackTrace();
}
return data;
}
这是我UC浏览器的历史记录界面
我把这个页面保存下来
在eclipse中,打印结果是正确的
但在控制台下,就成了乱码
然后我把文件的编码改成了ANSI
打印结果就正确了
public class Test {
public static void main(String[] args) throws Exception {
StringBuffer path = Conver.nsb("c:/下载/历史记录.html");
StringBuffer fileName = Conver.nsb("c:/下载/UC浏览器历史记录.txt");
// 如果html文件存在, 更改文件名
if (FindFile.isExists(path) == true) {
FilePath.rename(path, fileName);
}
else {
// 如果txt文件也不存在, 输出错误
if (FindFile.isExists(fileName) == false) {
System.out.println("\n文件名更改失败!\n");
System.exit(1);
}
}
// 将路径保存在path里
path = fileName;
fileName = null;
// 读取文件
ArrayList<StringBuffer> fileData = ReadData.readData(path);
if (fileData == null) {
System.out.println("\n读取失败!\n");
System.exit(1);
}
ArrayList<StringBuffer> temp = null;
for (int i = 0;i < fileData.size();++i) {
// 截取 title=" 到 "> 之间的字符串
temp = SubStr.separateBetweenStr(fileData.get(i), Conver.nsb("title=\""), Conver.nsb("\">"));
if (temp == null) {
continue;
}
for (int j = 0;j < temp.size();++j) {
// 如果引用为null, 或者是 "删除" 和 "搜索相关的记录" 则过滤, 不显示
if (temp.get(j) == null || ComStr.compare(temp.get(j), "删除") == true || ComStr.compare(temp.get(j), "搜索相关的记录") == true) {
continue;
}
// 显示字符串
System.out.println(temp.get(j));
}
}
}
}