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

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

Related

Get resource path of a file in java using Path class

I'm new to Java, someone please let me know how to get resource path of a file instead of using entire path. Below is the code I've.
Path file = Paths.get("/home/Documents/src/test/resources/files/sample.txt");
I need something like getResource("files/sample.txt").
You need this:
URI uri = getClass().getClassLoader().getResource("/files/sample.txt").toURI();
Path path = Paths.get(uri);
i think you are searching for something like :
Path resolve(String other)
please look at javadoc here

How to load the class with full absolute path

This code works only for the class in the same package. I need to load the file from absolute path like c:/home/lpl/asm/hello.class
Any one please help me do this
InputStream in=ASMHelloWorld.class.getResourceAsStream("/aasm/ClassModificationDemo.class");
ClassReader classReader=new ClassReader(in);
To load a file from an absolute path:
String path = "c:/home/lpl/asm/hello.class";
InputStream in = new FileInputStream(path);
ClassReader classReader = new ClassReader(in);
Obviously hardcoding the path like this, would seriously restrict portability, so the path should be obtained from a command line parameter, user input, a properties file etc.
Why would you want this? If you do something like that you disable the ability to use the program on a different computer, unless you recreate that folder structure.
Seems like a duplicate question to me, just with a different file: Use Absolute path for ClassLoader getResourceAsStream()

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()

Getting error saying file won't open in Java...any idea why this is happening?

I think I am really close, but I am unable to open a file I have called LocalNews.txt. Error says can't find file specified.
String y = "LocalNews.txt";
FileInputStream fstream = new FileInputStream(y);
DataInputStream in = new DataInputStream(fstream);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
Name of file is LocalNews.txt in library called News....anyone know why the file will not open?
The file is in the same Java Project that I am working on.
Error: LocalNews.txt (The system cannot find the file specified)
Project is named Bst, package is src in subPackage newsFinder, and library that the text files are stored in is called News.
Found out it was looking in
C:\EclipseIndigoWorkspace1\Bst\bin\LocalNews.txt
But I want it to look in (I believe)
C:\EclipseIndigoWorkspace1\Bst\News\LocalNews.txt
But if I make the above url a string, I get an error.
String y = "LocalNews.txt";
instead use
String y = "path from root/LocalNews.txt"; //I mean the complete path of the file
Your program can probably not find the file because it is looking in another folder.
Try using a absolute path like
String y = "c:\\temp\\LocalNews.txt";
By 'library called News' I assume you mean a jar file like News.jar which is on the classpath and contains the LocalNews.txt file you need. If this is the case, then you can get an InputStream for it by calling:
InputStream is = Thread.currentThread().getContextClassLoader()
.getResourceAsStream("LocalNews.txt");
Use
System.out.println(System.getProperty("user.dir") );
to find out what your current directory is. Then you'll know for sure whether your file is in the current directory or not. If it is not, then you have to specify the path so that it looks in the right directory.
Also, try this -
File file = new File (y);
System.out.println(file.getCanonicalPath());
This will tell you the exact path of your file on the system, provided your file is in the current directory. If it does not, then you know your file is not in the current directory.

Get abs path from the currently edited file in Eclipse

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.

Categories