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.
Related
I have created a .txt file in my Eclipse Java project, and I want to find out the path to it so I can use it for a Scanner. I do not want to find out the path on my local drive, as I will be planning to share the program to someone else, and they will have a different folder structure, rather a path that can be used on anybodies machine.
Here is the code:
this.file = new File("<insert path here>");
you can use :
= new File("Build Path"); (your .java file exist in your build path)
The build path is used for building your application. It contains all of your source files and all Java libraries that are required to compile the application.
In eclipse, the default behavior is for the Java system property user.dir to be set to the project directory. This is what dictates where the "root" of File operations is. So if you created a file test.txt in the root project directory, you should be able to access it with new File("test.txt").
However, as Andrew Thompson mentioned in his comment, the more correct method would be using embedded resources.
Try one of These:
1.
System.getProperty("user.dir");
2.
File currentDirFile = new File(".");
String helper = currentDirFile.getAbsolutePath();
String currentDir = helper.substring(0, helper.length() - currentDirFile.getCanonicalPath().length());//this line may need a try-catch
I have not tested it just found while googling
I tried to reach a special path in Ubuntu, relative to the current jar file.
In Windows it is working without any problem:
String jarPath = Configuration.class.getProtectionDomain().getCodeSource().getLocation().getPath();
File f = new File(jarPath+"/../../configurationFile.xml");
However, in Linux I always get the jar file but I cannot step back two directories to the configurationFile.xml
/some/directory/where/xml/is/located/xyz.jar/configurationFile.xml: Not a directory
However, if I do
pwd /some/directory/where/xml/is/located/xyz.jar/../../
it works without any problems.
What I am doing wrong here?
I cannot figure it out.
Use only directories in your path.
After you determined the path to your jar file, extract the path to its directory and use directories only.
I've got a project to do with 2 other classmates.
We used Dropbox to share the project so we can write from our houses (Isn't a very good choice, but it worked and was easier than using GitHub)
My question is now about sharing the object stream.
I want to put the file of the stream in same dropbox shared directory of the code.
BUT, when i initialize the file
File f = new File(PATH);
i must use the path of my computer (C:\User**Alessandro**\Dropbox)
As you can see it is linked to Alessandro, and so to my computer.
This clearly won't work on another PC.
How can tell the compiler to just look in the same directory of the source code/.class files?
You can use Class#getResource(java.lang.String) to obtain a URL corresponding to the location of the file relative to the classpath of the Java program:
URL url = getClass().getResource("/path/to/the/file");
File file = new File(url.getPath());
Note here that / is the root of your classpath, which is the top of the directory containing your class files. So your resource should be placed inside the classpath somewhere in order for it to work on your friend's computer.
Don't use absolute paths. Use relative paths like ./myfile.txt. Start the program with the project directory as the current dir. (This is the default in Eclipse.) But then you have to ensure that this both works for development and for production use.
As an alternative you can create a properties file and define the path there. Your code then only refers to a property name and each developer can adjust the configuration file. See Properties documentation.
I am trying to access a text file, output.txt which is inside a project folder. The image below shows the folder structure.
String file=".\\testFiles\\output.txt";
BufferedReader br=new BufferedReader(new FileReader(file));
When I try to access that file using the code above, I get the following exception.
java.io.FileNotFoundException: .\testFiles\output.txt (No such file or directory)
I have tried different file path formats but none have worked. I think the problem is with the file path format.
Thanks in advance.
If I remember correctly you can get a folder/file in the current directory like so:
File folder = new File("testFiles");
Then you can open the file by getting the absolutePath and creating a new file with it, like so:
File file = new File(folder.getAbsoluteFile() + File.separator + "output.txt");
I'm not sure but I think you can also do:
File file = new File("testFiles/output.txt");
I hope this helps :)
P.S. this is all untested so it might not work.
Judging by the fact that you have a webcontent folder i presume this is a web project, possibly packaged as a war? In this case what you will want to do is package the respective files along with the classes and access it with something like this:
Thread.currentThread().getContextClassLoader().getResourceAsStream("output.txt")
The code above will work if you add the testFiles folder as a source folder (this means it will get packaged with the classes and be available at runtime)
The good thing is that this way the path can stay relative, no need to go absolute
I believe that your problem is due to the fact that you rely on a relative path as your path starts with a dot which means that it will by relative to the user directory (value of the system property user.dir) so I believe that your user directory is not what you expect. What you could do to debug is simply this:
System.out.println(new File(file).getAbsolutePath());
Thanks to this approach you will be able to quickly know if the absolute path is correct.
You must declare your file as a new file:
File yourFile = new File("testFiles/output.txt");
This is a very simple question for many of you reading this, but it's quite new for me.
Here is a screenshot for my eclipse
When i run this program i get java.io.FileNotFoundException: queries.xml (The system cannot find the file specified) i tried ../../../queries.xml but that is also not working. I really don't understand when to use ../ because it means go 1 step back in dir, and in some cases it works, can anyone explain this? Also how can I refer to queries.xml here. Thanks
Note: I might even use this code on a linux box
I assume it is compiling your code into a build or classes folder, and running it from there...
Have you tried the traditional Java way for doing this:
def query = new XmlSlurper().parse( GroovySlurping.class.getResourceAsStream( '/queries.xml' ) )
Assuming the build step is copying the xml into the build folder, I believe that should work
I don't use Eclipse though, so can't be 100% sure...
Try
file = new File("src/org/ars/groovy/queries.xml");
To check the actual working directory of eclipse you can use
File f = new File(".");
System.out.println(f.getAbsolutePath());
You could try using a property file to store the path of the xml files.
This way you can place the xml files in any location, and simply change the property file.
This will not require a change/recompilation of code.
This would mean you will only need to hardcode the path of the property file.
If you prefer not hardcoding the path of the property file, then you could pass it as an argument during startup in your server setup file. (in tomcat web.xml). Every server will have an equivalent setup file where you could specify the path of the property file.
Alternatively you could specify the path of the xml in this same file, if you don't want to use property files.
This link will show you an example of reading from property files.
http://www.zparacha.com/how-to-read-properties-file-in-java/