-1

可能重复:
从文件夹中读取 java 文件

我开发了一个 java 代码,可以从用户选择的文件夹中读取文件。它显示每个文件中有多少行代码,它只读取 .java 文件,最终结果显示在控制台上,我在想输出要显示在控制台上,但连同包含相同信息的文本文件一起获取也存储在桌面上,请告知如何做到这一点,并且生成的文件的名称将基于时间戳假设输出文件的名称为“output06282012”并且该文本文件应包含相同的信息显示在控制台上,这是我的一段代码...

    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 directory, String name) {
                      if(name.endsWith(".java"))
                      return true;
                    else
                      return false;              
                  }
                }
   );
              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);

             }     

    }

控制台上显示的输出也将存储在桌面上的文本文件中,请告知如何存储,并且生成的文件的名称将基于时间戳假设输出文件的名称为'output06282012' 并且该文本文件应包含与控制台上显示的相同信息

4

1 回答 1

3

我建议您更准确地提出问题。根据我从您的问题中了解到的情况,您想将一些信息写入文本文件。要做到这一点,你所要做的就是——

try{

 java.util.Date date= new java.util.Date();
 System.out.println(new Timestamp(date.getTime())); 

BufferedWriter out = new BufferedWriter(new FileWriter("C://Desktop//output"+new Timestamp(date.getTime())+".txt"));

out.write("some information");

out.close; 

}catch(IOException e){
  e.printStackTrace();
 }

因此,在您的代码代替System.out.println();语句中,您可以使用out.write() 语句写入文本文件。

希望这对您有所帮助。

于 2012-06-28T18:05:42.377 回答