2017-06-02 57 views
-2

我寫了一個java代碼打印我的輸出在位於文檔,但執行的代碼文件後,文件仍然是empty.Can有人讓我犯錯誤正在使用NetBeans創建此程序如何使用java獲取輸出到文件?

代碼

import java.net.HttpURLConnection; 
import java.net.URL; 
import java.lang.*; 
import java.io.*; 

public class NewClass1 { 

    static long start = 0; 
    static long finish = 0; 
    static long time; 

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

     BufferedWriter bw = null; 
     FileWriter fw = null; 
     for (int i = 0; i <= 5; i++) { 

      String stack_url = "https://www.google.lk/"; 

      try { 
       URL url = new URL(stack_url); 
       HttpURLConnection httpUrlConnect = (HttpURLConnection) url.openConnection(); 
       httpUrlConnect.setConnectTimeout(10); 
       start = System.currentTimeMillis(); 
       httpUrlConnect.connect(); 

       if (httpUrlConnect.getResponseCode() == 200) { 
        finish = System.currentTimeMillis(); 
        System.out.println(stack_url + " - " + httpUrlConnect.getResponseMessage() + " took " + (finish - start) + " Milli Seconds."); 
       } 

       if (httpUrlConnect.getResponseCode() == httpUrlConnect.HTTP_NOT_FOUND) { 
        System.out.println(stack_url + " - " + httpUrlConnect.getResponseMessage() + " - " + httpUrlConnect.HTTP_NOT_FOUND); 
       } 
      } catch (Exception e) { 

      } 

      time = finish - start; 
      finish = System.currentTimeMillis(); 
      System.out.println("Total Time for page load - " + (finish - start)); 

      try { 

       Integer _time = (int) (long) time; 
       String content = Integer.toString(_time); 
       fw = new FileWriter("time.sql"); 
       bw = new BufferedWriter(fw); 
       bw.write(content); 

       System.out.println("Done"); 

      } catch (IOException e) { 

       e.printStackTrace(); 

      } finally { 

       try { 

        if (bw != null) { 
         bw.close(); 
        } 

        if (fw != null) { 
         fw.close(); 
        } 

       } catch (IOException ex) { 

        ex.printStackTrace(); 

       } 
      } 
     } 

     Thread.sleep(1000); 
    } 
} 
+3

** **從不} write'趕上(例外五){ }'。始終處理異常。 atleast logg stacktrace – Jens

+0

我想你會得到一個異常。通過閱讀一個URL。在這種情況下,將創建一個空文件 – Jens

+1

此外,你創建一個循環中的文件,所以在每次執行該文件將被覆蓋 – Jens

回答

-1

您應該使用EDITED嘗試,與資源,如:

try (FileWriter fw = new FileWriter("D:\\time.sql")) 

作爲你的主要方法的第一行,並在你終於阻止,你可以刪除fw.close() 調用。

如果您希望可以直接使用fw.write(content);而不是緩衝寫入器。

你可以擺脫下面的代碼(如您的循環,每次一個新對象構造不要求),如果您使用的資源的嘗試:

fw = new FileWriter("time.sql"); 
bw = new BufferedWriter(fw); 
bw.write(content); 

,而使用:

fw.write(content); 

我確實進行了上述修改,它適用於我。

HTH, AJ

1

第一件事:

你的程序不花全部時間如此相反,你必須增加時間執行:

httpUrlConnect.setConnectTimeout(1000); 
//--------------------------------^^ 

第二件事:

不要創建你的代碼中的文件,只需創建它之前,所以你必須移動這兩個線路的外循環:

fw = new FileWriter("time.sql"); 
bw = new BufferedWriter(fw); 

第三件事:

請勿關閉你的文件在每次迭代中,當你完成時關閉它,所以你必須在你的循環之後刪除finally塊並關閉你的文件。

bw.close(); 
fw.close(); 

您的代碼應該是這樣的:

通過user1516873

@Test 
public void test44325932() { 

    final String stackUrl = "https://www.google.lk/"; 

    // BufferedWriter is closable, it will close automatically (and close underline stream) in finally block 
    try (BufferedWriter bw = new BufferedWriter(new FileWriter("time.sql"))) { 

     for (int i = 0; i <= 5; i++) { 
      String responseStatus; 
      long start = System.currentTimeMillis(); 
      long finish; 
      try { 
       URL url = new URL(stackUrl); 
       HttpURLConnection connection = (HttpURLConnection) url.openConnection(); 
       connection.setConnectTimeout(5000); // 5 sec connect timeout 
       connection.connect(); 
       if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) { 
        responseStatus = "OK"; 
       } else { // any non 200 status counts as error 
        responseStatus = connection.getResponseCode() + " : " + connection.getResponseMessage(); 
       } 
       // full time should be calculated after completely reading response, not after get status code, but it is depends of requirements 
       // I put it here as example 
       // IOUtils.readFully(connection.getInputStream(), new byte[1024]); 
       connection.disconnect(); 
      } catch (IOException e) {     
       responseStatus = "Exception : " + e.getClass().getSimpleName(); 
      } finally { 
       finish = System.currentTimeMillis(); 
      } 

      long time = finish - start; 
      System.out.println("Server response: " + time + " ms, status: " + responseStatus); 
      bw.write(String.valueOf(time)); // i think you should write status here, too. 
      bw.newLine(); 
      Thread.sleep(1000); 
     } 
     System.out.println("Done"); 
    } catch (Exception ex) { 
     ex.printStackTrace(); 
    } 
} 
相關問題