当前位置: 代码迷 >> J2SE >> 请问如果知道一个string类型的文件名(没有后缀)怎么读取文件
  详细解决方案

请问如果知道一个string类型的文件名(没有后缀)怎么读取文件

热度:173   发布时间:2016-04-24 15:24:26.0
请教如果知道一个string类型的文件名(没有后缀)如何读取文件?
比如在目录的文件夹下有个文件haha.txt,现在我通过计算知道string   haha了,如何根据这个string来读取文件呢?

------解决方案--------------------
try {

FileReader fr = new FileReader(path); //path 为haha.txt的绝路径
BufferedReader br = new BufferedReader(fr);
/*逐行读取*/
String line = br.readLine();
while (line != null) {
System.out.println(line);
line = br.readLine();
}
br.close();
fr.close();
} catch (FileNotFoundException ex) {
ex.printStackTrace();
} catch (IOException ex) {
ex.printStackTrace();
}
  相关解决方案