This discussion is archived
0 Replies Latest reply: Feb 15, 2011 10:16 PM by 840126 RSS

How to Write source code of a html page to a File

840126 Newbie
Currently Being Moderated
Using this code i could find the source code of a html page from a web server

import java.io.*;
import java.net.*;

public class SourceViewer{
public static void main (String[] args) throws IOException{
System.out.print("Enter url of local for viewing html source code: ");
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String url = br.readLine();
try{
URL u = new URL(url);
HttpURLConnection uc = (HttpURLConnection) u.openConnection();
int code = uc.getResponseCode();
String response = uc.getResponseMessage();
System.out.println("HTTP/1.x " + code + " " + response);
for(int j = 1; ; j++){
String header = uc.getHeaderField(j);
String key = uc.getHeaderFieldKey(j);
if(header == null || key == null)
break;
System.out.println(uc.getHeaderFieldKey(j) + ": " + header);
}
InputStream in = new BufferedInputStream(uc.getInputStream());
Reader r = new InputStreamReader(in);
int c;
while((c = r.read()) != -1){
System.out.print((char)c);
}
}
catch(MalformedURLException ex){
System.err.println(url + " is not a valid URL.");
}
catch(IOException ie){
System.out.println("Input/Output Error: " + ie.getMessage());
}
}
}

Iam not much a expert in java .. i could find some commands to write to a file but i dont know where to use those codes , some body please give me some idea ..

thanking you ..

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points