需求是:取出D:盘下全部的.java文件的文件路径保存在java.txt文件中
下面是我自己写的,但是只写出了一个文件,苦思冥想没有找到原因,请大家有空帮忙瞅瞅。- public static void main(String[] args) {
- File dir = new File("E:\\MyEclipse\\heimaStudy");
- File file = new File("C:\\java.txt");
- getJavaFile(dir, file);
- }
- public static void getJavaFile(File dir, File file1) {
- BufferedWriter bfw = null;
- try {
- bfw = new BufferedWriter(new FileWriter(file1));
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- File files[] = dir.listFiles();
- for (int i = 0; i < files.length; i++) {
- if (files[i].isDirectory())
- getJavaFile(files[i], file1);
- else {
- String file = files[i].getName();
- if (file.endsWith(".java")) {
- try {
- bfw.write(files[i].getAbsolutePath());
- bfw.newLine();
- bfw.flush();
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- }
- }
- try {
- bfw.close();
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- }
复制代码 |
|