Monday, 25 April 2011

Java file as url

 A file can be read as URL with the java.net package. You must provide absolute path after the URI scheme name file://.
package com.demo.io;

import java.io.*;
import java.net.*;

public class FileAsURL {
public static void main(String[] args) throws Exception {
URL url = new URL("file:///tmp/com/demo/io/test.txt");
URLConnection conn = url.openConnection();
BufferedReader r = new BufferedReader(
new InputStreamReader(
conn.getInputStream()
)
);
String l = r.readLine();
while (l != null) {
System.out.println(l);
l = r.readLine();
}
}
}

1 comment:

  1. II have read your blog its very attractive and impressive. I like it your blog.

    Java Training in Chennai Core Java Training in Chennai Core Java Training in Chennai

    Java Online Training Java Online Training JavaEE Training in Chennai Java EE Training in Chennai

    ReplyDelete