2

I would like to know how to save a file in the current directory. For example, let's say that the software application creates a text file called "Information.txt", and the software application allows this text file to be edited. If I edit the text in this text file and save it, I would like to be able to copy the project onto a flash drive and use it on another computer, still having access to the "Information.txt" file.

Basically, what I want to be able to do is save a file in the same directory as the project. What filepath would I use to do this. And, if I was doing this with a database file, how might I go about saving the database in the current directory?

Thank you in advance for any helpful answers.

Regards,

Ryan Shukis

Ryan
  • 511
  • 1
  • 8
  • 18
  • This is trickier than you think because the concept of "current directory" may not be defined, or may not mean what you think. I _believe_ you actually want the file saved in the directory where the application JAR file lives, which may or may not be the "current directory". Please clarify. – Jim Garrison Nov 01 '14 at 22:41
  • This _may_ be a duplicate of http://stackoverflow.com/q/320542/18157 but until the OP clarifies I can't really close as a duplicate. – Jim Garrison Nov 01 '14 at 22:45
  • Okay, I would like to save a database inside of the folder that the JAR is running in. That would be perfect. So, how do I figure out exactly what directory the currently running JAR file is located in? – Ryan Nov 02 '14 at 00:59
  • There's no such thing as _"the folder that the JAR is running in"_. The jar file itself can exist in a specific folder. The process that is executing the JVM and has loaded the jar can have a current directory, which probably is not the same as the jar's location. Then, if the jar was loaded across the network it may not reside on the machine on which it's running at all. The best you can do is provide a command-line option to TELL the program where to store its database on any given machine. – Jim Garrison Nov 04 '14 at 04:29
  • With that in mind, the problem I run in to is that the databases are stored on each machine LOCALLY, so the application will only pull information that is stored on that computer, rather than for the entire network of computers. – Ryan Nov 04 '14 at 04:54
  • Assuming that's what you want, you should make the user tell you (once) the desired location of the database, and use the Preferences API to store that. Or store it by default in the user's home directory (`user.home` system property). – Jim Garrison Nov 04 '14 at 04:56

1 Answers1

1

Assuming you know how to write to a file you can use the following:

File file = new File(System.getProperty("user.dir") + yourPath);
FileOutputStream fos = new FileOutputStream(file);

The System.getProperty("user.dir") returns the current working directory.

nullByteMe
  • 6,141
  • 13
  • 62
  • 99
  • So, I would be able to use this to save a database as well, correct? Let's change course a little bit and say that, instead of a text file, I want to save a database file. How would I go about doing that? – Ryan Nov 02 '14 at 01:00
  • @Ryan I don't see why not – nullByteMe Nov 02 '14 at 15:25
  • How would I do that though? – Ryan Nov 02 '14 at 15:31
  • @Ryan writing data is writing data. If you're familiar with the FileOutputStream there should be no difference in how you're trying to write to disk. – nullByteMe Nov 02 '14 at 17:37
  • But when I initiate the database, I have to put a URL or filepath in the setup. What filepath would I use? – Ryan Nov 02 '14 at 19:21
  • @Ryan what path is the database looking for? – nullByteMe Nov 02 '14 at 19:28
  • When I use NetBeans to add a new database, I have to go through the setup. Or, do I even need to set up the database through NetBeans "new database setup"? If not, I would just initiate it in the code, and it would be created right in the folder that contains the JAR file. But, I need to know what folder the JAR file is in. How do I figure that out? I basically just want it to say "JAR file is located in the 'HERE' folder, so place the database in the 'HERE' folder as well. How do I go about that? – Ryan Nov 02 '14 at 19:45