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

  • Home
  • SEARCH
  • 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 8337829
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T04:24:27+00:00 2026-06-09T04:24:27+00:00

I have this code to validate a java.io.File parameter which should not be null

  • 0

I have this code to validate a java.io.Fileparameter which should not be null, should be accessible, should be a file and not a directory, etc.:

private static final String EXCEPTION_FILE_CAN_NOT_BE_READ =
    "The file %s does not seem to readable.";
private static final String EXCEPTION_PATH_DOES_NOT_EXIST =
    "The path %s does not seem to exist.";
private static final String EXCEPTION_PATH_IS_NOT_A_FILE =
    "The path %s does not seem to correspond to a file.";
private static final String EXCEPTION_PATH_REFERENCE_IS_NULL =
    "The supplied java.io.File path reference can not be null.";

public static Banana fromConfigurationFile(
    File configurationFile) {
  if (configurationFile == null) {
    String nullPointerExceptionMessage =
        String.format(EXCEPTION_PATH_REFERENCE_IS_NULL, configurationFile);
    throw new NullPointerException();
  }
  if (!configurationFile.exists()) {
    String illegalArgumentExceptionMessage =
        String.format(EXCEPTION_PATH_DOES_NOT_EXIST,
            configurationFile.getAbsolutePath());
    throw new IllegalArgumentException(illegalArgumentExceptionMessage);
  }
  if (!configurationFile.isFile()) {
    String illegalArgumentExceptionMessage =
        String.format(EXCEPTION_PATH_IS_NOT_A_FILE,
            configurationFile.getAbsolutePath());
    throw new IllegalArgumentException(illegalArgumentExceptionMessage);
  }
  if (!configurationFile.canRead()) {
    String illegalArgumentExceptionMessage =
        String.format(EXCEPTION_FILE_CAN_NOT_BE_READ,
            configurationFile.getAbsolutePath());
    throw new IllegalArgumentException(illegalArgumentExceptionMessage);
  }
  // ... more tests, like "isEncoding(X)", "isBanana(ripe)", ...
}

Looks like a lot of boilerplate for something I could be “pinching” from somewhere. Especially because these are not all the checks that I need, there is more (e.g. the file is a text file and has the right encoding, …). It seems reasonable to me that there would be a simpler way to do it than this. Perhaps a FileSpecs object to construct through a Builder and to pass to a verifyFileSpecs static helper?

Question: am I doing it wrong or is there code I could reuse?

Answer to the FAQ for post validity:

Shows I made some research beforehand: I looked at the Java 6 SDK, that’s where I got the different methods from, looked at JDK 7 and Files.isReadable, looked at Apache Commons IO, …

Shows that this question is unique: I am specifically asking if there is code that I can reuse, I am not asking “how do I check if a path corresponds to a file and not a directory?”, all of which has already an answer on SO

Why this could be useful to others: teams don’t like boilerplate code like that submitted for code review, checked-in and versioned, potentially maintained (unit tests, etc.) So, borrowing the code from a reputable source would be very helpful, in my opinion.

  • 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-09T04:24:30+00:00Added an answer on June 9, 2026 at 4:24 am

    Yes, I would say above code is not DRY (Don't Repeat Yourself).

    Consider using Validate from Apache Commons.

    public static Banana fromConfigurationFile(File configurationFile) {
      Validate.notNull(configurationFile, String.format(EXCEPTION_PATH_REFERENCE_IS_NULL, configurationFile));
      Validate.isTrue(configurationFile.exists(), String.format(EXCEPTION_PATH_DOES_NOT_EXIST, configurationFile.getAbsolutePath()));
      Validate.isTrue(configurationFile.isFile()), String.format(EXCEPTION_PATH_IS_NOT_A_FILE, configurationFile.getAbsolutePath()));
      // and more validation...
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this code: ie1.link(:text, /Exception:/) It is producing an error message which I
Doing a small project I have received some Java code that I should rewrite
We have a Perl program to validate XML which is invoked from a Java
I have a XSD file, from which I want to generate C# and Java
I have a model defined this way class Lga < ActiveRecord::Base validates_uniqueness_of :code validates_presence_of
I have this code <div id=main style=background:#aaaaaa;float:left;height:160px;margin:5px;position:relative;display:block;width:630px;> <div id=1 class=item style=background:#ffaacc;float:left;width:200px;height:150px;margin:5px;position:absolute;left:0px;top:0px;> </div> <div id=2
I have this code : void Main() { System.Timers.Timer t = new System.Timers.Timer (1000);
I have this code for changing the image of a button: - (void)mouseEntered:(NSEvent *)event
I have this code in XAML <Grid x:Name=LayoutRoot Background=Transparent> <Grid.RowDefinitions> <RowDefinition Height=Auto/> <RowDefinition Height=*/>
I have this code: EditText value = ( EditText )findViewById( R.id.editbox ); Integer int_value

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.