Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #11178 > unrolled thread

append() vs. write()

Started byBenjamin Trendelkamp-Schroer <benjamin.trendelkampschroer@googlemail.com>
First post2012-01-10 08:17 -0800
Last post2012-01-10 18:06 -0800
Articles 8 — 4 participants

Back to article view | Back to comp.lang.java.programmer


Contents

  append() vs. write() Benjamin Trendelkamp-Schroer <benjamin.trendelkampschroer@googlemail.com> - 2012-01-10 08:17 -0800
    Re: append() vs. write() Jeff Higgins <jeff@invalid.invalid> - 2012-01-10 13:14 -0500
    Re: append() vs. write() markspace <-@.> - 2012-01-10 13:30 -0800
    Re: append() vs. write() Jeff Higgins <jeff@invalid.invalid> - 2012-01-10 16:48 -0500
      Re: append() vs. write() Jeff Higgins <jeff@invalid.invalid> - 2012-01-10 17:15 -0500
        Re: append() vs. write() Jeff Higgins <jeff@invalid.invalid> - 2012-01-10 18:35 -0500
          Re: append() vs. write() Jeff Higgins <jeff@invalid.invalid> - 2012-01-10 19:01 -0500
    Re: append() vs. write() Lew <noone@lewscanon.com> - 2012-01-10 18:06 -0800

#11178 — append() vs. write()

FromBenjamin Trendelkamp-Schroer <benjamin.trendelkampschroer@googlemail.com>
Date2012-01-10 08:17 -0800
Subjectappend() vs. write()
Message-ID<3d2746a6-f57c-4fa1-b547-dfddc23bb6b6@n30g2000yqd.googlegroups.com>
Hi,

I want to write a method that can write possibly large matrices of
floating point numbers in scientific notation to human readable ascii
files. I want to be able to specify the formatting of the floating
point numbers usins format strings like "%1.8e" or "%2.5f".

From what I take from the java doc. I can do something like

i) java.io.File file = new java.io.File(pathnameOfMyFile);
  java.io.FileWriter fileWriter = new java.io.FileWriter(file);
  java.util.Formatter f = new java.util.Formatter(fileWriter);
  for(int i ...){
     for(int j ...){
        f.format("%1.8e", Matrix.getEntry(i,j))
        f.format("%s", columnSeparator)    //columnSeparator = "
" (for example)
     }
     f.format("%s", rowSeparator) // rowSeparator="\n" (for example)
  }

ii) Use a buffered writer which is adviced as being good practice in
all tutorials since it has a buffered write method. But as far as I
understand Formatter does only use the format method of the Appendable
interface and not the bufered write method. If my Matrix gets very
large (say 10000 rows and columns) I would have to put each row in an
appropriately formatted String o0r StringBuffer, calling the append
method on the StringBuffer through the format method of Formatter many
times and use write to output it to the buffered writer. But this
string would be quite large + I would have to create that string and
the Formatter for each new row (using for example
StringBuffer.toString(), Formatter(StringBuffer)).

I am asking because I am not sure what is the best practice hear. My
favorite solution would be to have a method that I could just pass a
Formatter to so that I could use that formatter object to call other
methods doing the formatting on the level of single entries of my
matrix without the need to create a lot of formatter objects. But on
the other hand I would like to make writing out to files as fast as
possible and do not want to suffer performance penalties from
repeatedly calling an append() method where it would be advisable to
make fewer calls to write with string containing more characters.

I am quite new to Java so I would appreciate any help with that and
ask you to excuse any obvious mistakes and style flaws that I have
made.

[toc] | [next] | [standalone]


#11185

FromJeff Higgins <jeff@invalid.invalid>
Date2012-01-10 13:14 -0500
Message-ID<jehuph$bi2$1@dont-email.me>
In reply to#11178
On 01/10/2012 11:17 AM, Benjamin Trendelkamp-Schroer wrote:
> Hi,
>
> I want to write a method that can write possibly large matrices of
> floating point numbers in scientific notation to human readable ascii
> files. I want to be able to specify the formatting of the floating
> point numbers usins format strings like "%1.8e" or "%2.5f".
>
>  From what I take from the java doc. I can do something like
>
> i) java.io.File file = new java.io.File(pathnameOfMyFile);
>    java.io.FileWriter fileWriter = new java.io.FileWriter(file);
>    java.util.Formatter f = new java.util.Formatter(fileWriter);
>    for(int i ...){
>       for(int j ...){
>          f.format("%1.8e", Matrix.getEntry(i,j))
>          f.format("%s", columnSeparator)    //columnSeparator = "
> " (for example)
>       }
>       f.format("%s", rowSeparator) // rowSeparator="\n" (for example)
>    }
>
> ii) Use a buffered writer which is adviced as being good practice in
> all tutorials since it has a buffered write method. But as far as I
> understand Formatter does only use the format method of the Appendable
> interface and not the bufered write method. If my Matrix gets very
> large (say 10000 rows and columns) I would have to put each row in an
> appropriately formatted String o0r StringBuffer, calling the append
> method on the StringBuffer through the format method of Formatter many
> times and use write to output it to the buffered writer. But this
> string would be quite large + I would have to create that string and
> the Formatter for each new row (using for example
> StringBuffer.toString(), Formatter(StringBuffer)).
>
> I am asking because I am not sure what is the best practice hear. My
> favorite solution would be to have a method that I could just pass a
> Formatter to so that I could use that formatter object to call other
> methods doing the formatting on the level of single entries of my
> matrix without the need to create a lot of formatter objects. But on
> the other hand I would like to make writing out to files as fast as
> possible and do not want to suffer performance penalties from
> repeatedly calling an append() method where it would be advisable to
> make fewer calls to write with string containing more characters.
>
> I am quite new to Java so I would appreciate any help with that and
> ask you to excuse any obvious mistakes and style flaws that I have
> made.
PrintWriter.append(DecimalFormat.format(myFPNumber,null,null))?

[toc] | [prev] | [next] | [standalone]


#11188

Frommarkspace <-@.>
Date2012-01-10 13:30 -0800
Message-ID<jeialn$pco$1@dont-email.me>
In reply to#11178
On 1/10/2012 8:17 AM, Benjamin Trendelkamp-Schroer wrote:

> ii) Use a buffered writer which is adviced as being good practice in
> all tutorials since it has a buffered write method. But as far as I
> understand Formatter does only use the format method of the Appendable
> interface and not the bufered write method. If my Matrix gets very


Well, no and no.  No, Formatter has several constructors with different 
arguments, one of which is Appendable.

But also, no you CAN use BufferedWriter because it is an Appendable 
also.  Double check my reading of the docs but I'm sure that's what it says.

<http://docs.oracle.com/javase/7/docs/api/java/io/BufferedWriter.html>

"All Implemented Interfaces:
     Closeable, Flushable, Appendable, AutoCloseable"

So you can just use a BufferedWriter, no problem.

[toc] | [prev] | [next] | [standalone]


#11190

FromJeff Higgins <jeff@invalid.invalid>
Date2012-01-10 16:48 -0500
Message-ID<jeib9l$shf$1@dont-email.me>
In reply to#11178
On 01/10/2012 11:17 AM, Benjamin Trendelkamp-Schroer wrote:
> Hi,
>
> I want to write a method that can write possibly large matrices of
> floating point numbers in scientific notation to human readable ascii
> files. I want to be able to specify the formatting of the floating
> point numbers usins format strings like "%1.8e" or "%2.5f".
>
>  From what I take from the java doc. I can do something like
>
> i) java.io.File file = new java.io.File(pathnameOfMyFile);
>    java.io.FileWriter fileWriter = new java.io.FileWriter(file);
>    java.util.Formatter f = new java.util.Formatter(fileWriter);
>    for(int i ...){
>       for(int j ...){
>          f.format("%1.8e", Matrix.getEntry(i,j))
>          f.format("%s", columnSeparator)    //columnSeparator = "
> " (for example)
>       }
>       f.format("%s", rowSeparator) // rowSeparator="\n" (for example)
>    }
>
> ii) Use a buffered writer which is adviced as being good practice in
> all tutorials since it has a buffered write method. But as far as I
> understand Formatter does only use the format method of the Appendable
> interface and not the bufered write method. If my Matrix gets very
> large (say 10000 rows and columns) I would have to put each row in an
> appropriately formatted String o0r StringBuffer, calling the append
> method on the StringBuffer through the format method of Formatter many
> times and use write to output it to the buffered writer. But this
> string would be quite large + I would have to create that string and
> the Formatter for each new row (using for example
> StringBuffer.toString(), Formatter(StringBuffer)).
>
> I am asking because I am not sure what is the best practice hear. My
> favorite solution would be to have a method that I could just pass a
> Formatter to so that I could use that formatter object to call other
> methods doing the formatting on the level of single entries of my
> matrix without the need to create a lot of formatter objects. But on
> the other hand I would like to make writing out to files as fast as
> possible and do not want to suffer performance penalties from
> repeatedly calling an append() method where it would be advisable to
> make fewer calls to write with string containing more characters.
>
> I am quite new to Java so I would appreciate any help with that and
> ask you to excuse any obvious mistakes and style flaws that I have
> made.
I'll try again.

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.Writer;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.Collection;


public class Scratch {

   public static void main(String[] args) throws IOException {

     Writer writer = new PrintWriter(
         new BufferedWriter(
             new FileWriter(args[0])));

     String tableHeader, tableFooter,
     rowHeader, rowFooter, columnSeparator;

     // not intended to compile

     Matrix matrix;
     MatrixRow row;

     writer.append(tableHeader);
     while (matrix.hasRows()) {
       row = matrix.nextRow();
       writer.append(rowHeader);
       while (row.hasNextCell()) {
         // java.io.Writer has an append method that takes a CharSequence
         writer.append(
             getCellFormatter(row.rowIndex, row.columnIndex)
             //DecimalFormat has a format method that returns a 
StringBuilder
             //StringBuilder implements CharSequence
             .format(row.nextCell().doubleValue(),null,null));
         writer.append(columnSeparator);
       }
       writer.append(rowFooter);
     }
     writer.append(tableFooter);
   }
}

/* probably in your Matrix class

DecimalFormat getCellFormatter(int rowIndex, int columnIndex) {
   DecimalFormat formatter =
       (DecimalFormat)DecimalFormat.getInstance();
   // apply a pattern depending upon row and column
   formatter.applyPattern("yourPattern");
   return formatter;
}
*/

[toc] | [prev] | [next] | [standalone]


#11191

FromJeff Higgins <jeff@invalid.invalid>
Date2012-01-10 17:15 -0500
Message-ID<jeicso$7pr$1@dont-email.me>
In reply to#11190
On 01/10/2012 04:48 PM, Jeff Higgins wrote:
> On 01/10/2012 11:17 AM, Benjamin Trendelkamp-Schroer wrote:
>> Hi,
>>
>> I want to write a method that can write possibly large matrices of
>> floating point numbers in scientific notation to human readable ascii
>> files. I want to be able to specify the formatting of the floating
>> point numbers usins format strings like "%1.8e" or "%2.5f".
>>
Oops! java.utilFormatter Duh. Sorry, please ignore previous post.

[toc] | [prev] | [next] | [standalone]


#11197

FromJeff Higgins <jeff@invalid.invalid>
Date2012-01-10 18:35 -0500
Message-ID<jeihiq$8dr$1@dont-email.me>
In reply to#11191
On 01/10/2012 05:15 PM, Jeff Higgins wrote:
> On 01/10/2012 04:48 PM, Jeff Higgins wrote:
>> On 01/10/2012 11:17 AM, Benjamin Trendelkamp-Schroer wrote:
>>> Hi,
>>>
>>> I want to write a method that can write possibly large matrices of
>>> floating point numbers in scientific notation to human readable ascii
>>> files. I want to be able to specify the formatting of the floating
>>> point numbers usins format strings like "%1.8e" or "%2.5f".
>>>
> Oops! java.utilFormatter Duh. Sorry, please ignore previous post.

Well,maybe not all is lost.
PrintWriter does have a printf( String format, Object... args).
So I guess you can lose the DecimalFormat for the printf.

[toc] | [prev] | [next] | [standalone]


#11198

FromJeff Higgins <jeff@invalid.invalid>
Date2012-01-10 19:01 -0500
Message-ID<jeij4b$fkq$1@dont-email.me>
In reply to#11197
On 01/10/2012 06:35 PM, Jeff Higgins wrote:
> On 01/10/2012 05:15 PM, Jeff Higgins wrote:
>> On 01/10/2012 04:48 PM, Jeff Higgins wrote:
>>> On 01/10/2012 11:17 AM, Benjamin Trendelkamp-Schroer wrote:
>>>> Hi,
>>>>
>>>> I want to write a method that can write possibly large matrices of
>>>> floating point numbers in scientific notation to human readable ascii
>>>> files. I want to be able to specify the formatting of the floating
>>>> point numbers usins format strings like "%1.8e" or "%2.5f".
>>>>
>> Oops! java.utilFormatter Duh. Sorry, please ignore previous post.
>
> Well,maybe not all is lost.
> PrintWriter does have a printf( String format, Object... args).
> So I guess you can lose the DecimalFormat for the printf.

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.Writer;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.Collection;


public class Scratch {

   public static void main(String[] args) throws IOException {

     PrintWriter writer = new PrintWriter(
         new BufferedWriter(
             new FileWriter(args[0])));

     String tableHeader, tableFooter,
     rowHeader, rowFooter, columnSeparator;

     // not intended to compile

     Matrix matrix;
     MatrixRow row;

     writer.print(tableHeader);
     while (matrix.hasRows()) {
       row = matrix.nextRow();
       writer.print(rowHeader);
       while (row.hasNextCell()) {
         printFormattedCellValue(
 
writer,row.nextCell().doubleValue(),row.rowIndex,row.columnIndex);
         writer.print(columnSeparator);
       }
       writer.append(rowFooter);
     }
     writer.append(tableFooter);
   }
}
/* probably in your Matrix class

void printFormattedCellValue(
   PrintWriter writer, double value, int rowIndex, int columnIndex) {

   // apply a format depending upon rowIndex and columnIndex
   String format;
   Object[] args;
   writer.printf(format,value,args[0]...);
}
*/

[toc] | [prev] | [next] | [standalone]


#11204

FromLew <noone@lewscanon.com>
Date2012-01-10 18:06 -0800
Message-ID<jeiqqm$7v0$1@news.albasani.net>
In reply to#11178
Benjamin Trendelkamp-Schroer wrote:
> I want to write a method that can write possibly large matrices of
> floating point numbers in scientific notation to human readable ascii
> files. I want to be able to specify the formatting of the floating
> point numbers usins format strings like "%1.8e" or "%2.5f".

Everyone else has provided great answers, especially Jeff Higgins, so I won't 
repeat what they said.

>  From what I take from the java doc [sic]. I can do something like
>
> i) java.io.File file = new java.io.File(pathnameOfMyFile);
>    java.io.FileWriter fileWriter = new java.io.FileWriter(file);
>    java.util.Formatter f = new java.util.Formatter(fileWriter);
>    for(int i ...){
>       for(int j ...){
>          f.format("%1.8e", Matrix.getEntry(i,j))
>          f.format("%s", columnSeparator)    //columnSeparator = "
> " (for example)
>       }
>       f.format("%s", rowSeparator) // rowSeparator="\n" (for example)
>    }
>
> ii) Use a buffered writer which is adviced [sic] as being good practice in
> all tutorials since it has a buffered write method. But as far as I
> understand Formatter does only use the format method of the Appendable
> interface and not the bufered write method. If my Matrix gets very

You need to keep reading Javadocs.  You missed something, but again others 
have pointed it out already.

> large (say 10000 rows and columns) I would have to put each row in an
> appropriately formatted String o0r StringBuffer, calling the append

Why do you insist on 'StringBuffer'?  What does the synchronization provide 
that you need?

Not that you need it or 'StringBuilder', but I'm curious why you went with 
'StringBuffer'.

> method on the StringBuffer through the format method of Formatter many
> times and use write to output it to the buffered writer. But this
> string would be quite large + I would have to create that string and
> the Formatter for each new row (using for example
> StringBuffer.toString(), Formatter(StringBuffer)).
>
> I am asking because I am not sure what is the best practice hear [sic]. My
> favorite solution would be to have a method that I could just pass a
> Formatter to so that I could use that formatter object to call other
> methods doing the formatting on the level of single entries of my
> matrix without the need to create a lot of formatter objects. But on
> the other hand I would like to make writing out to files as fast as
> possible and do not want to suffer performance penalties from
> repeatedly calling an append() method where it would be advisable to
> make fewer calls to write with string containing more characters.

You don't know what your performance will be *until* you measure.  Have you 
measured?  If not, why are you worried about performance?

Go with better code and generally you'll see the performance you want.

> I am quite new to Java so I would appreciate any help with that and
> ask you to excuse any obvious mistakes and style flaws that I have
> made.

There's no point in excusing flaws at the beginning, because they become bad 
habits that then you will have great ego defense over when people criticize 
your mistakes, especially the obvious ones.  Don't come asking for help and 
then ask people not to give it.  Every bit of "criticism" you get now will 
help you be a better programmer.  Far from repelling the feedback, you should 
beg for it.

To start with, the language is "Java", not "java", the documentation comments 
produce "Javadocs", not "java docs", and you should usually prefer 
'StringBuilder' to 'StringBuffer'.

-- 
Lew
Honi soit qui mal y pense.
http://upload.wikimedia.org/wikipedia/commons/c/cf/Friz.jpg

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.java.programmer


csiph-web