Solved

Java Referencing file in eclipse project

Posted on 2014-01-15
7
313 Views
Last Modified: 2014-01-16
Hello there,

Can somebody help me to understand how to reference a file in a folder. I have acctached my eclipse project structure. I am trying to refer a file in the templateprint folder in a java class in com.docmosis.sample.

I tried something like this but it cannot fine the file

TemplateIdentifier templateId = new TemplateIdentifier("/templateprint/exampleTemplate.doc);

Open in new window


cheers
ZOlf
ee1.gif
0
Comment
Question by:zolf
7 Comments
 
LVL 6

Expert Comment

by:Mahesh Bhutkar
ID: 39781588
You can add your 'templateprint' directory as a source-folder (Project -> Properties -> build path > use as source folder).

InputStream ir = Thread.currentThread().getContextClassLoader().getResourceAsStream("exampleTemplate.doc");

It should work.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 39781962
0
 

Author Comment

by:zolf
ID: 39782213
Thanks for the feedbacks.but in my code what do i need to do so my code will use that file.i mean what am i doing wrong in my code
0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
LVL 86

Accepted Solution

by:
CEHJ earned 450 total points
ID: 39782484
Actually, since the (quite sparse) api docs suggest that the only TemplateStore implementation is file based, you can probably ignore my comments about resource loading. Instead, you should probably use file paths. The 'proper' way would be to configure that declaratively, with paths to stores being enterable into the app itself. If you want to provide a default, then you should probably be aware of the current directory and construct the path using java.io.File accordingly

http://technojeeves.com/joomla/index.php/free/132-find-the-current-directory-in-java

The composition of a TemplateIdentifier is something entirely unknown to me
0
 
LVL 35

Assisted Solution

by:mccarl
mccarl earned 50 total points
ID: 39784309
Not withstanding the facts above that in general you should provide proper file/path handling, if this is just an example program that you are trying to get going, this may work...
TemplateIdentifier templateId = new TemplateIdentifier("templateprint/exampleTemplate.doc);

Open in new window

I have removed the leading / so that it uses relative paths, and Eclipse should launch your program with the current directory set as your project directory, so it should be able to find it there.
0
 
LVL 86

Assisted Solution

by:CEHJ
CEHJ earned 450 total points
ID: 39785277
and Eclipse should launch your program with the current directory set as your project directory, so it should be able to find it there.
That might be the case (i haven't checked the paths, nor know what the composition of that ID should be) but how an app runs from an IDE is irrelevant and using relative paths like that is fragile as (particularly in the absence of an installer) the current directory is not completely certain
0
 

Author Closing Comment

by:zolf
ID: 39787635
Thanks
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

26 Experts available now in Live!

Get 1:1 Help Now