java读取文件下所有html文件,用多线程读取目录中所有html文件内容,求修正代码...

该楼层疑似违规已被系统折叠 隐藏此楼查看此楼

public class AllThread extends Thread {

public String content = "";

public String url = "";

public void run() {

System.out.println(url);

content = getContents(url);

}

public AllThread(String the_url) {

url = the_url;

}

private static String getContents(String urlName) {

try {

StringBuffer sb = new StringBuffer();

URL url = new URL(urlName);

URLConnection u = url.openConnection();

InputStream is = u.getInputStream();

InputStreamReader isr = new InputStreamReader(is, "gbk");

int c;

while ((c = isr.read()) != -1) {

sb.append((char) c);

}

is.close();

isr.close();

return sb.toString();

} catch (IOException ex) {

return ex.getMessage();

}

}

public static void main(String args[]) {

//TODO自动生成方法存根

//String[] dir = {"http://www.baidu.com ", "http://www.yisou.com "}; File[] dir = new File("//192.168.18.232/heritrix/www.duote.com/softin").listFiles();//提取softin中所有的html文件

AllThread[] threads = new AllThread[dir.length];

//循环输出文件名

for (int i = 0; i < dir.length; i++) {

threads[i] = new AllThread(dir[i].getName());

threads[i].start();

}

//循环输出文件内容

for (int i = 0; i < dir.length; i++) {

try {

threads[i].join();

} catch (InterruptedException e) {

// TODO 自动生成 catch 块

e.printStackTrace(); }

System.out.println(threads[i].content);

}

}

}

输出结果:31283.html3108.html53809.html22770.html42448.html11075.html22426.htmlno protocol: 2253.htmlno protocol: 19455.htmlno protocol: 26932.htmlno protocol: 21136.html…………………………(读取文件名成功,读取内容却失败了TwT)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值