All for Joomla All for Webmasters

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.

  1. URL u=new URL(“https://www.makeforsure.com”);//change the URL
  2. URLConnection uc=u.openConnection();
  3. InputStream is=uc.getInputStream();
  4. int i;
  5. StringBuilder sb=new StringBuilder();
  6. while((i=is.read())!=-1){
  7.     sb.append((char)i);
  8. }
  9. String source=sb.toString();

Let’s see the swing code to generate Source Code of URL.

  1. import java.awt.*;
  2. import java.awt.event.*;
  3. import java.io.InputStream;
  4. import java.net.*;
  5. public class SourceGetter extends Frame implements ActionListener{
  6.     TextField tf;
  7.     TextArea ta;
  8.     Button b;
  9.     Label l;
  10.     SourceGetter(){
  11.         super(“Source Getter Tool – makeforsure.com”);
  12.         l=new Label(“Enter URL:”);
  13.         l.setBounds(50,50,50,20);
  14.         tf=new TextField();
  15.         tf.setBounds(120,50,250,20);
  16.         b=new Button(“Get Source Code”);
  17.         b.setBounds(120100,120,30);
  18.         b.addActionListener(this);
  19.         ta=new TextArea();
  20.         ta.setBounds(120,150,250,150);
  21.         add(l);add(tf);add(b);add(ta);
  22.         setSize(400,400);
  23.         setLayout(null);
  24.         setVisible(true);
  25.     }
  26.     public void actionPerformed(ActionEvent e){
  27.         String s=tf.getText();
  28.         if(s==null){}
  29.         else{
  30.             try{
  31.             URL u=new URL(s);
  32.             URLConnection uc=u.openConnection();
  33.             InputStream is=uc.getInputStream();
  34.             int i;
  35.             StringBuilder sb=new StringBuilder();
  36.             while((i=is.read())!=-1){
  37.                 sb.append((char)i);
  38.             }
  39.             String source=sb.toString();
  40.             ta.setText(source);
  41.             }catch(Exception ex){System.out.println(e);}
  42.         }
  43.     }
  44.     public static void main(String[] args) {
  45.         new SourceGetter();
  46.     }
  47. }
PinIt
submit to reddit

Leave a Reply

Top