0% found this document useful (0 votes)
125 views11 pages

Reading A .TXT File in A Project in Eclipse

ok

Uploaded by

endale
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
125 views11 pages

Reading A .TXT File in A Project in Eclipse

ok

Uploaded by

endale
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 11

Stack Overflow

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

Reading a .txt file in a project in Eclipse


Ask Question
Asked 4 years, 8 months ago
Active 4 years, 8 months ago
Viewed 3k times
0
Well I understand that this question has been asked multiple times, but despite following all the advice in those links I still end up getting file not
found error.

These are the links I've already checked.

Where do I put the txt file that I want to read in Java?

How to really read text file from classpath in Java

So please don't get trigger happy marking it as duplicate.


For one most of these links seem to use the word ClassPath and BuildPath interchangeably, are they the one and the same thing, coz in my IDE I
can't seem to find anything related to ClassPath, it's only BuildPath.

So this is my directory structure.


The caller class needs the file
Well I've tried pretty much everything in the links and I still can't read the file.

The code that reads the file is here

String path = "DataSource/data1.txt";


String jsonData = null;

try {

jsonData = Reader.readFile(path);
} catch (Exception e) {
e.printStackTrace();
}
Any help appreciated.
javaeclipse

share  improve this question  follow 


edited May 23 '17 at 12:24

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

String path = "src/main/java/DataSource/data1.txt";


understand that your execution point inside eclipe is at project root.

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

1. Provide the absolute file path.


2. Provide relative path, which you are doing, but make sure it is relative to the current location from where you are running your
program. In case of IDE relative path should be from Project-Directory.
share  improve this answer  follow 
answered Apr 1 '16 at 14:28

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

Post Your Answer


By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy
Not the answer you're looking for? Browse other questions tagged java eclipse or ask your own question.
The Overflow Blog
 Podcast 295: Diving into headless automation, active monitoring, Playwright…
 Hat season is on its way! Join us for Winter Bash 2020
Featured on Meta
 New Feature: Table Support
Report this ad

Remote jobs

Backend Engineer w/ Machine Learning - Remote


NumbrsNo office location
REMOTE
javaapache-kafka


Senior Java Software Developer- Remote Cincinnati
CallibrityCincinnati, OH
REMOTERELOCATION
javaaws

Android Developer (Java/Kotlin)


Retrica, Inc.No office location
$12K - $45KREMOTE
javaandroid

High response rate

Senior PHP Software Engineer (m/f/d) - 100% REMOTE


Joyride GmbHNo office location
€60K - €96KREMOTE
phpamphp

Senior Java + Spring Boot Developer


Napptilus Tech LabsBarcelona, Spain
REMOTE
javaspring-boot

Java Developer
BairesDev LLCSan Francisco, CA
REMOTE
java

High response rate

Security Engineer - Remote


NumbrsNo office location
REMOTE
distributed-systempython

Senior Backend Java Engineer


Hypixel StudiosNo office location
REMOTE
javakubernetes

View more jobs on Stack Overflow

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

Hot Network Questions


 English word for someone who often and unwarrantedly imposes on others
 What was the breakthrough behind the sudden feasibility of mRNA vaccines in 2020?
 Zero-gravity space bar fight?
 Curving grades without creating competition among students
 Reason of variation in sizes of fractions?
 Dice rolling mechanic where modifiers have a predictable and consistent effect on difficulty
 How does Eurostar segregate Brussels-bound and London-bound passengers from the Netherlands?
 How to make asset look more "3d" (sail of a sailboat)
 Did Trump vote illegally?
 Starlink vs 5G ? Starlink will help 5G and the "next G's" after that?
 What will cause nobles to tolerate the destruction of monarchy
 Faculty TT verbal offer made, but no written offer (it's been about 10 business days)
 Constructing the adjacency matrix of a square lattice
 Plotting the Star of Bethlehem
 Why were the FBI agents so willing to risk the hostages' lives?
 On the proof of The Bounded Convergence Theorem
 Are functor categories with triangulated codomains themselves triangulated?
 Select the holes in a vector shapefile in QGIS
 What does it mean that ''though [Abel] is dead, he still speaks'' in Hebrews 11:4?
 Why can't the human eye focus to make blurry photos/video clear?
 What is the origin of 場所?
 Did any European computers use 10-line fonts?
 Is memorizing common interview questions a good tactic in preparing for interviews?
 How to make a round corner inside?

 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
 

You might also like