2016-11-09 29 views
0

我需要創建4個輸出文件。從字符串數組創建輸出文件

我目前獲得一個文件。

String url1 = "www.xxxx.com"; 
String url2 = "www.xxxx.com"; 
String url3 = "www.xxxx.com"; 
String url4 = "www.xxxx.com"; 
String tableaurl[] = {url1,url2,url3,url4}; 

for(String url : tableaurl) 
{ 
     String encodedString = UrlUtils.encodeAnchor(url); 
     System.out.format("%s\n", encodedString); 
     URL myURL = new URL(encodedString); 
     String userpass = "username" + ":" + "password"; 
     String basicAuth = "Basic " + Base64.encode(userpass.getBytes("UTF-8")); 
     URLConnection myURLConnection = myURL.openConnection(proxy); 
     myURLConnection.setRequestProperty("Authorization", basicAuth); 
     myURLConnection.connect(); 
     InputStream is = myURLConnection.getInputStream(); 
     BufferedReader br = null; 
     File dir = new File(home + File.separator + "collected" + File.separator +"test"); 
     dir.mkdirs(); 
     File file = new File(dir + File.separator + date.getTime()); 
     FileOutputStream fos = new FileOutputStream(file); 
     StringBuilder sb = new StringBuilder(); 
+0

此代碼應該已經創建了4個文件,標記爲'new File(dir + File.separator + date.getTime());'(假設時間不同) –

+0

hello cricket_007,謝謝。 – baba

+0

這是代碼的結尾:String line; \t \t \t \t嘗試{ \t \t \t \t \t \t \t \t \t \t INT讀= 0; \t \t \t \t \t byte [] bytes = new byte [1024]; \t \t \t \t \t而((讀取= is.read(字節))= - 1!){ \t \t \t \t \t \t fos.write(字節,0,讀); \t \t \t \t \t \t // sb.append(line); \t \t \t \t \t} \t \t \t \t}趕上(例外五){ \t \t \t \t \t e.printStackTrace(); \t \t \t \t \t \t \t \t \t} – baba

回答

1

如果你想要4個文件,然後使用4個不同的名稱。

int i = 0; // Some number counter 
for(String url : tableaurl) { 
    // other code... 

    i++; 
    File file = new File(dir + File.separator + i + "_" + date.getTime()); 
0

您應該使用不同的日期對象來創建不同的文件名。目前,您使用的是每次調用get time()時返回相同時間的單個對象。

您可以使用新的Date()。get time()。

+0

hello Python,用new Date()。get time(),不需要使用循環。謝謝 – baba