URL reading program

Hello, with this program I am trying to get input (the content) from one or more urls and then print that content to a txt file or html file.

This is my code so far, please could you advise or change the code so that it carries out the function I require.

import java.util.*;

import java.io.*;

import java.net.*;

publicclass URLReader

{

publicvoid process (String[] argStrings)throws Exception

{

URL url;

String fileName = argStrings[0];

String contents =null;

url =new URL(argStrings[0]);

System.out.println();

System.out.println("Conatenated contents of selected pages:");

System.out.println();

PrintStream output =new PrintStream("NewFile.txt");

for (int index = 0; index < argStrings.length; index++)

{

Scanner sc =new Scanner(new File(argStrings[index]));

contents = sc.nextLine();

System.out.println(contents);

output.println(contents);

sc.close();

}

}

}

Thanks anyone

[1756 byte] By [John4938a] at [2007-10-2 6:24:52]
# 1

See URL.openConnection() and URL.openStream()

From there yoou could put a bufferedreader on an inputstreamreader and read the contents.

BTW, while I'm not that big of a style pusher

> public void process (String[] argStrings) throws

> s Exception <== declaring 'throws Exception' is really bad and can cause all sorts of problems for you down the road

> {

> URL url;

> String fileName = argStrings[0];

> String contents = null;

>

> url = new URL(argStrings[0]);

>

> System.out.println();

> System.out.println("Conatenated contents of

> of selected pages:");

> System.out.println();

>

> PrintStream output = new

> ew PrintStream("NewFile.txt");

>

> for (int index = 0; index < argStrings.length;

> h; index++)

> {

> Scanner sc = new Scanner(new

> new File(argStrings[index]));

>

> contents = sc.nextLine();

> System.out.println(contents);

>

> output.println(contents);

>

> sc.close();

> }

> }

> }

> [/code]

>

> Thanks anyone

tjacobs01a at 2007-7-16 13:26:43 > top of Java-index,Java Essentials,Java Programming...
# 2
look at the URL.getContent method
FuzzyOniona at 2007-7-16 13:26:43 > top of Java-index,Java Essentials,Java Programming...
# 3
Thanks for the help although this did not realy get me anywhere. Im quite new to java therefore if someone could start to write the code (not all of it), that would be a better hint. Person who does this can have one of those point things.cheersjohn
John4938a at 2007-7-16 13:26:43 > top of Java-index,Java Essentials,Java Programming...
# 4
In the Networking tutorial: http://java.sun.com/docs/books/tutorial/networking/under Working with URLs, you will find exactly the code you need.
DrClapa at 2007-7-16 13:26:43 > top of Java-index,Java Essentials,Java Programming...
# 5

Thanks, it was not the exact code as I want to be able to specify the url 's on the command line but anyway that helped alot.

Please check out my code and see whats wrong as it compiles ok but gives an error in dos.

cheers

// Import necessary classes

import java.util.*;

import java.io.*;

import java.net.*;

public class URLReader

{

public void process (String[] argStrings) throws Exception

{

// Declaring necessary variables

String contents = null;

URL url = new URL(argStrings[0]);

BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));

// Asks user to input URL's to be concatenated

System.out.println();

System.out.println("Concatenated contents of selected pages:");

System.out.println();

// Opens file

PrintStream output = new PrintStream("NewFile.txt");

// Loop where all URL's content is read and then written to NewFile.txt

for (int index = 0; index < argStrings.length; index++)

{

// New instance of scanner

Scanner sc = new Scanner(new File(argStrings[index]));

contents = sc.nextLine();

System.out.println(contents);

output.println(contents);

sc.close();

}

}

}

John4938a at 2007-7-16 13:26:43 > top of Java-index,Java Essentials,Java Programming...
# 6
You'll have to ask somebody who knows what the error message says. Somebody standing next to you would be a good candidate. Or if there's nobody standing next to you, ask somebody on the internet but remember to tell them what the stinking error message actually says.
DrClapa at 2007-7-16 13:26:43 > top of Java-index,Java Essentials,Java Programming...
# 7

Hello, I am realy finding this difficult. Im trying to compile this but this is the error I get:

RLreader.java:28: cannot find symbol

symbol : method nextLine()

location: class java.io.BufferedReader

contents = in.nextLine();

^

1 error

// Import necessary classes

import java.util.*;

import java.io.*;

import java.net.*;

class URLReader

{

public void process (String[] argStrings) throws Exception

{

// Declaring necessary variables

String contents = null;

URL url = new URL(argStrings[0]);

// Asks user to input URL's to be concatenated

System.out.println();

System.out.println("Concatenated contents of selected pages:");

System.out.println();

// Opens file

PrintStream output = new PrintStream("NewFile.txt");

// Loop where all URL's content is read and then written to NewFile.txt

for (int index = 0; index < argStrings.length; index++)

{

BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));

contents = in.nextLine();

System.out.println(contents);

output.println(contents);

in.close();

}

}

}

Thanks anyone

John4938a at 2007-7-16 13:26:43 > top of Java-index,Java Essentials,Java Programming...
# 8
It's not nextLine, it's readLine
FuzzyOniona at 2007-7-16 13:26:43 > top of Java-index,Java Essentials,Java Programming...