2012-06-28 59 views
-1

可能重複:
Reading the java files from the folder生成輸出的文本文件,通過Java IO

我公司開發的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); 

      }  

    } 

輸出顯示在控制檯被存儲在桌面上的一個文本文件還,請指教如何在和的名字生成它的名字的文件是基於時間戳讓我們假設outp的名字ut文件將是'output06282012',並且該文本文件應該包含控制檯上顯示的相同信息。

+0

夥計們請告知 –

+1

永遠不要這樣做'main(String [] args)引發FileNotFoundException'永遠不會在主要引發異常中捕獲它! –

+0

@DavidKroukamp請告訴我可以在我的代碼片段中捕捉它 –

回答

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() 語句寫入到一個文本文件中。

希望這可以幫助你。

+0

是的,我想要接近但不完全是這樣 –

+0

那麼你到底想做什麼? –

+0

在控制檯上顯示的輸出也將被存儲在桌面上的文本文件中,請告知如何以及生成其文件名的文件的名稱是基於時間戳的。假設輸出文件的名稱將會是'output06282012',並且該文本文件應該包含與控制檯上顯示的信息相同的信息。 –