• Post Reply Bookmark Topic Watch Topic
  • New Topic

Open a file from network share  RSS feed

 
Richard Marx
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have googled everywhere and I am not finding an answer to this question. How would one use java to open a file that is on a network?
 
Paweł Baczyński
Bartender
Posts: 2074
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I googled java open file from network and I found many answers.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12562
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What have you tried? What did it do (or not do)? If you really want help, you need to TellTheDetails.
 
Paweł Baczyński
Bartender
Posts: 2074
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
fred rosenberger wrote:What have you tried? What did it do (or not do)? If you really want help, you need to TellTheDetails.

And ShowSomeEffort.
 
Richard Marx
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
fred rosenberger wrote:What have you tried? What did it do (or not do)? If you really want help, you need to TellTheDetails.


I have tried what I found when I googled which were examples using Desktop.


This would work if the file is on my local machine, but does not work with a network path.
 
Richard Marx
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
fred rosenberger wrote:What have you tried? What did it do (or not do)? If you really want help, you need to TellTheDetails.


The code I posted above, opened a file on my local machine, but would fail anytime I tried to open a file on a network path.
 
Richard Marx
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Let me also add that my network path will have spaces in the network path. So that may throw a wrench in the way that the file will be opened.
 
Richard Marx
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Richard Marx wrote:Let me also add that my network path will have spaces in the network path. So that may throw a wrench in the way that the file will be opened.

I have also tried to run


The output displays the proper path & filename.
But when it try's to open I get this error:
Caught IOException: Failed to open file: \\\\ServerName\\Folder1\\Folder2\\Folder 3\\ReportsAlpha.xls
Error Message: The system can not find the file specified.

The file exists. If I go to start and run and I copy paste the location from my new file dialog there, it will actually open the workbook.
So either my code is way wrong, or I am attempting to open a file on the network incorrectly.
 
Ivan Jozsef Balazs
Rancher
Posts: 999
5
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is very suspicious:

Caught IOException: Failed to open file: \\\\ServerName\\Folder1\\Folder2\\Folder 3\\ReportsAlpha.xls


Because \ is the escape character within the string literals, so


should show up as
\\ServerName\Folder1\Folder2\Folder 3\ReportsAlpha.xls

which appears to be your intention.

Or maybe you do not copy-and-paste (which you should) the error but reinterpet/rephrase it (which you should not).



 
Richard Marx
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ivan Jozsef Balazs wrote:This is very suspicious:

Caught IOException: Failed to open file: \\\\ServerName\\Folder1\\Folder2\\Folder 3\\ReportsAlpha.xls


Because \ is the escape character within the string literals, so


should show up as
\\ServerName\Folder1\Folder2\Folder 3\ReportsAlpha.xls

which appears to be your intention.

Or maybe you do not copy-and-paste (which you should) the error but reinterpet/rephrase it (which you should not).



I did hand-type it, let me copy/paste the error this go round:
Caught IOException: Failed to open file:////ServerName/Folder1/Folder2/Folder%203/ReportsAlpha.xls
Error Message: The system could not find the file specified.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Richard Marx wrote:I did hand-type it, let me copy/paste the error this go round:
Caught IOException: Failed to open file:////ServerName/Folder1/Folder2/Folder%203/ReportsAlpha.xls
Error Message: The system could not find the file specified.

Looks to me like you've doubled up forwards slashes in your String when you don't need to. Only back-slashes need to be duplicated; and you don't need to use those for path names in Java; it's exclusively a Wndows thing.

Winston
 
Richard Marx
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:
Richard Marx wrote:I did hand-type it, let me copy/paste the error this go round:
Caught IOException: Failed to open file:////ServerName/Folder1/Folder2/Folder%203/ReportsAlpha.xls
Error Message: The system could not find the file specified.

Looks to me like you've doubled up forwards slashes in your String when you don't need to. Only back-slashes need to be duplicated; and you don't need to use those for path names in Java; it's exclusively a Wndows thing.

Winston


So everywhere in the string would just need to be single slashes? --- If that is the case it won't even compile anymore.
If I reduce the 4 slashes to 2 at the beginning of my string so it reads: //ServerName/Folder1/Folder2/Folder%203/ReportsAlpha.xls I then get an error of:
Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: The file: \ServerName\Folder1\Folder2\Folder%203\ReportsAlpha.xls does not exist.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Richard Marx wrote:So everywhere in the string would just need to be single slashes? --- If that is the case it won't even compile anymore.
If I reduce the 4 slashes to 2 at the beginning of my string so it reads: //ServerName/Folder1/Folder2/Folder%203/ReportsAlpha.xls I then get an error of:
Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: The file: \ServerName\Folder1\Folder2\Folder%203\ReportsAlpha.xls does not exist.

I think you need to show us your code; but the fact is that you shouldn't use backslashes for path names in Java:
1. It's only Windows that specifies paths in that bizarre way.
2. Backslashes in Java Strings are used for escaping, which is why you have to double them up, which makes things mighty confusing.
3. There may be problems with the '%20' notation (I'd have to check the docs again, because it's been a while).

But first: show us your code, because it's difficult to know what's wrong until you do.

Winston
 
Richard Marx
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:
Richard Marx wrote:So everywhere in the string would just need to be single slashes? --- If that is the case it won't even compile anymore.
If I reduce the 4 slashes to 2 at the beginning of my string so it reads: //ServerName/Folder1/Folder2/Folder%203/ReportsAlpha.xls I then get an error of:
Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: The file: \ServerName\Folder1\Folder2\Folder%203\ReportsAlpha.xls does not exist.

I think you need to show us your code; but the fact is that you shouldn't use backslashes for path names in Java:
1. It's only Windows that specifies paths in that bizarre way.
2. Backslashes in Java Strings are used for escaping, which is why you have to double them up, which makes things mighty confusing.
3. There may be problems with the '%20' notation (I'd have to check the docs again, because it's been a while).

But first: show us your code, because it's difficult to know what's wrong until you do.

Winston

Java is adding in the %20 in the space, I do not have that hardcoded, below is my code that I am running
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Richard Marx wrote:Java is adding in the %20 in the space, I do not have that hardcoded, below is my code that I am running

OK, now we can get somewhere; but I'm afraid nothing leaps out at me.

First: I'd take out the Desktop stuff for the moment and just test it with a simple File. Does File.exists() return false?

Second: Have you tried creating a URI instead (don't forget to add "file:", and I'd suggest using forward slashes (undoubled)), and then testing whether you can connect to it with toURL.openConnection().connect()? That might help you to work out where your problem is occurring, and as soon as you get that working, you can then create a File using the URI.

My problem is that I (and possibly you) don't know enough about Desktop or what it does with Files to speculate, so if you remove that layer and just test with basic classes, you may be able work out what the problem is more easily.

Alternatively, someone cleverer than me may be able to tell you exactly what your problem is.

Winston
 
Richard Marx
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:
Richard Marx wrote:Java is adding in the %20 in the space, I do not have that hardcoded, below is my code that I am running

OK, now we can get somewhere; but I'm afraid nothing leaps out at me.

First: I'd take out the Desktop stuff for the moment and just test it with a simple File. Does File.exists() return false?

Second: Have you tried creating a URI instead (don't forget to add "file:", and I'd suggest using forward slashes (undoubled)), and then testing whether you can connect to it with toURL.openConnection().connect()? That might help you to work out where your problem is occurring, and as soon as you get that working, you can then create a File using the URI.

My problem is that I (and possibly you) don't know enough about Desktop or what it does with Files to speculate, so if you remove that layer and just test with basic classes, you may be able work out what the problem is more easily.

Alternatively, someone cleverer than me may be able to tell you exactly what your problem is.

Winston


Simply running this:


Will return File exists.

Now for your 2nd recommendation I have not tried using URI as I am beginning Java. I will google and see what results I can return from that and see if I can create a working example. Thank you for the help thus far.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Richard Marx wrote:Now for your 2nd recommendation I have not tried using URI as I am beginning Java. I will google and see what results I can return from that and see if I can create a working example. Thank you for the help thus far.

No probs. My problem is that I simply haven't done that stuff recently enough to help more - and I'm not in a position to set up a network share to try it out.

However, it's generally a good rule when dealing with problems like this to remove as much "middleware" stuff (like Desktop) from the equation as you can and test with basic components.

BTW: If you go the URI/URL route, I'd also split up the process completely, viz something like:That way you'll see exactly where the error is occurring if one does. If all of that works fine, then you can create a File, then a FileInputStream, and try and read from it; but do it all in separate statements, just as if you were showing the individual steps to a kid.

And like I say, it's very possible that someone will get off work, come here, and see immediately what your problem is. I have to use old-fashioned "try this" logic.

HIH

Winston
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!