0

I need to create 4 output files.

I currently obtain a single file.

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();
7
  • This code should already create 4 files marked by new File(dir + File.separator + date.getTime()); (assuming different times) Commented Nov 9, 2016 at 21:11
  • hello cricket_007, thanks you. Commented Nov 9, 2016 at 21:19
  • This is the end of the code :String line; try { int read = 0; byte[] bytes = new byte[1024]; while ((read = is.read(bytes)) != -1) { fos.write(bytes, 0, read); // sb.append(line); } } catch (Exception e) { e.printStackTrace(); } Commented Nov 9, 2016 at 21:21
  • have to create a loop for the end, to obtain 4 files ? Commented Nov 9, 2016 at 21:23
  • I don't understand what you are asking.... That comment is reading the inputstream and writing to a single file. for(String url : tableaurl) already loops a total of 4 times and should create 4 files, as I said Commented Nov 9, 2016 at 21:26

2 Answers 2

1

If you want 4 files, then use 4 distinct names.

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

    i++;   
    File file = new File(dir + File.separator + i + "_" + date.getTime()); 
Sign up to request clarification or add additional context in comments.

Comments

0

You should use different date object to create different file name. Currently you are using a single object which returns the same time each time you call get time().

You can use new Date().get time().

1 Comment

hello Python, with new Date().get time(), not need to use a loop. Thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.