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 9317353
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 19, 20262026-06-19T02:51:28+00:00 2026-06-19T02:51:28+00:00

I ran into a funny situation because java’s File.listFiles() method returns a list of

  • 0

I ran into a funny situation because java’s File.listFiles() method returns a list of Files in a directory without any guarantees on the order. One class reads some information from all files in a directory using this listFiles() method and writes to an OutputStream. A corresponding JUnit test cases tries to compare the data written to the output stream with some expected byteArray. The test passed on my dev machine and the production stack, but failed on a dev server because it uses a different OS and a different version of Java.

In how to File.listFiles in alphabetical order? the solution is to force the sorting of the array returned from File.listFiles(). But I am not comfortable about this solution because it is doing some extra work just so the JUnit test can pass.

So I am just wondering if there is a better way. Any advice? Thanks

  • 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-19T02:51:29+00:00Added an answer on June 19, 2026 at 2:51 am

    I do not see a problem with always sorting the list. You are not only doing it for the unit test: Reducing randomness in a system is usually a good idea (it makes testing easier, and when problems occur, it makes them easier to reproduce). It’s true that you are doing extra work, but unless you are reading thousands and thousands of files, the sorting is unlikely to be a bottleneck (of course, profile regularly).

    So in my opinion, sorting is the best option.

    If you don’t want to / cannot do this, the other option would be to make the unit test smarter; for example, don’t compare the whole byteArray in one go, but check total size and that the result for each file is present in the array. Or parse and sort the array in the test (though that may be complicated).

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

Sidebar

Related Questions

I ran into a problem or maybe even a MySQL limitation. The situation is
Ran into a situation where am not sure how to handle it. I have
I have the funny situation, that I store a Coordinate into a HashMap<Coordinate, GUIGameField>
I ran into an issue today because I am developing an application in Visual
I ran into a funny issue today working with large data structures. I initially
I ran into a method today that is .. not used anywhere .. but
I ran into an interesting (and very frustrating) issue with the equals() method today
Ran into an Out of Stack Space error trying to serialize an ASP.Net AJAX
Ran into something interesting, want to know if I'm doing something wrong or if
Ran into this error message while trying to select some records off a table.

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.