Re: append() vs. write()
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]...);
}
*/