Writing out StringBuffer with proper format

A

Alan

When I write out a StringBuffer, I lose all the original line
breaks. The code below reads a web page and writes it out to a
file.

Is there a way to use String Buffer (fast) instead of String, but
still get the output properly formatted?

Using the toString() method on the StringBuffer does not change
the output any.

Example of format is found below the code. If I read it line by
line into a String and write it out to a file, it is properly
formatted with line breaks (see last entry below).

Thanks, Alan

import java.net.*;
import java.io.*;

public class TestStringBuffer
{
public static void main ( String[] args ) throws IOException
{
try
{
String aURL = "http://www.weather.gov";
PrintWriter outfile = new PrintWriter(new
FileOutputStream("index.html"));
// Read the web page into a string
StringBuffer sb = URLtoString(aURL);
// Write out the stgring buffer
outfile.println(sb);
outfile.close();
}
catch (IOException e) {e.printStackTrace();}
}


public static StringBuffer URLtoString(String aURL) throws
IOException
{
try
{
URL url = new URL(aURL);
BufferedReader inURL = new BufferedReader(new
InputStreamReader(url.openStream()));
StringBuffer sb = new StringBuffer();
String lineOfText = null;
while ((lineOfText = inURL.readLine()) != null)
{
sb.append(lineOfText);
}
inURL.close();
return sb;
}
catch (MalformedURLException e)
{
e.printStackTrace();
return null;
}
catch (IOException e)
{
System.err.println("*** Unable to read web page " + aURL);
e.printStackTrace();
return null;
}

}


}

Written out with StringBuffer:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://
www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://
www.w3.org/1999/xhtml"><!-- #BeginTemplate "/Templates/
main_php.dwt.php" --><!-- DW6 --><head><!-- #BeginEditable "doctitle"
--><title>NOAA's National Weather Service</title><meta name="DC.title"
content="NOAA's National Weather Service" /><meta
name="DC.description" content="National Weather Service Home page. The
starting point for official government weather forecasts, warnings,
meteorological products for forecasting the weather, and information
about meteorology." /><meta name="DC.subject" content="Weather,
Warnings, Forecasts, Model Products, Climate information, Facsimile
Charts, Observations, links to government web sites, documentation on
meteorology, meteorological standards, Telecommunication protocols,
WMO Abbreviated Headings, National Oceanic Atmospheric Administration
organization, NESDIS Imagery, hydrometeorologic, hydrologic" /><meta
name="DC.date.reviewed" scheme="ISO8601" content="2007-05-24" /><meta
name="DC.date.created" scheme="ISO8601" content="2006-05-11" /><!--
#EndEditable --><meta name="DC.format" content="text/html;
charset=iso-8859-1" /><meta name="DC.language"
scheme="DCTERMS.RFC1766" content="EN-US" /><meta
name="DC.Distribution" content="Global" /><meta name="DC.robot"
content="all" /><meta name="DC.creator" content="NOAA's National
Weather Service" /><meta name="DC.contributor" content="NWS Internet
Services Team" /><link rel="schema.DC" href="http://purl.org/dc/
elements/1.1/" /><link rel="schema.DCTERMS" href="http://purl.org/dc/
terms/" /><link rel="DC.rights" href="http://www.weather.gov/
disclaimer.php" /><link rel="stylesheet" type="text/css" href="/
main.css" /><link rel="shortcut icon" href="/favicon.ico" /><script
type="text/javascript" language="JavaScript" src="/master.js"></
script></head><body onload="init()" background="/images/
background1.gif"><!-- Start banner --><!-- start banner inc --><table
cellspacing="0" cellpadding="0" border="0" width="100%" background="/
images/topbanner.jpg"> <tr> <td align="right" height="19"><a
href="#contents"><img src="/images/skipgraphic.gif" alt="Skip
Navigation Links" width="1" height="1" border="0" /></a> <a
href="/"><span class="nwslink">weather.gov</span></a>&nbsp;</td> </
tr></table><table cellspacing="0" cellpadding="0"

Written out with String (line by line):

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://
www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"><!-- #BeginTemplate "/
Templates/main_php.dwt.php" --><!-- DW6 -->
<head>
<!-- #BeginEditable "doctitle" -->
<title>NOAA's National Weather Service</title>
<meta name="DC.title" content="NOAA's National Weather Service" />
<meta name="DC.description" content="National Weather Service Home
page. The starting point for official government weather forecasts,
warnings, meteorological products for forecasting the weather, and
information about meteorology." />
<meta name="DC.subject" content="Weather, Warnings, Forecasts, Model
Products, Climate information, Facsimile Charts, Observations, links
to government web sites, documentation on meteorology, meteorological
standards, Telecommunication protocols, WMO Abbreviated Headings,
National Oceanic Atmospheric Administration organization, NESDIS
Imagery, hydrometeorologic, hydrologic" />
<meta name="DC.date.reviewed" scheme="ISO8601" content="2007-05-24" />
<meta name="DC.date.created" scheme="ISO8601" content="2006-05-11" />
<!-- #EndEditable -->
<meta name="DC.format" content="text/html; charset=iso-8859-1" />
<meta name="DC.language" scheme="DCTERMS.RFC1766" content="EN-US" />
<meta name="DC.Distribution" content="Global" />
<meta name="DC.robot" content="all" />
<meta name="DC.creator" content="NOAA's National Weather Service" />
<meta name="DC.contributor" content="NWS Internet Services Team" />
<link rel="schema.DC" href="http://purl.org/dc/elements/1.1/" />
<link rel="schema.DCTERMS" href="http://purl.org/dc/terms/" />
<link rel="DC.rights" href="http://www.weather.gov/disclaimer.php" />
<link rel="stylesheet" type="text/css" href="/main.css" />
<link rel="shortcut icon" href="/favicon.ico" />
<script type="text/javascript" language="JavaScript" src="/
master.js"></script>
</head>
 
D

David Zimmerman

Andrew said:
. So put them back in.

..

// add the System dependant EOL
sb.append(lineOfText + System.getProperty("line.separator") );

sb.append(lineOfText).append(System.getProperty("line.separator"));

You have perfectly good StringBuffer, might as well use it.
 
R

Roedy Green

while ((lineOfText = inURL.readLine()) != null)
{
sb.append(lineOfText);
}
you have used readLine which strips the line breaks.

See http://mindprod.com/products1.html#HTTP
for a way to read without readLine.

OR you can append your own line terminator to replace the one you
threw away : e.g.

sb.append( '\n');
or sb.append( lineSeparator);


static final String lineSeparator = System.getProperty (
"line.separator" );
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top