【发布时间】:2015-03-03 09:29:06
【问题描述】:
public class FIlesInAFolder {
private static BufferedReader br;
public static void main(String[] args) throws IOException {
File folder = new File("C:/filesexamplefolder");
FileReader fr = null;
if (folder.isDirectory()) {
for (File fileEntry : folder.listFiles()) {
if (fileEntry.isFile()) {
try {
fr = new FileReader(folder.getAbsolutePath() + "\\" + fileEntry.getName());
br = new BufferedReader(fr);
System.out.println(""+br.readLine());
}
catch (FileNotFoundException e) {
e.printStackTrace();
}
finally {
br.close();
fr.close();
}
}
}
}
}
}
如何打印目录的第一个文件的第一个单词,第二个文件的第二个单词和同一目录的第三个文件的第三个单词。
i am able to open directory and print the line from each file of the directory,
but tell me how to print the first word from first file and second word from second file and so on . .
【问题讨论】:
-
从一个目录中读取数据并不容易...
-
为每个文件创建不同的流
-
你有新的 FileReader(folder.getAbsolutePath()+"\\"+fileEntry.getName()),新的 FIleReader(fileEntry) 会这样做
-
也完整阅读了邮件正文
标签: java