Solved

create a file object without a path

Posted on 2014-04-04
12
237 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
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

 
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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
runtime exception 2 41
Delphi Firemonkey: if the Sms contain special characters it won't send it 3 43
Tagging and Merging on Branch 1 29
junit as external jar or library 7 22
For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
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:

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