I am working on a java project and I was wondering if it is possible to write a code that can create a txt file directly into the bin folder (for example the bin folder for eclipse, where I can use getClass() to access the txt files) so that the user will not be able to see the created txt files when using the program.
You can surely do this. But think again. User does not run program from Eclipse. User typically runs program packed in jar, so all class files are not in filesystem but into jar. Moreover the user even probably does not have rights to write into file system except special folders.
Bottom line: you you want to create application that stores some run-time data in file system you should either user user home or temporary directory or use java.util.prefs.Preferences that provide platform independent way to save and retrieve data using file system in unix and registry in windows.
If you choose to create file yourself you can retrieve use home and temporary directory using system properties user.home and java.io.tmpdir.
Related
So I've created just a simple application which I'm using to apply for a highschool internship. It was built using Eclipse.
I initially exported into a runnable .jar file, but the location I initially saved files, ("src/fileDirectories") didn't work on export.I then set the location to "./fileDirectories") which works and the app can actually run, however the .jar file creates the folder directory in the same folder as the .jar file.
This isn't too bad, as I can create a folder with the .jar and the saved files in it, which is fine, but similar to images, I'm wondering if there is a way to save .txt files in a directory to the .jar file so everything works with just the .jar application.
Assuming the files you want to access are static, and are not changed by the application, you can do this using the classpath.
When you access a file using new File("path"), Java will look for it in the file system, relative to the working directory (i.e. where the application was launched from.
An alternative way to access files is to look them up from the classpath - this is the list of locations Java searches for resources. It includes, among other things, the top level of your JAR archive. You can access this as follows:
this.getClass().getResourceAsStream("/my_file.txt")
Build tools will generally have preconfigured directories (e.g. src/main/resources) which get copied from your source tree into the JAR so they can be accessed in this way.
This is great if you have some static resources or config which you want to access at runtime, but don't want to have to pass around with your application.
However, if what you want is a working folder for files which your application will make changes to or create new instances of, like user documents, you don't want this to be editing the JAR - you'll need to make a separate folder for these.
I've been working on a Java program for a company over the summer and it is nearing completion. Being something of a novice programmer, I've never produced a program for use on other systems using files I've created. My program reads and writes to Excel using Apache Poi, and currently the Excel file being used lies in a specific directory specified by the program. The program also uses some images that lie in directories specified by the code.
How could I make this program runnable on other systems? Would it be possible to have the program create an Excel document whenever it is "installed" on another system?
Currently I'm using Eclipse and the systems are windows 7.
You can pack your application in a .jar, actually a zip file with inside .class files and resource files. Those resource files, like your images, can be taken with getClass().getResource("path/x.png") or getResourceAsStream. The path is either package relative or absolute: "/abc/def.jpg".
These resources must be read-only (as they are inside the .jar). However you may store an Excel template as resource, and copy that to the user's directory.
System.getProperty("user.home")
Will give the user's directory where you may copy your resource to.
Path appWorkspace = Paths.get(System.getProperty("user.home"), "myapp");
Files.createDirectories(appWorkspace);
Path xlsx = appWorkspace.resolve("untitled.xlsx");
Files.copy(getClass().getResourceAsStream("/data/empty.xlsx"),
xlsx);
I'm create an application that is going to be run on Windows, Mac OX and Linux. I have a properties file storing user settings which need to be read and changed on the fly.
A JAR file is compressed and is not meant to be changed on the fly which means I should write to an external file.
I'm using :
new FileInputStream("database")
new FileOutputStream("database")
How do I create a URL which is going to be consistent throughout all three operating systems. The JAR is run as an application on the desktop and I would like the file to be stored somewhere discrete.
I've tried reading from a local file in the same package as this class :
this.getClass().getResourceAsStream("database")
This works however I can't seem to create an output stream to write to the same file but this would be breaking the rule of changing a JAR file on the fly.
There are plenty of good reasons for which you should not do this, off the top of my head:
GetResourceAsStream does not necessarily get the file from the JAR itself. You coincidentally got it from there because the Jar was the first or the only element in the class path.
Writing a file in your own Jar could break the JAR signature if you are going to sign it.
The database could need to be backed up; in this case you may want to back it up separate from the code (the code could be upgraded when restoring the database).
Hope this helps.
I am writing Java code to generate .dat and .idx files, which I can do.
The files are saved to my workspace folder which I set up as a default when I started Eclipse.
Now I want to save these files and only these files I create to another directory on my server.
Is there code that can do this or do I have to change the settings in Eclipse to do this?
Its a easy thing. Check below if this is what you want. Use
File file = new File("C:\\results\\results.txt");
C:\results\ is your outside directory.
No you can definitely do it programmatically. When specifying the path use something like:
"/c/users/someperson/desktop/blah.dat"
I have a small java app that updates files in a set location.
the app reads from a UpdateFiles folder, moves the files in this folder to the appropriate folder. the app works. what i want to know is how should i set my path if i want the app to always try and look for the UpdateFiles folder in same location that the app is? for example if the app is executed from desktop i want it to look for the UpdateFiles folder in desktop, same for any other location. The app runs on windows and AIX/linux enviroments
thank you / kind regards
You can access the system property that contains the current working directory:
String currentDirectory = System.getProperty("user.dir");
You can determine the location of the jar file, but this is not the same as the current working directory (the directory you get if you don't specify a path)
However the location of the jar is rarely a good place to store files and you don't want to mix released programs with files as this make upgrading your software much more complex.
I suggest using the user's home directory System.getProperties("user.home"); or a sub-directory This doesn't change if you upgrade you software is should always be writable.
You have to start your app from the dir where UpdateFiles is located, if necessary you can create a bat file (for Windows) and run it from any location
cd c:\path_to_UpdateFiles
java MyApp
do something similar in Linux