【问题标题】:Reading the java files from the folder从文件夹中读取java文件
【发布时间】:2012-06-28 16:20:11
【问题描述】:

我开发了一个从用户选择的文件夹中读取文件的应用程序。它显示每个文件中有多少行代码。我只想在文件选择器中显示 Java 文件(扩展名为 .java 的文件)。以下是我的代码:

 public static void main(String[] args) throws FileNotFoundException {

        JFileChooser chooser = new JFileChooser();
        chooser.setCurrentDirectory(new java.io.File("C:" + File.separator));
        chooser.setDialogTitle("FILES ALONG WITH LINE NUMBERS");
        chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
        chooser.setAcceptAllFileFilterUsed(false);
                if (chooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION)
        {      Map<String, Integer> result = new HashMap<String, Integer>();
             File directory = new File(chooser.getSelectedFile().getAbsolutePath()); 
             int totalLineCount = 0;
             File[] files = directory.listFiles(new FilenameFilter(){
                  @Override
                  public boolean accept(File dir, String name) {
                    return name.matches("\\*\\.java");
                  }
                }
   );
              for (File file : files)
            {
                if (file.isFile())
                {    Scanner scanner = new Scanner(new FileReader(file));
                    int lineCount = 0;
                     try
                    { for (lineCount = 0; scanner.nextLine() != null; lineCount++) ;
                          } catch (NoSuchElementException e)
                    {   result.put(file.getName(), lineCount);
                    totalLineCount += lineCount;  
                                    }


                } }
              System.out.println("*****************************************");
              System.out.println("FILE NAME FOLLOWED BY LOC");
              System.out.println("*****************************************");

            for (Map.Entry<String, Integer> entry : result.entrySet())
            {   System.out.println(entry.getKey() + " ==> " + entry.getValue());
            }
            System.out.println("*****************************************");
            System.out.println("SUM OF FILES SCANNED ==>"+"\t"+result.size()); 
            System.out.println("SUM OF ALL THE LINES ==>"+"\t"+ totalLineCount);

             }  

我也编辑了,但还是不行,请指教 请告知如何仅读取具有 .java 作为扩展名的文件,换句话说,仅读取文件夹中的 java 文件,请告知

【问题讨论】:

    标签: java filenames jfilechooser


    【解决方案1】:

    您需要一个文件名过滤器。这应该适合你:

    FilenameFilter javaFileFilter= new FilenameFilter() {  
      @Override
      public boolean accept(File logDir, String name) {
        return name.endsWith(".java")
      }
    };
    

    【讨论】:

    • 应该只是return name.endsWith(".java")
    • @Christoffer Hammarström ..在我的代码中添加这个的地方请告知
    • @NareshSaxena:将FilenameFilter 作为参数传递给dir.listFiles()
    • @ChristofferHammarström;我从我正在使用的另一个 FilenameFilter 复制粘贴了代码,这有点复杂。没有改变条件部分。编辑我的帖子以反映您的评论。谢谢。
    • 可能它甚至应该是return name.toLowerCase().endsWith(".java")
    【解决方案2】:

    您应该在 JFileChooser 中查看 Filtering the list of Files

    它有一个ImageFilter.java 的例子,它只在文件选择器中显示图像文件。

    【讨论】:

    • 他只是在JFileChooser 中挑选一个目录。然后在那个目录中他想找到并阅读所有.java-files。
    • @Christoffer Hammarström 你能告诉我如何在我的代码中实现这个目标
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-02-09
    • 2016-03-17
    • 2017-03-06
    • 2022-01-08
    • 2011-10-11
    • 2014-12-23
    • 1970-01-01
    相关资源
    最近更新 更多