继续浏览精彩内容
慕课网APP
程序员的梦工厂
打开
继续
感谢您的支持,我会继续努力的
赞赏金额会直接到老师账户
将二维码发送给自己后长按识别
微信支付
支付宝支付

读取慕课网<code>标记内容

car
关注TA
已关注
手记 83
粉丝 56
获赞 363
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import junit.framework.TestCase;

public class FileReadNet extends TestCase {
    public void testName() throws IOException {
        URL url = new URL("http://www.imooc.com/article/12061");
        URLConnection connection = url.openConnection();
        connection.connect();
        InputStream inputStream = connection.getInputStream();
        InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
        BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
        String line;
        StringBuilder builder = new StringBuilder(1024);
        while ((line = bufferedReader.readLine()) != null) {
            builder.append(line);
            builder.append("\r\n");
        }
        bufferedReader.close();
        inputStreamReader.close();
        inputStream.close();
        line = builder.toString();
        int index = line.indexOf("<code>") + "<code>".length();
        int lastIndex = line.lastIndexOf("</code>");
        line = line.substring(index, lastIndex);
        line = line.replaceAll("&lt;", "<").replaceAll("&gt;", ">");
        System.out.println(line);
    }
}
打开App,阅读手记
0人推荐
发表评论
随时随地看视频慕课网APP