Java Reference
In-Depth Information
InputStream is = u.openStream();
OutputStream os = new FileOutputStream(filename);
downloadText(is, os);
is.close();
os.close();
} catch (MalformedURLException e)
{
e.printStackTrace();
} catch (IOException e)
{
e.printStackTrace();
}
}
/**
* Download a text file, and convert the line breaks for
* whatever the current operating system is.
*
* @param is The input stream to read from.
* @param os The output stream to write to..
*/
private void downloadText(InputStream is, OutputStream os)
throws IOException
{
byte lineSep[] =
System.getProperty("line.separator").getBytes();
int ch = 0;
boolean inLineBreak = false;
boolean hadLF = false;
boolean hadCR = false;
do
{
ch = is.read();
if (ch != -1)
{
if ((ch == '\r') || (ch == '\n'))
{
inLineBreak = true;
if (ch == '\r')
{
if (hadCR)
os.write(lineSep);
else
hadCR = true;
Search WWH ::




Custom Search