error in using getAbsolutePath function in java - java

My xml file is place in this path "C/users/input/abc.xml".
I am executing code from "c/users/prg/abc.java"
Using getAbsolutePath function on Xml file object but i am getting this"c/users/prg/abc.xml" as result.
i have already tried using the below code:
File file = new File("C:\\users\\Input\\abc.xml");
String absPath = file.getAbsolutePath();
System.out.print(absPath);
output: C\users\prg\abc.xml
is it Classpath issue? or Am i doing something wrong?

The absolute path is like below
C:\users\Input\abc.xml
Once again rebuild ur application. What your expecting from us.
Regards,
Sekhar

I Think the error is occurring due to wrong class path.
Check the class path if it is right and let me know.

Related

How do I get the folder name from a String containing the Absolute file path in android?

Path name is : /storage/emulated/0/Xender/video/MyVideo.mp4
I am able to get last file name [MyVideo.mp4] from path using
String path="/storage/emulated/0/Xender/video/MyVideo.mp4";
String filename=path.substring(path.lastIndexOf("/")+1);
https://stackoverflow.com/a/26570321/5035015
Now i want to extract path [/storage/emulated/0/Xender/video] from this path.
I have one use of this path in my code so that i want to do this like this.
How can i do this?
Any help will be appreciated.
new File(path).getParentFile().getName() should work.
With regards to your current code, don't implement your own path parser. Use File.
Also note that this has nothing to do with Android specifically; this is a general Java question.

How to get the right file path in Servlet program?

I am trying to read xml and txt file using relative path,
I tried getServletContext().getContextPath();
but it gets the path in a wrong way
for example
My file path is :
D:\dev\workspace\Simulater\src\resources\Map.xml
Now when I apply,
System.out.println(getServletContext().getContextPath());
I get as an output:
/Simulater
and when i apply :
File myTestFile= new File(Api.CONTEXT_PATH+fileName);
String path = myTestFile.getAbsoluteFile().toString();
System.out.println(path);
i get D:\Simulater\src\resources\Map.xml
an it is a wrong path since it dose not contains
:\dev\workspace\
it seams like java takes the project name and add the driver that contains it
so dose any one can provide any help to get the right path
thanx
use getServletContext().getRealPath("/") to get full path D:\dev\workspace\Simulater\src\resources\ then you can read file by giving this full path and file name.
To read a file you nead to open an InputStream, as your file is in your classpath you can open the stream with the following statement :
InputStream is = this.getClass().getResourceAsStream("/Map.xml");

Java - Clean file path

I want to clean the path I use in my App. The path can be modified and sometimes I got something like that:
C:/users/Username/Desktop/\..\..\..\Windows\Web\..\..\Program Files\..\Program Files\..\Python27\
But I would like to have something like:
C:\Python27\
That's an example!
How can I clean the path to get only the necessary part?
Thanks.
If fileName is your filename string, then something like:
String cleanedFilename = new File(fileName).getCanonicalPath();
should do it...
Se also the API description.
Here is the code I have just tried.
new File("c:/temp/..").getCanonicalPath();
It returns 'C:\', that is right. The parent of c:/temp is indeed c:\
You could try using the File.getCanonicalPath() method:
File file = new File("my/init/path");
String path = file.getCanonicalPath();
I haven't test though, tell us back!
EDIT:
#MathiasSchwarz is right, use getCanonicalPath() instead of getAbsolutePath() (link)

FileInputStream and FileNotFound Exception

I am trying to retrieve a jrxml file in a relative path using the following java code:
String jasperFileName = "/web/WEB-INF/reports/MemberOrderListReport.jrxml";
File report = new File(jasperFileName);
FileInputStream fis = new FileInputStream(report);
However, most probably I didn't succeed in defining the relative path and get an java.io.FileNotFoundException: error during the execution.
Since I am not so experienced in Java I/O operations, I didn't solve my problem. Any helps or ideas are welcomed.
You're trying to treat the jrxml file as an object on the file-system, but that's not applicable inside a web application.
You don't know how or where your application will be deployed, so you can't point a File at it.
Instead you want to use getResourceAsStream from the ServletContext. Something like:
String resourceName = "/WEB-INF/reports/MemberOrderListReport.jrxml"
InputStream is = getServletContext().getResourceAsStream(resourceName);
is what you're after.
You should place 'MemberOrderListReport.jrxml' in classpath, such as it being included in a jar placed in web-inf\lib or as a file in web-inf\classes.
The you can read the file using the following code:
InputStream is=YourClass.class.getClassLoader().getResourceAsStream("MemberOrderListReport.jrxml");
String jasperFileName = "/web/WEB-INF/reports/MemberOrderListReport.jrxml";
Simple. You don't have a /web/WEB-INF/reports/MemoberOrderListReport.jrxml file on your computer.
You are clearly executing in a web-app environment and expecting the system to automatically resolve that in the context of the web-app container. It doesn't. That's what getRealPath() and friends are for.
check that your relative base path is that one you think is:
File f = new File("test.txt");
System.out.println(f.getAbsoluteFile());
I've seen this kind of problem many times, and the answer is always the same...
The problem is the file path isn't what you think it is. To figure it out, simply add this line after creating the File:
System.out.println(report.getAbsolutePath());
Look at the output and you immediately see what the problem is.

getResource with parent directory reference

I have a java app where I'm trying to load a text file that will be included in the jar.
When I do getClass().getResource("/a/b/c/"), it's able to create the URL for that path and I can print it out and everything looks fine.
However, if I try getClass().getResource(/a/b/../"), then I get a null URL back.
It seems to not like the .. in the path. Anyone see what I'm doing wrong? I can post more code if it would be helpful.
The normalize() methods (there are four of them) in the FilenameUtils class could help you. It's in the Apache Commons IO library.
final String name = "/a/b/../";
final String normalizedName = FilenameUtils.normalize(name, true); // "/a/"
getClass().getResource(normalizedName);
The path you specify in getResource() is not a file system path and can not be resolved canonically in the same way as paths are resolved by File object (and its ilk). Can I take it that you are trying to read a resource relative to another path?

Categories

Resources