Okay I'm trying to read in newsgroups from the host newsserver listed in my instance data. The programs compiles but freezes at startup. Here's the code (I guess copy and paste it into an editor to view it easier)
import java.io.*;
import java.net.*;
import java.util.*;
public class NewsManager {
public static void main(String[] args) {
//instance constants
//instance variables
String fname = ".newsrc"; //name of file to read from
String host = "news.readfreenews.net";
String home;//user's home directory
String separator;//path separator
int nntpServicePortNumber = 119;
/*********************************************************************/
//get user home directory and file separator
home = System.getProperty("user.home");
File dir = new File(home);
separator = dir.pathSeparator;
//check if .newsrc file exists, if not create it
File newsrc = new File(dir, ".newsrc");
try {
newsrc.createNewFile(); }
catch (IOException ex) {
System.out.println("File IO error"); }
BufferedReader in = null;
PrintWriter out = null;
Socket nntpSocket = null;
try {
FileWriter fw = new FileWriter(newsrc, true);
BufferedWriter bw = new BufferedWriter(fw);
try {
nntpSocket = new Socket(host, nntpServicePortNumber);
out = new PrintWriter(nntpSocket.getOutputStream(), true);
in = new BufferedReader(new InputStreamReader(
nntpSocket.getInputStream())); }
catch (UnknownHostException e) {
System.err.println("Don't know about host " + host);
System.exit(1); }
catch (IOException e) {
System.err.println("Couldn't get I/O for the connection.");
System.exit(1); }
try {
String response200 = in.readLine();
if (!response200.startsWith("2")) {
System.err.println("Protocol Error");
System.exit(1); }
String response215 = in.readLine();
if (!response215.startsWith("215")) {
System.err.println("Protocol Error");
System.exit(1); }
for (;
{
String line = in.readLine();
if (line.startsWith(".")) break;
StringTokenizer toks = new StringTokenizer(line);
String name = toks.nextToken();
int first = 0;
String firstAsString = toks.nextToken();
try {
first = Integer.parseInt(firstAsString); }
catch (NumberFormatException ex) {
System.err.println("Bad number format? " + firstAsString);
System.exit(1); }
int last = 0;
String lastAsString = toks.nextToken();
try {
last = Integer.parseInt(lastAsString); }
catch (NumberFormatException ex) {
System.err.println("Bad number format? " + lastAsString);
System.exit(1); }
NewsGroup n = new NewsGroup(name, first, last);
try {
bw.write(name + first + last); }
catch (IOException ex) {
System.out.println("File IO error"); }
System.out.println(n);
}//end for
out.close();
in.close();
nntpSocket.close();
bw.close();
}//end try
catch (IOException ex) {
System.err.println("IO failure.");
ex.printStackTrace(); }
}//end try
catch (IOException ex) {
System.out.println("File IO error"); }
}
}