2016-11-09 73 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