本文实例为大家分享了java读取txt文件的具体代码,供大家参考,具体内容如下
/**
* @描述
* 读取txt中的内容
*/
public class ReadTxt {
public static String[] openTextFile(String path){
File source = new File(path);
System.out.println(path);
LinkedList<String> arr = new LinkedList<String>();
BufferedReader br = null;
try{
br = new BufferedReader(
new FileReader(source));
String info = null;
while((info = br.readLine()) != null){
arr.add(info);
}
}catch(FileNotFoundException e){
System.err.println("文件未找到");
}catch(IOException e){
System.err.println("文件读取出错");
}catch(Exception e){
System.err.println(e.getMessage());
}finally{
try{
br.close();
}catch(IOException e){
System.err.println(e.getMessage());
}
}
String[] results = new String[arr.size()];
arr.toArray(results);
return results;
// return stringHashMap;
}
// public static void main(String[] args) {
// ReadTxt readTxt = new ReadTxt();
// Map<String, String> map = readTxt.openTextFile("D:\\aaa.*.txt");
// for (String s : map.keySet()) {
// System.out.println(s+":"+map.get(s));
// }
// }
}