首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

java 读取网页源码

2013-04-09 
java 读取网页源码;输入标准为 http://www.xxx.comimport java.awt.event.ActionEventimport java.awt.ev

java 读取网页源码;

输入标准为 http://www.xxx.com

 

 

import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.io.DataInputStream;import java.io.IOException;import java.net.MalformedURLException;import java.net.URL;import java.net.URLConnection;import javax.swing.JButton;import javax.swing.JFrame;import javax.swing.JPanel;import javax.swing.JScrollPane;import javax.swing.JTextArea;import javax.swing.JTextField;import org.omg.CORBA.TCKind;public class web extends JFrame implements ActionListener{/** * @param args */JTextField t1=new JTextField(30);JTextArea t2=new JTextArea();JButton b1=new JButton("open");JPanel p=new JPanel();public web(){p.add(t1);p.add(b1);JScrollPane jp=new JScrollPane(t2);b1.addActionListener(this);getContentPane().add(p,"North");getContentPane().add(jp,"Center");setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);setSize(500, 400);setVisible(true);setTitle("read url");}public static void main(String[] args) {// TODO Auto-generated method stubweb webH=new web();}@Overridepublic void actionPerformed(ActionEvent e) {// TODO Auto-generated method stubreadURL(t1.getText());}private void readURL(String URLName) {// TODO Auto-generated method stubtry {URL url=new URL(URLName);URLConnection  tc=url.openConnection();tc.connect();DataInputStream dis=new DataInputStream(tc.getInputStream());String inputLineString;while ((inputLineString=dis.readLine())!=null) {t2.append(inputLineString+"\n");}} catch (MalformedURLException e) {// TODO: handle exceptione.printStackTrace();}catch (IOException e1) {e1.printStackTrace();// TODO: handle exception}}}


 

热点排行