Solved

create a file object without a path

Posted on 2014-04-04
12
238 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 32

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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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 32

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
 
LVL 32

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 32

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 32

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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
even odd program using while loop 3 72
servlet example 17 59
SequenceInputStream example 3 21
Is Applet the way to go for my drag and drop system? 8 33
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
This video teaches viewers about errors in exception handling.

713 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