Saturday, 10 January 2015

Reading a text file from a networked resource

Reading the content from network resource

package files;

import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;

public class ReadNetworkFile {

public static void main(String[] args) {

try {
URL url = new URL("http://services.explorecalifornia.org/rss/tours.php");

InputStream in = url.openStream();
BufferedInputStream bis = new BufferedInputStream(in);

StringBuilder sb = new StringBuilder();

while (true) {
int val = bis.read(); //reading byte by byte

if (val == -1){ // -1 represents it reached EOF
break;
}else{
sb.append((char)val);
}
}

System.out.println(sb);

} catch (IOException e) {
e.printStackTrace();
}

}

}

No comments:

Post a Comment