2016-11-09 23 views
0

4つの出力ファイルを作成する必要があります。文字列配列から出力ファイルを作成する

私は現在、1つのファイルを取得しています。

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

このコードでは、 'new file(dir + File.separator + date.getTime());'(別の時刻を想定)という4つのファイルを既に作成しているはずです –

+0

hello cricket_007、ありがとう。 – baba

+0

これはコードの最後です。 \t \t \t \t試み{ \t \t \t \t \t \t \t \t \t \t INT = 0を読み出します。 \t \t \t \t \tバイト[]バイト=新しいバイト[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(行); \t \t \t \t \t} \t \t \t \t}キャッチ(例外e){ \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。新しいDate()。get time()。ループを使う必要はありません。ありがとう – baba

関連する問題