Reading A .TXT File in A Project in Eclipse
Reading A .TXT File in A Project in Eclipse
1. About
2. Products
3. For Teams
1. Log in Sign up
2.
By using our site, you acknowledge that you have read and understand our Cookie Policy, Privacy Policy, and our Terms of Service.
1.
Home
2.
1. PUBLIC
2. Stack Overflow
3.
Tags
4.
Users
5. FIND A JOB
6.
Jobs
7.
Companies
3.
1. TEAMS
What’s this?
2.
Free 30 Day Trial
try {
jsonData = Reader.readFile(path);
} catch (Exception e) {
e.printStackTrace();
}
Any help appreciated.
javaeclipse
Community♦
111 silver badge
asked Apr 1 '16 at 14:06
Zeus
1,73333 gold badges2020 silver badges4141 bronze badges
1
So where's the code? – Codebender Apr 1 '16 at 14:07
Non-java files don't belong in src/main/java but in src/main/resources. Also: "I still can't read the file." - what error are you getting? – f1sh Apr
1 '16 at 14:10
I,m getting the following error java.io.FileNotFoundException: DataSource/data1.txt (No such file or directory) – Zeus Apr 1
'16 at 14:13
@Sombriks has the right answer. You should almost never use te java.io.File class when build web applications. Always work with
streams. – markbernard Apr 1 '16 at 14:19
add a comment
2 Answers
ActiveOldestVotes
2
try
However you might want to package such resource on your jar. if so, you'll need to use something like:
jsonData = Reader.readFile(Caller.class.getResourceAsStream("DataSource/data1.txt""));
At last, move the .txt file to 'src/main/resources', since it's a good practice.
share improve this answer follow
answered Apr 1 '16 at 14:16
Sombriks
2,20433 gold badges3131 silver badges4040 bronze badges
The first one works, is there a way I can use the second method to consume a String as opposed to an input stream. – Zeus Apr 1 '16 at 14:27
What I mean is my reader uses a string parameter not an inputStream. – Zeus Apr 1 '16 at 14:28
Hello @Zeus, there are other options, this one for exemaple: docs.oracle.com/javase/7/docs/api/java/nio/file/… you read the bytes and then do a new
String with those bytes. however the .jar file limitation remains. If, at some point, your resource get packaged, only classpath resources can help you. – Sombriks Apr
1 '16 at 17:11
add a comment
0
Term ClassPath related to java code compilation and execution while term BuildPath related to IDE (i.e. Eclipse). IDE adds libraries, which are
added to BuildPath, automatically to the ClassPath while building and running the application/program.
"The caller class needs the file", then you can provide the file-path in two way
Mahendra
1,36677 silver badges1313 bronze badges
add a comment
Your Answer
Sign up or log in
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Name
Email
Required, but never shown
Remote jobs
Senior Java Software Developer- Remote Cincinnati
CallibrityCincinnati, OH
REMOTERELOCATION
javaaws
Java Developer
BairesDev LLCSan Francisco, CA
REMOTE
java
Linked
377
How to really read text file from classpath in Java
16
Where do I put the txt file that I want to read in Java?
Related
4222
How do I read / convert an InputStream into a String in Java?
1281
How can you speed up Eclipse?
744
How can I show line numbers in Eclipse?
1267
'Must Override a Superclass Method' Errors after importing a project into Eclipse
1921
“Debug certificate expired” error in Eclipse Android plugins
723
Does a “Find in project…” feature exist in Eclipse IDE?
1695
How can I avoid Java code in JSP files, using JSP 2?
953
Reading a plain text file in Java
1264
How to add local jar files to a Maven project?
982
Can't start Eclipse - Java was started but returned exit code=13
Question feed
https://stackoverflow.
STACK OVERFLOW
Questions
Jobs
Developer Jobs Directory
Salary Calculator
Help
Mobile
Disable Responsiveness
PRODUCTS
Teams
Talent
Advertising
Enterprise
COMPANY
About
Press
Work Here
Legal
Privacy Policy
Contact Us
STACK EXCHANGE
NETWORK
Technology
Life / Arts
Culture / Recreation
Science
Other
Blog
Facebook
Twitter
LinkedIn
Instagram
site design / logo © 2020 Stack Exchange Inc; user contributions licensed under cc by-sa. rev 2020.12.16.38204