25package umontreal.ssj.util;
27import java.io.LineNumberReader;
29import java.io.InputStreamReader;
30import java.io.FileReader;
31import java.io.IOException;
34import java.util.logging.Logger;
42 private static Logger log = Logger.getLogger(
"umontreal.ssj.util");
59 LineNumberReader inb =
new LineNumberReader(input);
60 double[] data =
new double[5];
63 while ((li = inb.readLine()) !=
null) {
65 if (li.startsWith(
"#"))
70 while (index < li.length() && (li.charAt(index) ==
'+' || li.charAt(index) ==
'-' || li.charAt(index) ==
'e'
71 || li.charAt(index) ==
'E' || li.charAt(index) ==
'.' || Character.isDigit(li.charAt(index))))
75 li = li.substring(0, index);
78 data[n++] = Double.parseDouble(li);
79 if (n >= data.length) {
80 double[] newData =
new double[2 * n];
81 System.arraycopy(data, 0, newData, 0, data.length);
84 }
catch (NumberFormatException nfe) {
85 log.warning(
"Invalid line " + inb.getLineNumber() +
": " + li);
89 if (data.length != n) {
90 double[] data2 =
new double[n];
91 System.arraycopy(data, 0, data2, 0, n);
108 Reader reader =
new InputStreamReader(url.openStream());
127 FileReader reader =
new FileReader(file);
145 FileReader reader =
new FileReader(file);
161 public static int[]
readIntData(Reader input)
throws IOException {
162 LineNumberReader inb =
new LineNumberReader(input);
163 int[] data =
new int[5];
166 while ((li = inb.readLine()) !=
null) {
168 if (li.startsWith(
"#"))
173 while (index < li.length()
174 && (li.charAt(index) ==
'+' || li.charAt(index) ==
'-' || Character.isDigit(li.charAt(index))))
178 li = li.substring(0, index);
179 if (!li.equals(
"")) {
181 data[n++] = Integer.parseInt(li);
182 if (n >= data.length) {
183 int[] newData =
new int[2 * n];
184 System.arraycopy(data, 0, newData, 0, data.length);
187 }
catch (NumberFormatException nfe) {
188 log.warning(
"Invalid line " + inb.getLineNumber() +
": " + li);
192 if (data.length != n) {
193 int[] data2 =
new int[n];
194 System.arraycopy(data, 0, data2, 0, n);
210 Reader reader =
new InputStreamReader(url.openStream());
227 FileReader reader =
new FileReader(file);
244 FileReader reader =
new FileReader(file);
265 LineNumberReader inb =
new LineNumberReader(input);
266 String[] data =
new String[5];
269 while ((li = inb.readLine()) !=
null) {
271 if (li.startsWith(
"#"))
275 if (n >= data.length) {
276 String[] newData =
new String[2 * n];
277 System.arraycopy(data, 0, newData, 0, data.length);
281 if (data.length != n) {
282 String[] data2 =
new String[n];
283 System.arraycopy(data, 0, data2, 0, n);
299 Reader reader =
new InputStreamReader(url.openStream());
316 FileReader reader =
new FileReader(file);
333 FileReader reader =
new FileReader(file);
360 LineNumberReader inb =
new LineNumberReader(input);
361 double[][] data =
new double[5][];
366 while ((li = inb.readLine()) !=
null) {
368 if (li.startsWith(
"#"))
371 if (li.equals(
";")) {
372 data[n++] =
new double[0];
379 double[] row =
new double[5];
382 while (index < li.length() && (!end)) {
383 while (index < li.length() && (li.charAt(index) ==
'+' || li.charAt(index) ==
'-'
384 || li.charAt(index) ==
'e' || li.charAt(index) ==
'E' || li.charAt(index) ==
'.'
385 || Character.isDigit(li.charAt(index))))
388 if (index >= li.length() || (Character.isWhitespace(li.charAt(index)))) {
389 number = li.substring(begin, index);
392 if (!number.equals(
"")) {
394 row[k++] = Double.parseDouble(number);
395 if (k >= row.length) {
396 double[] newRow =
new double[2 * k];
397 System.arraycopy(row, 0, newRow, 0, row.length);
400 }
catch (NumberFormatException nfe) {
401 log.warning(
"Invalid column " + k +
" at line " + inb.getLineNumber() +
": " + number);
410 data[n] =
new double[k];
411 System.arraycopy(row, 0, data[n], 0, k);
414 log.warning(
"Invalid line " + inb.getLineNumber() +
": " + li);
418 if (n == data.length) {
419 double[][] newData =
new double[2 * n][];
420 System.arraycopy(data, 0, newData, 0, n);
425 double[][] data2 =
new double[n][];
426 System.arraycopy(data, 0, data2, 0, n);
441 Reader reader =
new InputStreamReader(url.openStream());
460 FileReader reader =
new FileReader(file);
478 FileReader reader =
new FileReader(file);
495 LineNumberReader inb =
new LineNumberReader(input);
496 int[][] data =
new int[5][];
501 while ((li = inb.readLine()) !=
null) {
503 if (li.startsWith(
"#"))
506 if (li.equals(
";")) {
507 data[n++] =
new int[0];
514 int[] row =
new int[5];
517 while (index < li.length() && (!end)) {
518 while (index < li.length()
519 && (li.charAt(index) ==
'+' || li.charAt(index) ==
'-' || Character.isDigit(li.charAt(index))))
522 if (index >= li.length() || (Character.isWhitespace(li.charAt(index)))) {
523 number = li.substring(begin, index);
526 if (!number.equals(
"")) {
528 row[k++] = Integer.parseInt(number);
529 if (k >= row.length) {
530 int[] newRow =
new int[2 * k];
531 System.arraycopy(row, 0, newRow, 0, row.length);
534 }
catch (NumberFormatException nfe) {
535 log.warning(
"Invalid column " + k +
" at line " + inb.getLineNumber() +
": " + number);
544 data[n] =
new int[k];
545 System.arraycopy(row, 0, data[n], 0, k);
548 log.warning(
"Invalid line " + inb.getLineNumber() +
": " + li);
552 if (n == data.length) {
553 int[][] newData =
new int[2 * n][];
554 System.arraycopy(data, 0, newData, 0, n);
559 int[][] data2 =
new int[n][];
560 System.arraycopy(data, 0, data2, 0, n);
574 Reader reader =
new InputStreamReader(url.openStream());
591 FileReader reader =
new FileReader(file);
608 FileReader reader =
new FileReader(file);
644 public static String[][]
readCSVData(Reader input,
char colDelim,
char stringDelim)
throws IOException {
647 LineNumberReader inb =
new LineNumberReader(input);
648 StringBuffer sb =
new StringBuffer();
649 boolean stringMode =
false;
650 String[][] data =
new String[5][];
653 boolean newRow =
false, newColumn =
false;
656 boolean readDone =
false;
665 newRow = newColumn = readDone =
true;
671 if (ch == stringDelim) {
673 int ichNext = inb.read();
675 char chNext = (char) ichNext;
676 if (chNext == stringDelim)
678 sb.append(stringDelim);
689 if (ch ==
'\n' || ch ==
'\r') {
690 int ichNext = inb.read();
692 char chNext = (char) ichNext;
693 if (ch ==
'\r' && chNext ==
'\n') {
694 ichNext = inb.read();
696 chNext = (char) ichNext;
707 }
else if (ch == colDelim)
709 else if (ch == stringDelim)
715 if (newColumn || newRow) {
716 if (numColumns == 0) {
721 if (data.length < numRows) {
722 String[][] newData =
new String[2 * data.length][];
723 System.arraycopy(data, 0, newData, 0, data.length);
726 if (data[numRows - 1] ==
null)
727 data[numRows - 1] =
new String[5];
728 else if (data[numRows - 1].length < numColumns) {
729 String[] newData =
new String[2 * data[numRows - 1].length];
730 System.arraycopy(data[numRows - 1], 0, newData, 0, data[numRows - 1].length);
731 data[numRows - 1] = newData;
733 data[numRows - 1][numColumns - 1] = sb.toString();
734 sb.delete(0, sb.length());
738 if (data[numRows - 1].length != numColumns) {
739 String[] data2 =
new String[numColumns];
740 System.arraycopy(data[numRows - 1], 0, data2, 0, numColumns);
741 data[numRows - 1] = data2;
749 throw new IllegalArgumentException(
"Too many string delimiters " + stringDelim);
750 if (data.length != numRows) {
751 String[][] data2 =
new String[numRows][];
752 System.arraycopy(data, 0, data2, 0, numRows);
770 public static String[][]
readCSVData(URL url,
char colDelim,
char stringDelim)
throws IOException {
771 Reader reader =
new InputStreamReader(url.openStream());
789 public static String[][]
readCSVData(File file,
char colDelim,
char stringDelim)
throws IOException {
790 FileReader reader =
new FileReader(file);
808 public static String[][]
readCSVData(String file,
char colDelim,
char stringDelim)
throws IOException {
809 FileReader reader =
new FileReader(file);
Provides static methods to read data from text files.
static String[][] readCSVData(Reader input, char colDelim, char stringDelim)
Reads comma-separated values (CSV) from reader input, and returns a 2D array of strings corresponding...
static String[][] readCSVData(URL url, char colDelim, char stringDelim)
Connects to the URL referred to by the URL object url, and calls readCSVData(Reader,...
static int[] readIntData(File file)
This is equivalent to readDoubleData(File), for reading integers.
static double[][] readDoubleData2D(File file)
Opens the file referred to by the file object file, and calls readDoubleData2D(Reader) to obtain a ma...
static double[][] readDoubleData2D(Reader input)
Uses the reader input to obtain a 2-dimensional array of double-precision values.
static double[][] readDoubleData2D(URL url)
Connects to the URL referred to by the URL object url, and calls readDoubleData2D(Reader) to obtain a...
static double[] readDoubleData(URL url)
Connects to the URL referred to by the URL object url, and calls readDoubleData(Reader) to obtain an ...
static String[] readStringData(String file)
This is equivalent to readDoubleData(String), for reading strings.
static String[] readStringData(File file)
This is equivalent to readDoubleData(File), for reading strings.
static double[] readDoubleData(Reader input)
Reads an array of double-precision values from the reader input.
static int[] readIntData(Reader input)
This is equivalent to readDoubleData(Reader), for reading integers.
static String[][] readCSVData(File file, char colDelim, char stringDelim)
This is equivalent to readDoubleData2D(File), for reading strings.
static int[] readIntData(String file)
This is equivalent to readDoubleData(String), for reading integers.
static double[][] readDoubleData2D(String file)
Opens the file with name file, and calls readDoubleData2D(Reader) to obtain a matrix of double-precis...
static int[][] readIntData2D(Reader input)
This is equivalent to readDoubleData2D(Reader), for reading integers.
static String[][] readCSVData(String file, char colDelim, char stringDelim)
This is equivalent to readDoubleData2D(String), for reading strings.
static int[][] readIntData2D(URL url)
Connects to the URL referred to by the URL object url, and calls readDoubleData(Reader) to obtain a m...
static double[] readDoubleData(File file)
Opens the file referred to by the file object file, and calls readDoubleData(Reader) to obtain an arr...
static int[][] readIntData2D(String file)
This is equivalent to readDoubleData2D(String), for reading integers.
static int[][] readIntData2D(File file)
This is equivalent to readDoubleData2D(File), for reading integers.
static String[] readStringData(Reader input)
Reads an array of strings from the reader input.
static int[] readIntData(URL url)
Connects to the URL referred to by the URL object url, and calls readIntData(Reader) to obtain an arr...
static double[] readDoubleData(String file)
Opens the file with name file, and calls readDoubleData(Reader) to obtain an array of double-precisio...
static String[] readStringData(URL url)
Connects to the URL referred to by the URL object url, and calls readStringData(Reader) to obtain an ...