I've looked all over SO and have yet to find a solution to this. Long and short of it is I'm trying to take a text file from a provided URL and read it into a String so I can do other things with it.
The specific compile error is:
/tmp/java_kWWEO5/Main.java:49: error: unreported exception IOException; must be caught or declared to be thrown String text = readUrlTextContent("http://textfiles.com/stories/antcrick.txt"); ^ 1 error
The code causing the error, in main
:
import java.util.*;
import java.net.URL;
import java.net.MalformedURLException;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
public class Main {
private static String readUrlTextContent(String url) throws IOException, MalformedURLException {
URL source = new URL(url);
BufferedReader reader = new BufferedReader(new InputStreamReader(source.openStream()));
try {
StringBuilder builder = new StringBuilder();
String line = reader.readLine();
while (line != null) {
builder.append(line);
builder.append("\n");
line = reader.readLine();
}
return builder.toString();
} catch (MalformedURLException urlEx) {
urlEx.printStackTrace();
throw new RuntimeException("Malformed URL Exception", urlEx);
} catch (IOException ioEx) {
ioEx.printStackTrace();
throw new RuntimeException("IO Exception", ioEx);
} finally {
reader.close();
}
}
public static void main(String[] args) {
String text = readUrlTextContent("http://textfiles.com/stories/antcrick.txt");
System.out.println(text);
}
}
What could I do to modify this short program so that it finally compiles and executes?