Solved

make a local file -> URL ???

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

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…

685 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