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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T07:18:53+00:00 2026-05-14T07:18:53+00:00

I’m trying to read in file content, ex : public void myMethod(){ FileInputStream fstream

  • 0

I’m trying to read in file content, ex :

public void myMethod(){
     FileInputStream fstream = new FileInputStream(fileLocation);
     BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
     String strLine;
     while ((strLine = br.readLine()) != null) {
....
....
.....
end while 
end method

And I have at the begining of the class body private String fileLocation; and at the end of a class I have a getter and setter for it. Now I’m trying inject this file location from spring inside bean from this class and I specify the init-method of this class. But I get error cannot find the specified file as if its not on a classpath but it is inside war file? I’m building the project with maven and I put file in src/main/resources This is the error I get when trying to read file :

Error: src\main\resources\ids.txt
(The system cannot find the path
specified)

That is when I tried this :

FileInputStream fstream = new FileInputStream("src\\main\\resources\\ids.txt");

how to reference the properly from the classpath?

EDIT

When I edit my code according to @BalusC solution , here is how it looks but I still get null error :

ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); 
   InputStream input = classLoader.getResourceAsStream("src/main/resources/ids.txt");
   BufferedReader br = new BufferedReader(new InputStreamReader(input));
   String strLine;
 while ((strLine = br.readLine()) != null) {
    ....
    ....
    .....
    end while 
    end method
  • 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-05-14T07:18:54+00:00Added an answer on May 14, 2026 at 7:18 am

    The Java IO API relies on the local disk file system, not on the classpath. Besides, using relative paths in Java IO stuff is recipe for portability trouble, don’t rely on it. To allocate resources in the classpath you would normally use ClassLoader#getResource() or ClassLoader#getResourceAsStream().

    ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
    InputStream input = classLoader.getResourceAsStream("src/main/resources/ids.txt");
    

    That said, you don’t need that DataInputStream line. You’re actually not taking any benefit from it.

    Update: if that doesn’t work, then either the resource name is simply invalid or the file is actually not there in the classpath where you expect it to be. My cents on that the src folder is actually the root of the classpath and not part of a package. Remove it from the name.

    Update 2: to get all root disk file system paths which are covered by the runtime classpath do:

    for (URL root : Collections.list(Thread.currentThread().getContextClassLoader().getResources(""))) {
        System.out.println(root);
    }
    

    The resource name needs to be relative to either of them. That it is been placed in /WEB-INF/classes during the build is normal. It is covered by the classpath. Your problem lies somewhere else. Are you sure that the resource name is correct? Are you sure that you’re running the code you think you are running?

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

Sidebar

Ask A Question

Stats

  • Questions 406k
  • Answers 406k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You will need a SMS gateway. A lot of them… May 15, 2026 at 6:15 am
  • Editorial Team
    Editorial Team added an answer Given the method signature you provided, you can 'simply' unit… May 15, 2026 at 6:15 am
  • Editorial Team
    Editorial Team added an answer looks like just this will do it ob_get_contents May 15, 2026 at 6:15 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.