How to get the right file path in Servlet program? - java

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");

Related

Accessing a network path and coverting it to a File path using Java

I have a network path which looks like this:
\\rennas01\\test\Project\\InputFiles
I need to use this network path as a abstract path for my File instance:
File src=new File(path);
How can I use this network path to look something like:
File src=new File("\\\\rennas01\\test\\Project\\InputFiles")
As it is a path to a network shared driver. File will not accept it as a correct parameter.
Thanks in advance!
In your example you escaped every backslash but you have forgotten the one at the beginning of the path:
// this path points to wrong location: \rennas01\test\Project\InputFiles
File src=new File("\\rennas01\<test\\Project\\InputFiles");
should actually be:
// this path points to desired location: \\rennas01\test\Project\InputFiles
File src=new File("\\\\rennas01\\test\\Project\\InputFiles");
This small correction should solve your problem.

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.

Getting the path of a txt file in a src folder so i can read from it into an array

I have a text file, that I am trying to convert to a String array (where each line is an element of the array).
I have found a code here that seems to do this perfectly, the only problem is, even when I use the relative path of the file, I get a FileNotFoundException.
The relative file path:
String path = "android.resource://"+getPackageName()+"/app/src/main/res/raw/"
+ getResources().getResourceEntryName(R.raw.txtAnswers);
When I try to use this path in a reader
BufferedReader reader = null;
try{
reader = new BufferedReader(new FileReader(path));
......
The path is underlined in red and says FileNotFoundException.
Perhaps it is expecting another type of path (not relative?) or did I get the path wrong?
The relative file path
That is not a file path.
Perhaps it is expecting another type of path
res/raw/... is a file on your hard drive. It is not a file on the device.
To access raw resources, use getResources() (called on any Context, like an Activity or Service) to get a Resources object. Then, call openRawResource(), passing in your resource ID, to get an InputStream on that resource's contents.
According to me the best way would be to past that txt file in assets folder of your source code.
To use that File just use the following snippet:
InputStream inputStream = getAssets().open("YOUR_TEXT_FILE");
It returns InputStream which further can be used to read the File & convert the data into a Array

error in using getAbsolutePath function in 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.

java Failed to create new file in windows 7?

I 'm trying to create new file in windows 7 using
file.createNewFile()
but the file is not created and I got the following exception
Message:
The system cannot find the path specified
Stack Trace:
[java.io.IOException: The system cannot find the path specified,
at java.io.WinNTFileSystem.createFileExclusively(Native Method),
at java.io.File.createNewFile(File.java:883),
at com.mercury.mtf.actions.file.CreateEmptyFileTask.execute(CreateEmptyFileTask.java:56),
at com.mercury.mtf.actions.file.CreateEmptyFileAction.execute(CreateEmptyFileAction.java:42),
at com.mercury.mtf.core.AbstractAction.run(AbstractAction.java:50),
at com.mercury.mtf.core.Unit.runUnitAction(Unit.java:347),
at com.mercury.mtf.core.Unit.executeUnitAction(Unit.java:176),
at com.mercury.mtf.core.Unit.run(Unit.java:121),
at com.mercury.mtf.core.execution.DefaultUnitExecutor.call(DefaultUnitExecutor.java:24),
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:303),
at java.util.concurrent.FutureTask.run(FutureTask.java:138),
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$301(ScheduledThreadPoolExecutor.java:98),
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:207),
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886),
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908),
at java.lang.Thread.run(Thread.java:619)]
I'm sure that the path exists, but I realized that the folder marked as read only. I tried to remove the read only flag but I can't get this to work.
Make sure your path separator character is proper.. You can use single forward slash or double back slashes. For example,
File f = new File("C:\\Documents and Settings\\thandasoru\\My Documents\\temp.txt");
f.createNewFile();
If the file is temporary you can use this function and you can forget all permissions problems:
File.createTempFile("prefix", "suffix")
Use File newFile=new File(folderName+chipItems[i]); rather than using File newFile=new File(folderName+chipItems[i], "w");. That will be OK. Avoid File Mode when you like to give functionality like Unix touch command.

Categories

Resources