Solved

make a local file -> URL ???

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

Independent Software Vendors: 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

Suggested Solutions

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
This video teaches viewers about errors in exception handling.
Suggested Courses

759 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