As the title says, the java.io.tmpdir property in Java is really nice, and I can't find an equivalent for C. I'm writing for Windows and in Visual Studio. I don't want to just use something like C:\Temp because in later versions of windows the appropriate directory could be something like C:\Users\[UserName]\AppData\Local\Temp and a way to retrieve this would be much easier. I want a directory and not just a temp file because I'm downloading and executing a file from the internet.
As a side note, if anyone can give me tips about how to better google for C stuff it would be much appreciated :)
If you're using the Win32 API, you can use GetTempPath().
It is not quite what you were asking, but in the C standard library:
tmpnam_r will create a filename
string
tmpfile will create and open a
file (returning a FILE*)
See: http://www.gnu.org/s/libc/manual/html_node/Temporary-Files.html for more possibilities
For the directory itself, you can either get the dirname of the filename generated by the above functions, or in Windows you can get the environmental variable TEMP, and on Unix-likes you can either get the variable TMPDIR or use /tmp if TMPDIR is not set
Related
I'd like to pass a file path argument to my application in a relative form, e.g ~/test.conf or ../test.conf, but i can't get a proper full file path, though i've tried it with old java.io and new java.nio Files/Paths. Is there a general way to get a resolved file path without large amount of code? It would be fine for the solution to work only in unix envs like OsX or Debian.
Update
With a provided argument like ~/test.conf
in case of getAbsolutePath it returns a path with a prefixed current folder - /Users/currentUser/Projects/Personal/TestProject/~/text.conf. Canonical path returns the same.
The hard bit here is dealing with POSIX home directories, and sure you deal with ~otheruser/dir/test.conf too (if you want to do it properly).
Luckily that's covered in How to handle ~ in file paths.
TL;DR - use something like:
path.replace("~/", System.getProperty("user.home") + "/");
Once you've done that, and as others have commented, you can just use standard java.io methods (including getCanonicalPath()).
I was making a Java game that stores options and the LWJGL jars in the standard application data storage directory (I think that's C:\Users\user\AppData\roaming\application on Windows, ~/.application on Linux, and ~/Library/Preferences on Mac). Currently I just use System.getProperty(os.name) and have an if-else to select the right file path (which uses system.getProperty(user.home) and appends the correct file path to it). However, this might not work on some weird OSs because the if-else won't find Windows, Mac, or Linux in the OS name, and this also seems to be a very bad way to do this in general (also the if-else assumes the OS is Linux if it isn't Windows or Mac). I tried decompiling Minecraft, which stores the app data and the LWJGL jars in the correct path, but the code is obfuscated. Is there any better way for me to do this, or should I stick with an if-else on os.name?
Java Preferences API was created specifically for such purpose.
For configuration preferences, there is the Java Preferences API.
For other files, I have not found a solution to this. The best solution, at present, is probably to dispatch on the OS type with a fallback based on the user.home property.
On Linux/Unix (not Mac), you might want to honor the XDG Base Directory specification.
Shouldn't a path like this work everywhere, i.e. without the if-else-if:
String userHome = System.getProperty("user.home");
if (!userHome.endsWith("/")) {
userHome += "/";
}
String targetPath = userHome + ".myapp/settings.properties";
My java applications is almost complete and now I am giving it a finishing touch. Although certain code requires to read data from specific directories on the computer. On my machine I did it in the following way:
FileOutputStream fos=new FileOutputStream(C:/User/AnirudhVarma/Document/Appname/foldername/file,true);
But on the users machine how do I ensure that it creates it in the following directory?
C:/enduser/appname/folder/file
Hard coding folder structure in code is very bad idea.
One way to handle this is:
You need to have a properties file something like that where user need to enter path to required folder. Mention this as requirement in install docs.
In your code you need to make sure folder has required content before proceeding. If not valid folder, just show error message with proper information to setup the directory path.
System.getProperty("user.dir") will return C:/User/AnirudhVarma on you machine. From there, I'd suggest you need to consider the platform. Under windows, you should store this kind of information into the AppData folder.
So you would end up with something like System.getProperty("user.dir") + File.seperator + "AppData/Appname/foldername/file"
Under MacOS, I believe the convention is to use .AppName instead, something like System.getProperty("user.dir") + File.seperator + ".Appname/foldername/file"
How would I access a picture in a different folder in Java? I have a series of pictures and they change based on user input, which is what x is for.
picture.setIcon(new ImageIcon("\\resources\\icons\\pictures\\"+x+".png"));
The images are located (from the .class files) in resources/icons/pictures, but the above code doesn't work. The value of x isn't the problem since it works as it should. Am I calling the pictures the right way?
Am I calling the pictures the right way?
Probably not. If they are (embedded) application resources they will typically be in a Jar and unavailable via the String based constructor of the ImageIcon (which expects the String equates to a File path).
For an embedded resources, access them by URL.
URL urlToImg = this.getClass().
getResource("/resources/icons/pictures/"+x+".png");
picture.setIcon(new ImageIcon(urlToImg));
You should be using forward slashes instead of backslashes I believe.
new ImageIcon("/resources/icons/pictures/"+x+".png")
This is the standard Java cross-platform way of denoting resource file paths. It still works on Windows - the Java runtime library handles the transaltion for you.
The images are located (from the .class files) in resources/icons/pictures
That's a problem. The system isn't interested in where the class file is, but from where you invoke a program.
Specifying a resource folder via command line,
java -jar myJar.jar C:\\home\\of\\the\\images
or via a property
java -jar myJar.jar -DImageHome=/foo/bar/images
or from a properties file is most flexible.
If you like to put the images into the jar, use Andrews suggestion, getClass ().getRessource ("...");
Btw: I know for sure, that forward slashes are portable. Backslashes, afaik, aren't.
I'm aware you can call
Runtime.getRuntime().exec(file);
and get an exception if it is not executable, however this is unsafe since running an executable can have side effects.
I guess just checking the extension is enough on Windows, but is there a way I can read the executable bit on *nix file systems?
What is the best way to find out if an file is executable in the OS?
See java.io.File.canExecute()
The class java.io.File has method canExecute().
You can use Files class.
It has this method static boolean isExecutable(Path path).
If you have just a String of path then you can get Path using Paths class. Like this
Path path = Paths.get(pathToFile);