Solved

make a local file -> URL ???

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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Suggested Solutions

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…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
This video teaches viewers about errors in exception handling.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

758 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now