Solved

make a local file -> URL ???

Posted on 1997-12-22
2
2,411 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
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
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…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…

856 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