KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > JSci > io > TextReader


1 package JSci.io;
2
3 import java.lang.Double JavaDoc;
4 import java.io.*;
5 import java.util.Vector JavaDoc;
6
7 import JSci.maths.*;
8
9 /**
10 * Text reader, reads data text files/streams.
11 * This class uses buffered I/O.
12 * @version 1.7
13 * @author Mark Hale
14 */

15 public final class TextReader extends InputStreamReader {
16         private final BufferedReader reader=new BufferedReader(this);
17         /**
18         * Reads text data from an input stream.
19         */

20         public TextReader(InputStream stream) {
21                 super(stream);
22         }
23         /**
24         * Reads a text file with the specified system dependent file name.
25         * @param name the system dependent file name.
26         * @exception FileNotFoundException If the file is not found.
27         */

28         public TextReader(String JavaDoc name) throws FileNotFoundException {
29                 super(new FileInputStream(name));
30         }
31         /**
32         * Reads a text file with the specified File object.
33         * @param file the file to be opened for reading.
34         * @exception FileNotFoundException If the file is not found.
35         */

36         public TextReader(File file) throws FileNotFoundException {
37                 super(new FileInputStream(file));
38         }
39         /**
40         * Read a single character.
41         * @exception IOException If an I/O error occurs.
42         */

43         public int read() throws IOException {
44                 return reader.read();
45         }
46         /**
47         * Reads data to an array.
48         * @exception IOException If an I/O error occurs.
49         */

50         public double[][] readArray() throws IOException {
51                 final Vector JavaDoc data=new Vector JavaDoc(10);
52                 for(int ch=read();ch!='\n' && ch!=-1;) {
53                         if(isNumber(ch)) {
54                                 StringBuffer JavaDoc str=new StringBuffer JavaDoc();
55                                 do {
56                                         str.append((char)ch);
57                                         ch=read();
58                                 } while(isNumber(ch));
59                                 data.addElement(str.toString());
60                         }
61                         while(!isNumber(ch) && ch!='\n' && ch!=-1)
62                                 ch=read();
63                 }
64                 int cols=data.size();
65                 int rows=1;
66                 for(int ch=read();ch!=-1;) {
67                         if(isNumber(ch)) {
68                                 StringBuffer JavaDoc str=new StringBuffer JavaDoc();
69                                 do {
70                                         str.append((char)ch);
71                                         ch=read();
72                                 } while(isNumber(ch));
73                                 data.addElement(str.toString());
74                         }
75                         while(!isNumber(ch) && ch!='\n' && ch!=-1)
76                                 ch=read();
77                         if(ch=='\n') {
78                                 ch=read();
79                                 rows++;
80                         }
81                 }
82                 double array[][]=new double[rows][cols];
83                 for(int j,i=0;i<rows;i++) {
84                         for(j=0;j<cols;j++)
85                                 array[i][j]=Double.parseDouble(data.elementAt(i*cols+j).toString());
86                 }
87                 return array;
88         }
89         private boolean isNumber(int ch) {
90                 return Character.isDigit((char)ch) || ch=='.' || ch=='+' || ch=='-' || ch=='e' || ch=='E';
91         }
92 }
93
94
Popular Tags