Solved

create a file object without a path

Posted on 2014-04-04
12
234 Views
Last Modified: 2014-04-10
I have a project that works fine with some code like this -
FileInputStream fileIn = new FileInputStream(new File("c:\path\filename.xls"));
As long as I'm on my local system. However, there is a need for others to use the same project. so I'm wondering how I can embed the filename.xls file into the class and create a new file (of FileInputStream) object without having to refer to the drive or path. Assume the class is named generator and the filename.xls is in the src folder.
0
Comment
Question by:awking00
  • 6
  • 5
12 Comments
 
LVL 31

Author Comment

by:awking00
ID: 39978964
I meant ... (or FileInputStream)
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 500 total points
ID: 39979019
Well, assuming you can write to the current directory, why not just use the file name only?
0
 

Expert Comment

by:Peter_Toretto
ID: 39979632
Hi,

Try using code similar to this: -
 
File file = new File("C:\\user\\Desktop\\dir1\\dir2\\filename.txt");
file.getParentFile().mkdirs();
FileWriter writer = new FileWriter(file);

Just use the file name.

Hope you find this information useful.

Regards
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 39979735
I wasn't thinking. In order to solve the problem i mentioned, as well as yours, you should probably use the user's home directory

File f = new File(System.getProperty("user.home"), saveAsFilename); // ('saveAsFilename' is of course a String)

Open in new window


That should always work, and cross platform
0
 
LVL 31

Author Comment

by:awking00
ID: 39980146
CEHJ,
The problem here is that "user.home" seems to identify a path that is on a local drive and what I want is for it to identify the file from the class itself. Adding the fact that the package name is org.assn.generator, could I define the path as something like -
org.assn.generator. (or /) filename.xls? If so, are quotes needed anywhere?
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 39980256
seems to identify a path that is on a local drive
Yes. Questions:

a. Do you want to write to the file?
If yes, then
b. Why is that a problem?
c. Where else could/should it be?
0
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!

 
LVL 31

Author Comment

by:awking00
ID: 39982971
The file is static and will only be read from. I don't want to have multiple persons keeping a copy of the file on their local drives, but maintain the file within the class.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 39982985
but maintain the file within the class.
You should load it as a resource from the app's jar

Better to load your files/images as resources:
http://technojeeves.com/index.php/aliasjava1/78-loading-files-as-resources-in-java-with-netbeans
http://technojeeves.com/index.php/aliasjava1/80-loading-files-as-resources-in-java-with-eclipse
0
 
LVL 31

Author Comment

by:awking00
ID: 39992359
Perhaps I asked this question in too complex a fashion. I resolved the issue simply by copying the file to the root directory of the project then was able to use -
File file = new File("filename.xls") => without any reference to path, which is what I wanted
Then simply create the FileInputStream as new FileInputStream(file). I'm not sure how I should handle the points since I did sort of figured it out on my own but, as an EE regular, I feel uncomfortable not giving points. Any input in this regard is most welcome. Thanks to everyone for their efforts.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 39992396
 I resolved the issue simply by copying the file to the root directory of the project then was able to use - File file = new File("filename.xls") => without any reference to path, which is what I wanted
So, IOW you did exactly what i suggested in my first comment HERE ;)

The problem is that 'filename.xls' doesn't mean 'use the root of the project directory', it means 'use the current directory'. Depending on how the app is run, that could mean that it's not found. That would not be the case with my later suggestion after discovering that it's read-only.

http://technojeeves.com/index.php/aliasjava1/91-find-the-current-directory-in-java
0
 
LVL 31

Author Closing Comment

by:awking00
ID: 39992476
Sorry, I overlooked this earlier response entirely.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 39992762
:)
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
allStar challenge 1 41
json example 39 114
Modeling a class in java 5 32
Java JRE greater than 1.6 5 23
For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
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…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

757 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

22 Experts available now in Live!

Get 1:1 Help Now