Where is java saving files on Linux? - java

so I have this problem:
I need to know where is Java saving the files when you create them with new, like this File file = new File ("file.txt"); on Linux?

Linux on not File file = new File ("file.txt") does not create a file on the file system. File is just a file path holder. You need to call file.createNewFile to create a file. Relative paths like file.txt are resolved against the current user directory, typically the directory in which the Java virtual machine was invoked

Assuming the file is actually being created by additional code (as Evgeniy mentioned), you could try checking to see if your current working directory isn't what you expect it to be. To find that directory you could try:
String cwd = System.getProperty("user.dir"));
System.out.println("Current working directory: " + cwd);
or just
System.out.println ("Path to file: " + file.getAbsolutePath());
To see where it should end up.

same place like in Windows: in the current directory

Your mentioned code doesn't create new file physically on drive, but logically yes. If you alter the code to create a new file like file.createNewFile then the directory where you executed the code, a file would be created there.

Related

How can I find a file while in my java code

I am stuck on a how to robustly find a file path from my Java program. If I am using Linux, I can launch my program from my home folder, and then I can't say find my file at ./myProgram/myFile. Is there a good way to find my file no matter what directory my console is in?
If you are trying to access the file using the path:
./myProgram/myFile
in your program, but you aren't executing the program from the myProgram directory, then you're Java code won't see the file. Try providing it with the full path instead of the relative path. If myProgram is a directory found in your user's Documents directory then a full path would likely look something like this:
/home/*username*/Documents/myProgram/myFile
You could also build in functionality that lets you select the file by navigating through the directories and listing the files. The would provide the user with options to choose which file to utilize in the program.
You can just as well cd to the myProgram directory before executing the file and then the relative path ./myProgram/myFile should work.
Hope that helps.
Try this:
File f = new File(System.getProperty("user.home") + System.getProperty("file.separator") + "myFile.txt");

How to find path of my folder

We are working on project.
Every colleague have different folder for the install
In my case the folder of my files is in
C:\\p4_3202\\CAR\\car.rt.appl\\dev\\car.components\\cars\\res\\car.rt.components.cars\\resources\\js;
for other colleague it could be in
C:\\my_3202\\CAR2\\car.rt.appl\\dev\\car.components\\cars\\res\\car.rt.components.cars\\resources\\js;
it is depends how you config your perforce.
I need to read files from my folder but i don't know the name of the folder ( as i explained it could be different )
File folderFile = new File(folder);
How i can find the location of my folder ? ( c:\p4\......test.js )
I tried with
System.getProperty("sun.java.command");
System.getProperty("user.home")
but it didn't give me the path of my folder
I would use a system property for each user. So all users tell where perforce is installed (might already exist a property for this, look at the docs).
This could then be read by your code like:
System.getenv().get("PROP");
On a unix/Linux system you can set the property in a shell/environment variable using:
export PROP=thepath
Windows was a long time ago for me but if I remember correctly its somewhere under System on control panel :)
Update:
http://www.itechtalk.com/thread3595.html
file.getAbsolutePath() or file.getAbsoluteFile()
String path = new java.io.File(".").getCanonicalPath();
If it are files for reading only, being stored inside the final produced jar, then use URL url = getClass().getResource("/.../...") or InputStream in = getClass().getResourceAsStream("/.../...").
That uses a path inside the jar/class path. Using only the jar would never do with File.

Java - read file from directory for jar

I have an application that creates a temporary mp3-file and puts it in a directory like C:\
File tempfile = File.createTempFile("something", ".mp3", new File("C:\\));
I'm able to read it by just using that same tempfile again.
Everything works fine in the Eclipse IDE.
But when I export my project for as a Runnable jar, my files are still being made correctly (I can play them with some normal music player like iTunes) but I can't seem to read them anymore in my application.
I found out that I need to use something like getClass().getResource("/relative/path/in/jar.mp3") for using resource files that are in the jar. But this doesn't seem to work if I want to select a file from a certain location in my file system like C:\something.mp3
Can somebody help me on this one?
It seems you dont have file name of the temp files . When you was running your program in eclipse that instance was creating a processing files, but after you made a runable you are not able to read those file that instance in eclipse created, You runable file can create its own temp file and can process them,
To make temp files globe put there (path + name ) entries in some db or property file
For example of you will create a temp file from the blow code
File tempfile = File.createTempFile("out", ".txt", new File("D:\\"));
FileWriter fstream = new FileWriter(tempfile);//write in file
out = new BufferedWriter(fstream);
the out will not be out.txt file it will be
out6654748541383250156.txt // it mean a randum number will be append with file
and you code in runable jar is no able to find these temp files
getClass().getResource() only reads resources that are on your classpath. The path that is passed to getResource() is, in fact, a path relative to any paths on your current classpath. This sounds a bit confusing, so I'll give an example:
If your classpath includes a directory C:\development\resources, you would be able to load any file under this directory using getResource(). For example, there is a file C:\development\resources\mp3\song.mp3. You could load this file by calling
getClass().getResource("mp3/song.mp3");
Bottom line: if you want to read files using getResource(), you will need those files to be on your classpath.
For loading from both privileged JARs and the file system, I have had to use two different mechanisms:
getClass().getClassLoader().getResource(path), and if that returns null,
new File(path).toURI().toURL();
You could turn this into a ResourceResolver strategy that uses the classpath method and one or more file methods (perhaps using different base paths).

Read File on Networkshare

I am running my Application from a network-share. For example: "\server\startProgramm.bat"
The Code in my startProgramm.bat:
java -jar %~dp0\app.jar
I need to open some config files. It is working local if i try to open them with:
new File("").getAbsolutePath() + "\\" + filename
but not on my Network share.
The config-files are in a subdir of the dir where my jar and bat files are.
new File("\\\\servername\\sharedDirectoryOnServer\\fileOnServer");
"Ok, this worked, but is there something like CurrentWorkDir?"
The current working directory is (by default) the one your program runs in. You can not change it within java (see here:
Changing the current working directory in Java?
),
the best practice is to just concatenate the server-path and file-name; but i think (not sure) it is also possible to start your program from the server (java \\server\\path\\myprog where there's a myprog.class in that directory) and make the working directory default to that path.

How to scan a file in a different directory in java?

How do you scan a file with java that isn't in the directory the java file is in?
For example: The java file is located at "C:\Files\JavaFiles\test.java" However, the file I want to scan is located at "C:\Data\DataPacket99\data.txt"
Note: I've already tried putting another java file in the "C:\Data" directory and using the test.java file as a class, but it doesn't work. It still tries to scan from the "C:\Files\JavaFiles" Directory.
By using an absolute path instead of a relative.
File file = new File("C:\\Data\\DataPacket99\\data.txt");
Then you can write code that accesses that file object, using a InputStream or similar.
You need to use absolute paths in java.io stuff. Thus not new File("data.txt"), but new File("C:/Data/DataPacket99/data.txt"). Otherwise it will be relative to the current working directory which may not per-se be the same in all environments or the one you'd expect.
You should be using an absolute path instead of a relative path.
You could use File file = new File("C:/Data/DataPacket99/data.txt"); but it might make your life easier in the future to use a file chooser dialog if at any point the user will have to enter a file path.
I would try this:
File file = new File("../../Data/DataPacket99/data.txt");

Categories

Resources