Solved

make a local file -> URL ???

Posted on 1997-12-22
2
2,417 Views
Last Modified: 2012-05-04
    I am currently writing a Java application (not an applet). To load a particular file (from the local hard disk), I need to have it represented as a URL class object. If I know my file to be: "C:\JavaWork\Sounds\mysound.au", how do I turn this into a URL?

I am not sure how to tell the URL constructor that this is a local file and how to represent the path and file name to the URL constructor so it is found.
0
Comment
Question by:dgason
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 5

Accepted Solution

by:
msmolyak earned 150 total points
ID: 1231796
Generally you don't have to represent a file as an URL just to open it from a local disk from within an application (FileInputStream should do).

To create an URL object you can use several methods:
new URL("file:///C|/JavaWork/Sounds/mysound.au");
or
new URL("file", "", "C:\JavaWork\Sounds\mysound.au");

Protocol "file" is supported by JDK (along with such protocols as doc, http, gopher, ftp, mailto). That means that after constructing an URL object with file protocol you can open an URLConnection to that object (from which in turn you can get an input or output stream for the underlying resource).

myFile = new URL("file:///C|/JavaWork/Sounds/mysound.au");
URLConnection uc = myFile.openConnection();
InputStream fileInputStream = uc.getInputStream();

Check the URLConnection class, it has plenty of useful method to work with your resource.
0
 

Expert Comment

by:ashishkh
ID: 1231797
Thanks for the help!
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Suggested Courses

630 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question