Wednesday

URLs to Local Files






The URL class can also be used to access files in the local file system. Thus the URL class can be a handy way to open a file, if you need your code to not know whether the file came from the network or local file system.

Here is an example of how to open a file in the local file system using the URL class: URL url = new URL("file:/c:/data/test.txt");

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();


Notice how the only difference from accessing a file on a web server via HTTP is the the URL: "file:/c:/data/test.txt".

Related Posts:

  • Difference between TCP and UDP TCP UDP Reliability: TCP is connection-oriented protocol. When a file or message send it will get delivered unless co… Read More
  • Traversing in Linear Array  Array is a container which can hold a fix number of items and these items should be of the same type. Most of the data structures make use of … Read More
  • URLs to Local Files The URL class can also be used to access files in the local file system. Thus the URL class can be a handy way to open a file, if you need your cod… Read More
  • HTTP GET and POST By default the URLConnection sends an HTTP GET request to the webserver. If you want to send an HTTP POST request instead, call the URLConnection… Read More
  • TCP/IP Client example in Java The Socket class is used for client connections. The client connects on the published port of the server. Please note the usage of InetAddress clas… Read More

0 comments:

Post a Comment