import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
public class ReadHtmlFile {
public static void main(String[] args) {
String sourcePath="http://www.sina.com/robots.txt";//读取新浪网站的机器人向导
new ReadHtmlFile().readHtmlFile(sourcePath);
}
/**
* 读取文件
* @param sourcePath 文件所在的网络路径
*/
public void readHtmlFile(String sourcePath){
String line;
int lineNum=0;
BufferedReader reader=null;
try{
URL url = new URL(sourcePath);
reader = new BufferedReader(new InputStreamReader(url.openStream()));
while ((line = reader.readLine()) != null){
lineNum++;
System.out.println(line);
}
}
catch (Exception ie){
ie.printStackTrace();
}finally{
try{
if(reader != null)
reader.close();
}catch (Exception e){
e.printStackTrace();
}
}
}
}
Java读取网络上的文件内容
最新推荐文章于 2024-03-03 11:33:03 发布