1 package JSci.io; 2 3 import java.io.*; 4 import JSci.maths.matrices.AbstractDoubleMatrix; 5 import JSci.maths.matrices.AbstractIntegerMatrix; 6 7 13 public final class TextWriter extends OutputStreamWriter { 14 private final BufferedWriter writer=new BufferedWriter(this); 15 private char delimiter; 16 19 public TextWriter(OutputStream stream) { 20 super(stream); 21 } 22 28 public TextWriter(String name,char ch) throws IOException { 29 super(new FileOutputStream(name)); 30 delimiter=ch; 31 } 32 38 public TextWriter(File file,char ch) throws IOException { 39 super(new FileOutputStream(file)); 40 delimiter=ch; 41 } 42 46 public void write(int c) throws IOException { 47 writer.write(c); 48 } 49 53 public void write(String str) throws IOException { 54 writer.write(str); 55 } 56 60 public void close() throws IOException { 61 writer.flush(); 62 super.close(); 63 } 64 69 public void write(double data[]) throws IOException { 70 int i; 71 for(i=0;i<data.length-1;i++) 72 write(data[i]+" "+delimiter+" "); 73 write(data[i]+"\n"); 74 } 75 80 public void write(double data[][]) throws IOException { 81 for(int j,i=0;i<data.length;i++) { 82 for(j=0;j<data[i].length-1;j++) 83 write(data[i][j]+" "+delimiter+" "); 84 write(data[i][j]+"\n"); 85 } 86 } 87 92 public void write(int data[]) throws IOException { 93 int i; 94 for(i=0;i<data.length-1;i++) 95 write(data[i]+" "+delimiter+" "); 96 write(data[i]+"\n"); 97 } 98 103 public void write(int data[][]) throws IOException { 104 for(int j,i=0;i<data.length;i++) { 105 for(j=0;j<data[i].length-1;j++) 106 write(data[i][j]+" "+delimiter+" "); 107 write(data[i][j]+"\n"); 108 } 109 } 110 115 public void write(AbstractDoubleMatrix matrix) throws IOException { 116 final int mRow=matrix.rows(); 117 final int mCol=matrix.columns(); 118 for(int j,i=0;i<mRow;i++) { 119 for(j=0;j<mCol-1;j++) 120 write(matrix.getElement(i,j)+" "+delimiter+" "); 121 write(matrix.getElement(i,j)+"\n"); 122 } 123 } 124 129 public void write(AbstractIntegerMatrix matrix) throws IOException { 130 final int mRow=matrix.rows(); 131 final int mCol=matrix.columns(); 132 for(int j,i=0;i<mRow;i++) { 133 for(j=0;j<mCol-1;j++) 134 write(matrix.getElement(i,j)+" "+delimiter+" "); 135 write(matrix.getElement(i,j)+"\n"); 136 } 137 } 138 } 139 140 | Popular Tags |