上班打酱油,用Eclipse看糗百

创意来自一位用python的大神写的看糗百,自己写了个Eclipse的,如有不足之处,希望各位多指教。

095032_3oE8_878647.png


package test;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.Scanner;

public class QiuShiBaiKe {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		StringBuffer sb = new StringBuffer();
		System.out.println("Please input the page:");
		int i = 1;
		while(true) {
			Scanner sc = new Scanner(System.in);
			if (sc.next() != "0") {
				sb = getUrl(i++);
			} else {
				i = 1;
			}
			System.out.println(sb);
		}
	}
	
	private static StringBuffer getUrl(int page) {
		StringBuffer sb = new StringBuffer();
		try {
			URL url = new URL("http://www.qiushibaike.com/8hr/page/"+page+"?s=4595975");
			InputStreamReader in = new InputStreamReader(url.openStream(),"UTF-8");
			BufferedReader br = new BufferedReader(in);
			String line;
			while((line = br.readLine())!=null) {
				if(line!=null && !line.isEmpty() && (line.charAt(0) == '<' || line.charAt(0) == '&')){
					continue;
				}
				sb.append("\n"+changeLine(line));
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return sb;
	}
	
	private static String changeLine(String line) {
		StringBuffer sb = new StringBuffer(line);
		for(int i = 0;i < line.length()/70 ; i++) {
			sb.insert((i+1)*70, "\n");
		}
		return sb.toString();
	}

}


转载于:https://my.oschina.net/Thistle/blog/220210

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值