Solved

Java Referencing file in eclipse project

Posted on 2014-01-15
7
315 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
The New “Normal” in Modern Enterprise Operations

DevOps for the modern enterprise offers many benefits — increased agility, productivity, and more, but digital transformation isn’t easy, especially if you’re not addressing the right issues. Register for the webinar to dive into the “new normal” for enterprise modern ops.

 
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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How to determine if a string is a valid SHA value 7 35
ejb entity bean example 2 11
Notify sent to other threads in Java 9 33
Java basic valueOf question 1 29
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
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 will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

839 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