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

No comments:

Post a Comment