Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Writing out StringBuffer with proper format

Reply
Thread Tools

Writing out StringBuffer with proper format

 
 
Alan
Guest
Posts: n/a
 
      10-08-2007
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>

 
Reply With Quote
 
 
 
 
Andrew Thompson
Guest
Posts: n/a
 
      10-08-2007
Alan wrote:
>When I write out a StringBuffer, I lose all the original line
>breaks.


.. So put them back in.

...
> sb.append(lineOfText);


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

--
Andrew Thompson
http://www.athompson.info/andrew/

Message posted via JavaKB.com
http://www.javakb.com/Uwe/Forums.asp...neral/200710/1

 
Reply With Quote
 
 
 
 
David Zimmerman
Guest
Posts: n/a
 
      10-09-2007


Andrew Thompson wrote:
> Alan wrote:
>> When I write out a StringBuffer, I lose all the original line
>> breaks.

>
> . So put them back in.
>
> ..
>> sb.append(lineOfText);

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


sb.append(lineOfText).append(System.getProperty("l ine.separator"));

You have perfectly good StringBuffer, might as well use it.
 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      10-10-2007
On Mon, 08 Oct 2007 02:52:45 -0000, Alan <(E-Mail Removed)>
wrote, quoted or indirectly quoted someone who said :

>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" );
--
Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
efficient way to replace char in StringBuffer Liz Java 1 08-02-2004 08:57 AM
String concatenation versus StringBuffer Darren Java 5 07-28-2004 09:35 AM
hash for String and StringBuffer John Galt Java 4 02-25-2004 11:01 PM
buff = new StringBuffer(10000); buff.toString(); Will GC be complete here? qazmlp Java 5 02-19-2004 04:07 PM
performance string, stringbuffer, and stringtoken Mike mmccaws@netscape.net Java 3 07-07-2003 02:51 PM



Advertisments