简体   繁体   English

从文件夹中读取java文件

[英]Reading the java files from the folder

I have developed an application that reads files from the folder chosen by the user. 我开发了一个应用程序,用于从用户选择的文件夹中读取文件。 It displays how many lines of code are in each file. 它显示每个文件中有多少行代码。 I want only Java files to be shown in the file-chooser (files having .java extension). 我只想在文件选择器中显示Java文件(扩展名为.java的文件)。 Below is my code: 以下是我的代码:

 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);

             }  

I have editied also but still it is not working please advise please advise how to read only the files having .java as an extension in other words only java files from the folder ,please advise 我也编辑了但是仍然没有工作请告知请告知如何只读取.java作为扩展名的文件,换句话说只有文件夹中的java文件,请指教

You should look upon Filtering the list of Files in JFileChooser. 您应该查看过滤 JFileChooser 中的文件列表

It has an example of ImageFilter.java which shows only image files in file chooser. 它有一个ImageFilter.java的例子,它只显示文件选择器中的图像文件。

You need a FilenameFilter. 你需要一个FilenameFilter。 This should work for you: 这应该适合你:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM