请教如果知道一个string类型的文件名(没有后缀)如何读取文件? 比如在目录的文件夹下有个文件haha.txt,现在我通过计算知道string haha了,如何根据这个string来读取文件呢? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 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(); } path 为 haha.txt 的绝对路径 如果 haha.txt 在 c:\mytest 下绝对路径表示为: path="c:\\mytest\\haha.txt"; 问题是你如何确定当前目录下没有叫"haha.jpg"或者其它后缀的?这个能解决就不会有问题.否则你必须指定后缀名来确认具体哪个文件. 很简单的程序,如果写测试案例,你会怎样写 请教高手!读TXT文档。 java实现http代理服务get和post如何实现??? 求救:java程序打包后无法正常运行 怎样知道一个类的ClassLoader是谁? 如何用JAVA实现WINDOWS 下的MS-DOS命令 Eclipse的plug-in 那里可以下载到中文语音输入软件(如 ibm的语音输入系统)!!! 用JAVA写了程序(application)之后,要在网上用,总不能让人家都装JDK吧,有什么好的办法 如何控制JTable中单元的值?如:只允许输入大于0的整数 大家说说最后输出的是什么。。(简单) 搞糊涂了
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();
}
如果 haha.txt 在 c:\mytest 下
绝对路径表示为: path="c:\\mytest\\haha.txt";
否则你必须指定后缀名来确认具体哪个文件.