KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > JSci > io > TextWriter


1 package JSci.io;
2
3 import java.io.*;
4 import JSci.maths.matrices.AbstractDoubleMatrix;
5 import JSci.maths.matrices.AbstractIntegerMatrix;
6
7 /**
8 * Text writer, writes data text files/streams.
9 * This class uses buffered I/O.
10 * @version 1.7
11 * @author Mark Hale
12 */

13 public final class TextWriter extends OutputStreamWriter {
14         private final BufferedWriter writer=new BufferedWriter(this);
15         private char delimiter;
16         /**
17         * Writes text data to an output stream.
18         */

19         public TextWriter(OutputStream stream) {
20                 super(stream);
21         }
22         /**
23         * Writes to a text file with the specified system dependent file name.
24         * @param name the system dependent file name.
25         * @param ch the character that delimits data columns.
26         * @exception IOException If the file is not found.
27         */

28         public TextWriter(String JavaDoc name,char ch) throws IOException {
29                 super(new FileOutputStream(name));
30                 delimiter=ch;
31         }
32         /**
33         * Writes to a text file with the specified File object.
34         * @param file the file to be opened for writing.
35         * @param ch the character that delimits data columns.
36         * @exception IOException If the file is not found.
37         */

38         public TextWriter(File file,char ch) throws IOException {
39                 super(new FileOutputStream(file));
40                 delimiter=ch;
41         }
42         /**
43         * Writes a single character.
44         * @exception IOException If an I/O error occurs.
45         */

46         public void write(int c) throws IOException {
47                 writer.write(c);
48         }
49         /**
50         * Writes a string.
51         * @exception IOException If an I/O error occurs.
52         */

53         public void write(String JavaDoc str) throws IOException {
54                 writer.write(str);
55         }
56         /**
57         * Close the stream.
58         * @exception IOException If an I/O error occurs.
59         */

60         public void close() throws IOException {
61                 writer.flush();
62                 super.close();
63         }
64         /**
65         * Writes an array of data.
66         * @param data the data to be written.
67         * @exception IOException If an I/O error occurs.
68         */

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         /**
76         * Writes an array of data.
77         * @param data the data to be written.
78         * @exception IOException If an I/O error occurs.
79         */

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         /**
88         * Writes an array of data.
89         * @param data the data to be written.
90         * @exception IOException If an I/O error occurs.
91         */

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         /**
99         * Writes an array of data.
100         * @param data the data to be written.
101         * @exception IOException If an I/O error occurs.
102         */

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         /**
111         * Writes a matrix.
112         * @param matrix the matrix to be written.
113         * @exception IOException If an I/O error occurs.
114         */

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         /**
125         * Writes a matrix.
126         * @param matrix the matrix to be written.
127         * @exception IOException If an I/O error occurs.
128         */

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