Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Data from Jar file

Status
Not open for further replies.

k4ghg

Technical User
Dec 25, 2001
191
US
I am currently accessing data from a file on my hard drive to populate a combo box:

BufferedReader br =
new BufferedReader{new FileReader (new File ("C:\\Program Files\\Java_Test\\Test_Data\\File.dat")));

While ((line = br.readLine()) != null{
cboText.addItem(line.substring(3, 3+12));
}

I would like to put the File.dat in a JAR with the rest of my program, but am having problems accessing the file from within the JAR. Could someone please help me access/read the file from with in a JAR called Test.JAR.

Thanks...Ronnie
 
Say you class is called Test, and is in the jar file.
If you added another file called "file.dat" in the same directory as the Test.class file, you can access that file like this :

InputStream is = (InputStream)(getClass().getResourceAsStream("file.dat"));

--------------------------------------------------
Free Database Connection Pooling Software
 
... also, if you want to put the file in the root directory of the jar instead, it'd be accessed like
Code:
InputStream is = (InputStream)(getClass().getResourceAsStream("/file.dat"));

Tim
---------------------------
"Your morbid fear of losing,
destroys the lives you're using." - Ozzy
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top