URL Source Code Generator in Java with Source Code
URL Source Code Generator in Java with Source Code: We can develop URL Source Code Generator in java with the help of networking, AWT/Swing with event handling. Let's see the code of creating URL Source Code Generator in java.
- URL u=new URL("https://www.facebook.com");//change the URL
- URLConnection uc=u.openConnection();
- InputStream is=uc.getInputStream();
- int i;
- StringBuilder sb=new StringBuilder();
- while((i=is.read())!=-1){
- sb.append((char)i);
- }
- String source=sb.toString();
Let's see the swing code to generate Source Code of URL.
- import java.awt.*;
- import java.awt.event.*;
- import java.io.InputStream;
- import java.net.*;
- public class SourceGetter extends Frame implements ActionListener{
- TextField tf;
- TextArea ta;
- Button b;
- Label l;
- SourceGetter(){
- super("Source Getter Tool - Javatpoint");
- l=new Label("Enter URL:");
- l.setBounds(50,50,50,20);
-
- tf=new TextField();
- tf.setBounds(120,50,250,20);
-
- b=new Button("Get Source Code");
- b.setBounds(120, 100,120,30);
- b.addActionListener(this);
-
- ta=new TextArea();
- ta.setBounds(120,150,250,150);
-
- add(l);add(tf);add(b);add(ta);
- setSize(400,400);
- setLayout(null);
- setVisible(true);
- }
- public void actionPerformed(ActionEvent e){
- String s=tf.getText();
- if(s==null){}
- else{
- try{
- URL u=new URL(s);
- URLConnection uc=u.openConnection();
-
- InputStream is=uc.getInputStream();
- int i;
- StringBuilder sb=new StringBuilder();
- while((i=is.read())!=-1){
- sb.append((char)i);
- }
- String source=sb.toString();
- ta.setText(source);
- }catch(Exception ex){System.out.println(e);}
- }
- }
- public static void main(String[] args) {
- new SourceGetter();
- }
- }
No comments:
Post a Comment