Get abs path from the currently edited file in Eclipse - java

I have the same problem as the guy in this post:
Get the absolute path of the currently edited file in Eclipse
but I dont understand how to do it. In the post they say that I should use IResource.getRawLocation() but how to I get the IResource from the current file that I am viewing?
IFile file2 = (IFile)workBench.getActiveWorkbenchWindow().getActivePage().getActiveEditor().getEditorInput().getAdapter(IFile.class);
IPath path = file2.getRawLocation().makeAbsolute();
String ss = path.toString();
System.out.println(ss);
But this will still not give me the right path to the linked file, I get something like:
/path/to/plugin/path/to/relative/path/to/file

An IFile is-an IResource. You don't need to get one from another. Just call file2.getRawLocation(). If that's not working it's not because it's not an IResource.
Is the file in a jar/zip/any archive file? If so, it looks like that could be the correct absolute path.

Related

Relative to absolute path in java

I have have a file that I want to use in my project which is in the resources package
src.res
Following what was stated in this answer, I believe that my code is valid.
File fil = new File("/res/t2.nii");
// Prints C:\\res\\t2.nii
System.out.println(fil.getAbsolutePath());
The problem is that I that file is in my projects file not there, so I get an Exception.
How am I suppose to properly convert from relative path to absolute?
Try with directory first that will provide you absolute path of directory then use file.exists() method to check for file existence.
File fil = new File("res"); // no forward slash in the beginning
System.out.println(fil.getAbsolutePath()); // Absolute path of res folder
Find more variants of File Path & Operations
Must read Oracle Java Tutorial on What Is a Path? (And Other File System Facts)
A path is either relative or absolute.
An absolute path always contains the root element and the complete directory list required to locate the file.
For example, /res/images is an absolute path.
A relative path needs to be combined with another path in order to access a file.
For example, res/images is a relative path. Without more information, a program cannot reliably locate the res/images directory in the file system.
Since you are using a Java package, you must to use a class loader if you want to load a resource. e.g.:
URL url = ClassLoader.getSystemResource("res/t2.nii");
if (url != null) {
File file = new File(url.toURI());
System.out.println(file.getAbsolutePath());
}
You can notice that ClassLoader.getSystemResource("res/t2.nii") returns URL object for reading the resource, or null if the resource could not be found. The next line convertes the given URL into an abstract pathname.
See more in Preferred way of loading resources in Java.
validate with
if (fil.exists()) { }
before and check if it really exist. if not then you can get the current path with
System.getProperty("user.dir"));
to validate that you are starting fromt he proper path.
if you really want to access the path you shouldnt use absolut pathes / since it will as explained start from the root of your Harddisk.
you can get the absolut path of the res folder by using this what my poster was writte in the previous answer:
File fil = new File("res");
System.out.println(fil.getAbsolutePath());

Get path directory only and discard file in Java

How do we actually discard last file from java string and just get the file path directory?
Random Path input by user:
C:/my folder/tree/apple.exe
Desired output:
C:/my folder/tree/
closest solution i found is from here . The answer from this forum only display last string acquired not the rest of it. I want to display the rest of the string.
The easiest and most failsafe (read: cross-platform) solution is to create a File object from the path.
Like this:
File myFile = new File( "C:/my folder/tree/apple.exe" );
// Now get the path
String myDir = myFile.getParent();
Try that:
String path = "C:/my folder/tree/apple.exe";
path = path.substring(0, path.lastIndexOf("/")+1);

Can't find specified path when using .. in the middle?

I'm trying to write in a file with a path like this:
D:\abcd\efgh\..\ijkl\file.txt
So I have an File object with such a path, but in the line
FileOutputStream fos = new FileOutputStream(f);
I get this:
java.io.FileNotFoundException: ..\ijkl\file.txt (The system cannot find the path specified)
Does anybody know what's wrong here? Is there a possibility to resolve the path in an absolute path?
Initialisation of the File object:
File f = new File(strImagePath);
strImagePath is built out of different Strings and looks exactly like the path shown above.
Thanks!
According to your code java try to access the folder D:\abcd\ijkl\file.txt as you have place ..\ijkl\file.txt but in your system doesnot have any file on that path. Thus you are getting the error.
Edit: can you please, try using D:\\abcd\\efgh\\../efgh\\ijkl\\file.txt

Finding absolute path

I am looking at code new FileInputStream("config.properties").
I have the same file "config.properties" in multiple places in my project(doing windows file search) and I am now confused as to which one does this function call refer to. How do i get to know the absolute path of file?
I found this on the internet but this location doesnt look like the right answer.
"ClassName".class.getProtectionDomain().getCodeSource().getLocation().getPath() but this doesnt look it. Can you please correct it if I am wrong
You can use File:
File f = new File("config.properties");
System.out.println(f.getAbsolutePath());
The path returned will be deduced from the current working directory.
File f = new File("config.properties");
String dirPath = file.getParentFile().getAbsolutePath()

Get the absolute path of the currently edited file in Eclipse

I'd like to write a plugin that does something with the currently edited file in Eclipse. But I'm not sure how to properly get the file's full path.
This is what I do now:
IFile file = (IFile) window.getActivePage().getActiveEditor.getEditorInput().
getAdapter(IFile.class);
Now I have an IFile object, and I can retrieve it's path:
file.getFullPath().toOSString();
However this still only gives me the path relative to the workspace. How can I get the absolute path from that?
Looks like you want IResource.getRawLocation(). That returns an IPath, which also has a makeAbsolute() method if you want to be doubly sure you've got an absolute path.
I think a more Java friendly solution would be to do use the following:
IResource.getLocation().toFile()
This takes advantage of the IPath API (the getLocation() part) and will return a java.io.File instance. Of course the other answers will probably get you to where you want to be too.
On a tangential note, I find the IDE class (org.eclipse.ui.ide.IDE) a useful utility resource when it comes to editors.
The answer that worked for me (and I tested it!) was:
// Get the currently selected file from the editor
IWorkbenchPart workbenchPart = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getActivePart();
IFile file = (IFile) workbenchPart.getSite().getPage().getActiveEditor().getEditorInput().getAdapter(IFile.class);
if (file == null) throw new FileNotFoundException();
String path = file.getRawLocation().toOSString();
System.out.println("path: " + path);
I usually call IFile.getLocation() which returns an IPath and then call IPath.toOSString().
file.getLocation().toOSString()
IWorkspace ws = ResourcesPlugin.getWorkspace();
IProject project = ws.getRoot().getProject("*project_name*");
IPath location = new Path(editor.getTitleToolTip());
IFile file = project.getFile(location.lastSegment());
into file.getLocationURI() it's the absolute path
For me, this run ok.
IWorkspaceRoot workSpaceRoot = ResourcesPlugin.getWorkspace().getRoot();
File file = workSpaceRoot.getRawLocation().makeAbsolute().toFile();
file list from this location:
File[] files = file.listFiles();

Categories