Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

create a file object without a path

Posted on 2014-04-04
12
Medium Priority
?
245 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 2000 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
Build and deliver software with DevOps

A digital transformation requires faster time to market, shorter software development lifecycles, and the ability to adapt rapidly to changing customer demands. DevOps provides the solution.

 
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: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Suggested Courses

660 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