• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Read File (Network)

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Chicken Farmer ()
Posts: 1932
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
I RELEASE YOU! (for now .... ) Feel free to peruse this tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic