Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8011973
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T19:11:05+00:00 2026-06-04T19:11:05+00:00

I have a function that downloads files from a web server that is sometimes

  • 0

I have a function that downloads files from a web server that is sometimes fed an empty collection. In the function I call each on that collection and what I would expect to happen is that the function just exits with the each closure not being run at all. The problem is that it does get run with an empty filename parameter and the creation of the FileOutputStream goes boom when it is fed a directory instead of a file.

def get(String baseUrl, List files, String targetDir) {
    files.each { filename ->
    // Goes BOOM on next line
    def fos = new FileOutputStream(targetDir + File.separator + filename)
    ...
}

Why does Groovy behave like this and what should I do instead?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T19:11:08+00:00Added an answer on June 4, 2026 at 7:11 pm

    It doesn’t, so I assume files contains something (like null?)

    [].each {
      println "boom"  // This doesn't appear
    }
    
    [null].each {
      println "pow!"  // this does
    }
    

    Assuming it is nulls in your files List that is causing the issue, you can get rid of them by:

    files.findAll().each { filename ->
      def fos = new FileOutputStream( new File( targetDir, filename ) )
      ...
    

    Or of course, make the thing that generates the List not add nulls in the first place

    Edit

    Actually, it sounds like you have a List with empty strings in it…

    The findAll fix should still work, as empty strings evaluate to false under Groovy Truth

    Edit 2

    As a quick note, you can probably change:

    def fos = new FileOutputStream( new File( targetDir, filename ) )
    ...
    

    to:

    new File( targetDir, filename ).withOutputStream { fos ->
      ...
    

    And it will ensure the stream is closed for you 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a downloader function that downloads multiple files parallely. I use multiprocessing.Pool.map_async in
We have a large project that needs to sync large files from a server
In my Java code I have function that gets file from the client in
Hey everyone, I have written a script that downloads a zip file from a
I have some code that downloads a file from the internet located here: http://www.amsat.org/amsat/ftp/keps/current/nasa.all
I am trying to program an App that fetches files from a server. I
I'm currently using a HttpResponse to download files from my Server. I already have
My php script downloads files from a remote server using cURL. I wanted to
I have a command-line utility that gets quite a bit of downloads from my
I have a function to download a mailmessage as MSG file from DocuShare server.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.