View RSS Feed

Java Socket

URL's to Local Files

Rate this Entry
by , 04-25-2012 at 10:42 PM (2858 Views)
In local file system, files could be accessed by using URL class. Hence, file opening is an easy task done by URL class, if your code is needed to find out whether file came from local file system or network.
How a file could be opened in local file system by URL class is shown in this example:
URL url = new URL("file:/c:/data/test.txt");
Java Code:
URLConnection urlConnection = url.openConnection();
InputStream input = urlConnection.getInputStream();

int data = input.read();
while(data != -1){
    System.out.print((char) data);
    data = input.read();
}
input.close();
It shall be noticed that how the difference from file access at server through HTTP is the the URL: "file:/c:/data/test.txt".

Submit "URL's to Local Files" to Facebook Submit "URL's to Local Files" to Digg Submit "URL's to Local Files" to del.icio.us Submit "URL's to Local Files" to StumbleUpon Submit "URL's to Local Files" to Google

Categories
Tutorial

Comments