Solved

Java Referencing file in eclipse project

Posted on 2014-01-15
7
311 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
Comment Utility
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
Comment Utility
0
 

Author Comment

by:zolf
Comment Utility
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
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
LVL 86

Accepted Solution

by:
CEHJ earned 450 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Thanks
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
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…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…

762 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

7 Experts available now in Live!

Get 1:1 Help Now