Solved

create a file object without a path

Posted on 2014-04-04
12
239 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Technology Partners: 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

Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

Question has a verified solution.

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

Suggested Solutions

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…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
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…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Suggested Courses

738 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