• Post Reply Bookmark Topic Watch Topic
  • New Topic

Read File (Network)  RSS feed

 
Derek Etnyre
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I have a Java program that currently opens an Excel file from my local hard drive.
I'm needing to change the programming so it will now open a file (always the same file) from a URL location.
I assume I should create an InputStream (see code below), but what do I do with the InputStream when my code is expecting a File object.
Any suggestions.
URL url = new URL("http://www.javaranch.com");
InputStream stream = url.openStream();
//How do I connect an InputStream to a File object?
 
jason adam
Chicken Farmer ()
Ranch Hand
Posts: 1932
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Might need to see a bit more of your code to help you with this.
File and Streams are different beasts. A File object is an abstract representation of something that may or may not exist on the local file system. A stream, however, is a direct connection to a real resource. So the two really aren't interchangeable.
Classes that read in files, like FileInputStream and FileReader, can be passed File objects when constructing them, but since File objects aren't guaranteed to represent an existing resource, this can throw an exception. That's about as close as you get to "connection" a File object with a stream.
You can do something like BufferedReader reader = new BufferedReader( new InputStreamReader( url.openStream() ) ); and then call reader.readLine().
If you REALLY need to deal with a File object, you could create a file locally, read in each line from the URL resource to that file, and deal with it. There's a few ways to do that, not sure what would be the "best" way.
What's the code like that is requiring a File object?
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!