Friday, 14 July 2017

URL Source Code Generator in java ~ java help

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.facebook.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 - Javatpoint");  
  12.         l=new Label("Enter URL:");  
  13.         l.setBounds(50,50,50,20);  
  14.           
  15.         tf=new TextField();  
  16.         tf.setBounds(120,50,250,20);  
  17.           
  18.         b=new Button("Get Source Code");  
  19.         b.setBounds(120100,120,30);  
  20.         b.addActionListener(this);  
  21.           
  22.         ta=new TextArea();  
  23.         ta.setBounds(120,150,250,150);  
  24.           
  25.         add(l);add(tf);add(b);add(ta);  
  26.         setSize(400,400);  
  27.         setLayout(null);  
  28.         setVisible(true);  
  29.     }  
  30.     public void actionPerformed(ActionEvent e){  
  31.         String s=tf.getText();  
  32.         if(s==null){}  
  33.         else{  
  34.             try{  
  35.             URL u=new URL(s);  
  36.             URLConnection uc=u.openConnection();  
  37.           
  38.             InputStream is=uc.getInputStream();  
  39.             int i;  
  40.             StringBuilder sb=new StringBuilder();  
  41.             while((i=is.read())!=-1){  
  42.                 sb.append((char)i);  
  43.             }  
  44.             String source=sb.toString();  
  45.             ta.setText(source);  
  46.             }catch(Exception ex){System.out.println(e);}  
  47.         }  
  48.     }  
  49.     public static void main(String[] args) {  
  50.         new SourceGetter();  
  51.     }  
  52. }

No comments:

Post a Comment