I am trying to d file = new file (location)
while location of file is absolute path with somethign like this : \\test\hold\REPO/TEST/Letter/123.pdf
I am getting file not found exception while files are there in this path. what could be going wrong? can i have path with both forward and backward slash?

- 5,569
- 1
- 24
- 45

- 11
- 1
-
Try to list all the files in the path using list method of File class to check whether file is actually present in the path or not ? If file exists then see the case of file name, even extension ? Problem is either the program is not taking the correct path or there is a mismatch between file name and what you have given in code. – Amit Bhati Aug 16 '15 at 03:49
-
Why are slashes different? – Mikhail Krutov Aug 16 '15 at 03:50
-
Please look into : `File.separator` variable. This answer might help you too : http://stackoverflow.com/questions/5971964/file-separator-or-file-pathseparator – Gyan Aug 17 '15 at 07:03
5 Answers
String separator = System.getProperty("file.separator"); So location can be rewritten to location=separator+"test"+separator+"hold"+separator +"REPO"+separator "TEST"+separator+"Letter"+separator+"123.pdf"; In this case no need to think about underlying OS

- 526
- 9
- 24
You need two slashes in a string literal to represent one in the filename. Try
"\\\\test\\hold\\REPO/TEST/Letter/123.pdf"
or better still
"//test/hold/REPO/TEST/Letter/123.pdf"
There is never a need to use a backslash in a filename in Java.

- 305,947
- 44
- 307
- 483
You can write your code without single backward slashes.if you want to use back-word slashes use \\ instead of \ .A single backward slashes create a problem if you put it inside the String literal.So you can write you code in multiple ways to avoid your exceptions.
1) File f=new File("\\test/hold/REPO/TEST/Letter/123.pdf");
2) File f=new File("\\test\\hold\\REPO/TEST/Letter/123.pdf");
3) File f=new File("\\test\\hold\\REPO\\TEST\\Letter\\123.pdf");
4) File f=new File("/test/hold/REPO/TEST/Letter/123.pdf");

- 39
- 1
- 7
you can use :-
InputStream input = new URL("\\test\hold\REPO/TEST/Letter/123.pdf").openStream();
or
File file = new File(location);
where location=\\test\hold\REPO/TEST/Letter/123.pdf;
and Check Using SOP statement whether URL is Call properly or not . hope it will help you to fine better solution

- 1,452
- 13
- 23