How to read text files from the Classpath in Java

0 votes
I am trying to read the file which is set in system CLASSPATH system variable. How to read this file?
May 8, 2018 in Java by Sushmita
• 6,920 points
3,694 views

1 answer to this question.

0 votes
InputStream in = this.getClass().getClassLoader().getResourceAsStream("TextFile.txt");
InputStream in = this.getClass().getResourceAsStream("/TextFile.txt");


package dummy;

import java.io.*;

public class Test
{
    public static void main(String[] args)
    {
        InputStream stream = Test.class.getResourceAsStream("/TextFile.txt");
        System.out.println(stream != null);
        stream = Test.class.getClassLoader().getResourceAsStream("TextFile.txt");
        System.out.println(stream != null);
    }
}
answered May 8, 2018 by Akrati
• 3,190 points

Related Questions In Java

0 votes
1 answer

How to read all files in a folder from Java?

public void listFilesForFolder(final File folder) { ...READ MORE

answered Dec 30, 2020 in Java by Gitika
• 65,730 points
3,288 views
0 votes
3 answers

How to read a Text File in Java?

You can use readAllLines and the join method to ...READ MORE

answered Jul 28, 2018 in Java by samarth295
• 2,220 points
3,684 views
0 votes
2 answers

How to read the files using Java?

You can use collections. try (Stream<Path> filePathStream=Files.walk(Paths.get("/home/you/Desktop"))) { ...READ MORE

answered Jul 10, 2018 in Java by Sushmita
• 6,920 points
1,929 views
0 votes
2 answers

How can we add the local JAR files to the Maven Project in Java?

Firstly I would like to give credit ...READ MORE

answered Nov 5, 2018 in Java by Sushmita
• 6,920 points
14,104 views
0 votes
1 answer

How can we get file extension in Java?

In Java, you can find the file ...READ MORE

answered Apr 3, 2024 in Java by kevin

edited Mar 5, 2025 4,047 views
0 votes
1 answer

How to read and write on an excel files?

Follow these steps: Write: public class User { ...READ MORE

answered May 15, 2018 in Java by Rishabh
• 3,600 points
2,218 views
0 votes
1 answer

Access restriction on class error

My guess is that you are trying ...READ MORE

answered Jun 21, 2018 in Java by Rishabh
• 3,600 points
1,588 views
0 votes
2 answers

How can I get the filenames of all files in a folder which may or may not contain duplicates

List<String> results = new ArrayList<String>(); File[] files = ...READ MORE

answered Sep 12, 2018 in Java by Sushmita
• 6,920 points
2,865 views
0 votes
2 answers

How to read a text file in Java?

You can use Scanner class to read ...READ MORE

answered Aug 9, 2018 in Java by Parth
• 4,640 points
2,092 views
0 votes
2 answers

How to break the nested loop in Java?

You can use break with a label for the ...READ MORE

answered Sep 20, 2018 in Java by Sushmita
• 6,920 points
2,230 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP