A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 谢冬 中级黑马   /  2013-3-10 17:44  /  1619 人查看  /  7 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;

/**
* 需求:把i盘目录下的所有.java结尾的文件路径存储到一个文件中,方便查找
* @author xiedong
*
*/
public class ShowPath {
        public static void main(String[] args) throws IOException {
                //封装数据
                File file1 = new File("I://");
                //创建集合,保存数据
                ArrayList<File> al = new ArrayList<File>();
                getJavaFile(file1, al);
                //封装目的地
                File file2 = new File("i://codepath.txt");
                getJavaFilePath(al, file2);
        }

        /**
         * 写入数据
         * @param al
         * @param file2
         * @throws IOException
         */
        private static void getJavaFilePath(ArrayList<File> al, File file2) throws IOException {
                // TODO Auto-generated method stub
                BufferedWriter bw = new BufferedWriter(new FileWriter(file2));
                for(File fileItem : al)
                {
                        bw.write(fileItem.getAbsolutePath());
                        bw.newLine();
                        bw.flush();
                }

                bw.close();
        }

        /**
         * 获取java文件的方法
         * @param file
         * @param al
         * @throws IOException
         */
        private static void getJavaFile(File file, ArrayList<File> al) throws IOException {
                File[] files = file.listFiles();
                for(File fileItem : files)
                {
                        if(fileItem.isDirectory())
                        {
                                getJavaFile(fileItem, al);
                        }else
                        {
                                if(file.getName().endsWith(".java"))
                                {
                                        //将数据存入集合
                                        al.add(fileItem);
                                }

                        }
                }

        }
}
上面程序无法将i盘下面所有的java文件路径,写入知道位置,这是错在了哪里?

评分

参与人数 1技术分 +1 收起 理由
陈丽莉 + 1

查看全部评分

7 个回复

倒序浏览
如果要直接访问盘符下文件 for()循环下要判断下 {   }
  1.   for(File fileItem : files)
  2.                 {
  3.                      if (fileItem != null) {   
  4.                         if(fileItem.isDirectory())
  5.                         {
  6.                                 getJavaFile(fileItem, al);
  7.                         }else
  8.                         {
  9.                                 if(file.getName().endsWith(".java"))
  10.                                 {
  11.                                         //将数据存入集合
  12.                                         al.add(fileItem);
  13.                                 }
  14.                         }
  15.                         }
  16.                 }
复制代码

评分

参与人数 1技术分 +1 收起 理由
陈丽莉 + 1

查看全部评分

回复 使用道具 举报
public class ShowPath {
        public static void main(String[] args) throws IOException {
                //封装数据
                File file1 = new File("I://");//路径格式错误
                //创建集合,保存数据
                ArrayList<File> al = new ArrayList<File>();
                getJavaFile(file1, al);
                //封装目的地
                File file2 = new File("i://codepath.txt");//路径格式错误

                getJavaFilePath(al, file2);
        }

        /**
         * 写入数据
         * @param al
         * @param file2
         * @throws IOException
         */
        private static void getJavaFilePath(ArrayList<File> al, File file2) throws IOException {
                // TODO Auto-generated method stub
                BufferedWriter bw = new BufferedWriter(new FileWriter(file2));
                for(File fileItem : al)
                {
                        bw.write(fileItem.getAbsolutePath());
                        bw.newLine();
                        bw.flush();
                }

                bw.close();
        }

        /**
         * 获取java文件的方法
         * @param file
         * @param al
         * @throws IOException
         */
        private static void getJavaFile(File file, ArrayList<File> al) throws IOException {
                File[] files = file.listFiles();
                for(File fileItem : files)
                {
                        if(fileItem.isDirectory())
                        {
                                getJavaFile(fileItem, al);
                        }else
                        {
                               if(file.getName().endsWith(".java"))//应该改为fileItem.getName().endsWith(".java");
                                {
                                        //将数据存入集合
                                        al.add(fileItem);
                                }

                        }
                }

        }
}

评分

参与人数 1技术分 +1 收起 理由
陈丽莉 + 1

查看全部评分

回复 使用道具 举报
public File[] listFiles()
返回一个抽象路径名数组,这些路径名表示此抽象路径名表示的目录中的文件。
如果此抽象路径名不表示一个目录,那么此方法将返回 null。否则返回一个 File 对象数组,
还要判断是否为null,
hehe

点评

请改成自己的名字哦~  发表于 2013-3-10 19:39

评分

参与人数 1技术分 +1 收起 理由
陈丽莉 + 1

查看全部评分

回复 使用道具 举报
File对象代表一个具体的文件或文件夹,不能将硬盘分区封装成File对象
File file1 = new File("I://");发生空指针异常。
如果真的能封装成File对象,试问当要创建这个文件时,文件应该创建在哪个目录下

  if(file.getName().endsWith(".java"))应该是fileItem.getName()
回复 使用道具 举报
如果有解决问题的答案,请回复他告知,之后将帖子分类改成【已解决】,多谢合作~
回复 使用道具 举报
张宁 中级黑马 2013-3-11 00:01:50
7#
  1. public class ShowPath {
  2.         public static void main(String[] args) throws IOException {
  3.                 //封装数据
  4.                 File file1 = new File("I://");//
  5.                 //创建集合,保存数据
  6.                 ArrayList<File> al = new ArrayList<File>();
  7.                 getJavaFile(file1, al);
  8.                 //封装目的地
  9.                 File file2 = new File("i://codepath.txt");

  10.                 getJavaFilePath(al, file2);
  11.         }
复制代码
你这里的路径 // 不对啦,应该是\\,改好之后,你的codepath.txt 就可以在你的I盘看见啦,

还有啦。if(file.getName().endsWith(".java"))应该改为fileItem.getName().endsWith(".java"); 小细节,细心一点了。
回复 使用道具 举报
再次提醒,如果还有问题,请继续追问,没有问题,请将帖子分类改成【已解决】。
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马