Writing out StringBuffer with proper format

Discussion in 'Java' started by Alan, Oct 8, 2007.

  1. Alan

    Alan Guest

    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>
     
    Alan, Oct 8, 2007
    #1
    1. Advertisements

  2. Andrew Thompson, Oct 8, 2007
    #2
    1. Advertisements

  3. 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("line.separator"));

    You have perfectly good StringBuffer, might as well use it.
     
    David Zimmerman, Oct 9, 2007
    #3
  4. Alan

    Roedy Green Guest

    On Mon, 08 Oct 2007 02:52:45 -0000, Alan <>
    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
     
    Roedy Green, Oct 10, 2007
    #4
    1. Advertisements

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mike
    Replies:
    3
    Views:
    2,092
  2. gk

    proper format

    gk, Dec 27, 2005, in forum: Java
    Replies:
    4
    Views:
    530
    drwsh
    Dec 30, 2005
  3. Chance Ginger
    Replies:
    1
    Views:
    597
    Diez B. Roggisch
    Mar 14, 2006
  4. John Salerno

    proper format for this database table

    John Salerno, Oct 19, 2006, in forum: Python
    Replies:
    16
    Views:
    597
    Dennis Lee Bieber
    Oct 21, 2006
  5. Ven
    Replies:
    6
    Views:
    689
  6. Proper strinfg format

    , Jun 21, 2007, in forum: ASP .Net
    Replies:
    4
    Views:
    1,076
    David R. Longnecker
    Jun 21, 2007
  7. Benoit Daloze

    Writing proper getter in a Ruby way

    Benoit Daloze, Jan 23, 2010, in forum: Ruby
    Replies:
    8
    Views:
    323
    Benoit Daloze
    Jan 24, 2010
  8. Öö Tiib
    Replies:
    2
    Views:
    358
    Jason Benjamin
    Oct 31, 2012
Loading...