Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!aioe.org!eternal-september.org!feeder.eternal-september.org!mx04.eternal-september.org!.POSTED!not-for-mail From: Jeff Higgins Newsgroups: comp.lang.java.programmer Subject: Re: append() vs. write() Date: Tue, 10 Jan 2012 13:14:41 -0500 Organization: A noiseless patient Spider Lines: 48 Message-ID: References: <3d2746a6-f57c-4fa1-b547-dfddc23bb6b6@n30g2000yqd.googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Injection-Date: Tue, 10 Jan 2012 18:07:45 +0000 (UTC) Injection-Info: mx04.eternal-september.org; posting-host="qwFw1g9RsQ6TkML5yezG9A"; logging-data="11842"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/DVMd4JeFNoJ2Dl4CVa2/s6Px8RwLaGFg=" User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.1.16) Gecko/20111110 Icedove/3.0.11 In-Reply-To: <3d2746a6-f57c-4fa1-b547-dfddc23bb6b6@n30g2000yqd.googlegroups.com> Cancel-Lock: sha1:Pe2TUfnscNHlloV7CEoaNLWO89c= Xref: x330-a1.tempe.blueboxinc.net comp.lang.java.programmer:11185 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))?