Skip to content

Instantly share code, notes, and snippets.

@LukasKnuth
Created April 6, 2012 14:23
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save LukasKnuth/2320294 to your computer and use it in GitHub Desktop.
Save LukasKnuth/2320294 to your computer and use it in GitHub Desktop.
import java.io.*;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
/**
* This is an example-implementation, showing how to download a raw-RSS
* feed from a web-address and how to write it's content to a File.</p>
* All Exceptions are handled internally by their corresponding methods.
* In a real-use scenario, this might be the wrong way, since your
* program should react on occurring exception's rather then just printing
* out the StackTrace and ignoring it.
* @see <a href="http://stackoverflow.com/questions/10032583">for the discussion</a>
* @author Lukas Knuth
* @version 1.0
*/
public class ReadFromWeb {
/**
* Main entry point.
*/
public static void main(String[] args){
// Read the contents:
URL url = null;
try {
url = new URL("http://stackoverflow.com/feeds/tag/java");
} catch (MalformedURLException e) {
// When the URL was not well-formed
e.printStackTrace();
}
String content = readContents(url);;
// Write the contents:
File output_file = new File("output.xml");
writeContents(output_file, content);
}
/**
* This method will write the given contents (in string-form)
* to the given File-Instance.</p>
* All possible exceptions are handled internally, this should
* be changed when in real-use-scenarios...
* @param file the file to write to.
* @param content the contents to write to the file.
*/
public static void writeContents(File file, String content){
BufferedWriter out = null;
try {
out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file)));
// Write:
out.write(content);
/*
Normally, you don't need to call this manually, because it is called when
the "close()"-method is called. I like to do it anyhow...
*/
out.flush();
} catch (FileNotFoundException e) {
// The given file could not be found
e.printStackTrace();
} catch (IOException e) {
// There was a problem writing to the File
e.printStackTrace();
} finally {
// Close the Streams to prevent memory-leaks:
if (out != null) try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* Reads the raw-data from a given {@code URL}.</p>
* This method has undefined behaviour for reading binary-
* files. It is intended for plain-text files only.</p>
* All possible exceptions are handled internally, this should
* be changed when in real-use-scenarios...
* @param url the {@code URL} to read from.
* @return the raw-content of the document at the given {@code URL}.
*/
public static String readContents(URL url){
BufferedReader in = null;
try {
URLConnection con = url.openConnection();
in = new BufferedReader(new InputStreamReader(con.getInputStream()));
// Load the contents:
String line = in.readLine();
/*
You should use a StringBuilder when appending multiple times
because it's faster then using normal concation (+=) with Strings.
*/
StringBuilder builder = new StringBuilder();
do {
builder.append(line+"\n"); // Keep the line-endings (pretty print)
} while ( (line = in.readLine()) != null);
// Return the contents:
return builder.toString();
} catch (IOException e) {
// Problem reading from the URL
e.printStackTrace();
} finally {
// Close the Streams to prevent memory-leaks:
if (in != null) try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return ""; // Better way to do that...
}
}
@smile481
Copy link

smile481 commented Apr 1, 2013

I am using your code and trying to convert the xml file generated to XML column compatible (binary stream) and store it in database. My table column is XML column.

When I try to run the below code as right click -> Run as->Java Application, a pop up shows up Select Java Application. This is because I have written throws SQLException, FileNotFoundException. But I cant avoid this because I added the below lines of code.

Please see http://stackoverflow.com/questions/15738868/how-can-i-convert-file-content-output-xml-to-xml-datatype-and-store-in-db2-dat

Can you please help me out? Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment