SSJ API Documentation
Stochastic Simulation in Java
Loading...
Searching...
No Matches
TextDataReader.java
1/*
2 * Class: TextDataReader
3 * Description: Provides static methods to read data from text files
4 * Environment: Java
5 * Software: SSJ
6 * Copyright (C) 2001 Pierre L'Ecuyer and Universite de Montreal
7 * Organization: DIRO, Universite de Montreal
8 * @author
9 * @since
10 *
11 *
12 * Licensed under the Apache License, Version 2.0 (the "License");
13 * you may not use this file except in compliance with the License.
14 * You may obtain a copy of the License at
15 *
16 * http://www.apache.org/licenses/LICENSE-2.0
17 *
18 * Unless required by applicable law or agreed to in writing, software
19 * distributed under the License is distributed on an "AS IS" BASIS,
20 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
21 * See the License for the specific language governing permissions and
22 * limitations under the License.
23 *
24 */
25package umontreal.ssj.util;
26
27import java.io.LineNumberReader;
28import java.io.File;
29import java.io.InputStreamReader;
30import java.io.FileReader;
31import java.io.IOException;
32import java.io.Reader;
33import java.net.URL;
34import java.util.logging.Logger;
35
41public class TextDataReader {
42 private static Logger log = Logger.getLogger("umontreal.ssj.util");
43
58 public static double[] readDoubleData(Reader input) throws IOException {
59 LineNumberReader inb = new LineNumberReader(input);
60 double[] data = new double[5];
61 int n = 0;
62 String li;
63 while ((li = inb.readLine()) != null) {
64 li = li.trim();
65 if (li.startsWith("#"))
66 continue;
67
68 // look for the first non-digit character on the read line
69 int index = 0;
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))))
72 ++index;
73
74 // truncate the line
75 li = li.substring(0, index);
76 if (!li.equals("")) {
77 try {
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);
82 data = newData;
83 }
84 } catch (NumberFormatException nfe) {
85 log.warning("Invalid line " + inb.getLineNumber() + ": " + li);
86 }
87 }
88 }
89 if (data.length != n) {
90 double[] data2 = new double[n];
91 System.arraycopy(data, 0, data2, 0, n);
92 return data2;
93 }
94 return data;
95 }
96
107 public static double[] readDoubleData(URL url) throws IOException {
108 Reader reader = new InputStreamReader(url.openStream());
109 try {
110 return readDoubleData(reader);
111 } finally {
112 reader.close();
113 }
114 }
115
126 public static double[] readDoubleData(File file) throws IOException {
127 FileReader reader = new FileReader(file);
128 try {
129 return readDoubleData(reader);
130 } finally {
131 reader.close();
132 }
133 }
134
144 public static double[] readDoubleData(String file) throws IOException {
145 FileReader reader = new FileReader(file);
146 try {
147 return readDoubleData(reader);
148 } finally {
149 reader.close();
150 }
151 }
152
161 public static int[] readIntData(Reader input) throws IOException {
162 LineNumberReader inb = new LineNumberReader(input);
163 int[] data = new int[5];
164 int n = 0;
165 String li;
166 while ((li = inb.readLine()) != null) {
167 li = li.trim();
168 if (li.startsWith("#"))
169 continue;
170
171 // look for the first non-digit character on the read line
172 int index = 0;
173 while (index < li.length()
174 && (li.charAt(index) == '+' || li.charAt(index) == '-' || Character.isDigit(li.charAt(index))))
175 ++index;
176
177 // truncate the line
178 li = li.substring(0, index);
179 if (!li.equals("")) {
180 try {
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);
185 data = newData;
186 }
187 } catch (NumberFormatException nfe) {
188 log.warning("Invalid line " + inb.getLineNumber() + ": " + li);
189 }
190 }
191 }
192 if (data.length != n) {
193 int[] data2 = new int[n];
194 System.arraycopy(data, 0, data2, 0, n);
195 return data2;
196 }
197 return data;
198 }
199
209 public static int[] readIntData(URL url) throws IOException {
210 Reader reader = new InputStreamReader(url.openStream());
211 try {
212 return readIntData(reader);
213 } finally {
214 reader.close();
215 }
216 }
217
226 public static int[] readIntData(File file) throws IOException {
227 FileReader reader = new FileReader(file);
228 try {
229 return readIntData(reader);
230 } finally {
231 reader.close();
232 }
233 }
234
243 public static int[] readIntData(String file) throws IOException {
244 FileReader reader = new FileReader(file);
245 try {
246 return readIntData(reader);
247 } finally {
248 reader.close();
249 }
250 }
251
264 public static String[] readStringData(Reader input) throws IOException {
265 LineNumberReader inb = new LineNumberReader(input);
266 String[] data = new String[5];
267 int n = 0;
268 String li;
269 while ((li = inb.readLine()) != null) {
270 li = li.trim();
271 if (li.startsWith("#"))
272 continue;
273
274 data[n++] = li;
275 if (n >= data.length) {
276 String[] newData = new String[2 * n];
277 System.arraycopy(data, 0, newData, 0, data.length);
278 data = newData;
279 }
280 }
281 if (data.length != n) {
282 String[] data2 = new String[n];
283 System.arraycopy(data, 0, data2, 0, n);
284 return data2;
285 }
286 return data;
287 }
288
298 public static String[] readStringData(URL url) throws IOException {
299 Reader reader = new InputStreamReader(url.openStream());
300 try {
301 return readStringData(reader);
302 } finally {
303 reader.close();
304 }
305 }
306
315 public static String[] readStringData(File file) throws IOException {
316 FileReader reader = new FileReader(file);
317 try {
318 return readStringData(reader);
319 } finally {
320 reader.close();
321 }
322 }
323
332 public static String[] readStringData(String file) throws IOException {
333 FileReader reader = new FileReader(file);
334 try {
335 return readStringData(reader);
336 } finally {
337 reader.close();
338 }
339 }
340
359 public static double[][] readDoubleData2D(Reader input) throws IOException {
360 LineNumberReader inb = new LineNumberReader(input);
361 double[][] data = new double[5][];
362 int n = 0;
363 String li;
364 String number;
365
366 while ((li = inb.readLine()) != null) {
367 li = li.trim();
368 if (li.startsWith("#"))
369 continue;
370
371 if (li.equals(";")) {
372 data[n++] = new double[0];
373 } else {
374
375 int index = 0;
376 int begin = 0;
377 boolean end = false;
378
379 double[] row = new double[5];
380 int k = 0;
381
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))))
386 ++index;
387
388 if (index >= li.length() || (Character.isWhitespace(li.charAt(index)))) {
389 number = li.substring(begin, index);
390 begin = ++index;
391
392 if (!number.equals("")) {
393 try {
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);
398 row = newRow;
399 }
400 } catch (NumberFormatException nfe) {
401 log.warning("Invalid column " + k + " at line " + inb.getLineNumber() + ": " + number);
402 }
403 }
404 } else {
405 end = true;
406 }
407 }
408
409 if (k > 0) {
410 data[n] = new double[k];
411 System.arraycopy(row, 0, data[n], 0, k);
412 n++;
413 } else {
414 log.warning("Invalid line " + inb.getLineNumber() + ": " + li);
415 }
416 }
417
418 if (n == data.length) {
419 double[][] newData = new double[2 * n][];
420 System.arraycopy(data, 0, newData, 0, n);
421 data = newData;
422 }
423 }
424
425 double[][] data2 = new double[n][];
426 System.arraycopy(data, 0, data2, 0, n);
427 return data2;
428 }
429
440 public static double[][] readDoubleData2D(URL url) throws IOException {
441 Reader reader = new InputStreamReader(url.openStream());
442 try {
443 return readDoubleData2D(reader);
444 } finally {
445 reader.close();
446 }
447 }
448
459 public static double[][] readDoubleData2D(File file) throws IOException {
460 FileReader reader = new FileReader(file);
461 try {
462 return readDoubleData2D(reader);
463 } finally {
464 reader.close();
465 }
466 }
467
477 public static double[][] readDoubleData2D(String file) throws IOException {
478 FileReader reader = new FileReader(file);
479 try {
480 return readDoubleData2D(reader);
481 } finally {
482 reader.close();
483 }
484 }
485
494 public static int[][] readIntData2D(Reader input) throws IOException {
495 LineNumberReader inb = new LineNumberReader(input);
496 int[][] data = new int[5][];
497 int n = 0;
498 String li;
499 String number;
500
501 while ((li = inb.readLine()) != null) {
502 li = li.trim();
503 if (li.startsWith("#"))
504 continue;
505
506 if (li.equals(";")) {
507 data[n++] = new int[0];
508 } else {
509
510 int index = 0;
511 int begin = 0;
512 boolean end = false;
513
514 int[] row = new int[5];
515 int k = 0;
516
517 while (index < li.length() && (!end)) {
518 while (index < li.length()
519 && (li.charAt(index) == '+' || li.charAt(index) == '-' || Character.isDigit(li.charAt(index))))
520 ++index;
521
522 if (index >= li.length() || (Character.isWhitespace(li.charAt(index)))) {
523 number = li.substring(begin, index);
524 begin = ++index;
525
526 if (!number.equals("")) {
527 try {
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);
532 row = newRow;
533 }
534 } catch (NumberFormatException nfe) {
535 log.warning("Invalid column " + k + " at line " + inb.getLineNumber() + ": " + number);
536 }
537 }
538 } else {
539 end = true;
540 }
541 }
542
543 if (k > 0) {
544 data[n] = new int[k];
545 System.arraycopy(row, 0, data[n], 0, k);
546 n++;
547 } else {
548 log.warning("Invalid line " + inb.getLineNumber() + ": " + li);
549 }
550 }
551
552 if (n == data.length) {
553 int[][] newData = new int[2 * n][];
554 System.arraycopy(data, 0, newData, 0, n);
555 data = newData;
556 }
557 }
558
559 int[][] data2 = new int[n][];
560 System.arraycopy(data, 0, data2, 0, n);
561 return data2;
562 }
563
573 public static int[][] readIntData2D(URL url) throws IOException {
574 Reader reader = new InputStreamReader(url.openStream());
575 try {
576 return readIntData2D(reader);
577 } finally {
578 reader.close();
579 }
580 }
581
590 public static int[][] readIntData2D(File file) throws IOException {
591 FileReader reader = new FileReader(file);
592 try {
593 return readIntData2D(reader);
594 } finally {
595 reader.close();
596 }
597 }
598
607 public static int[][] readIntData2D(String file) throws IOException {
608 FileReader reader = new FileReader(file);
609 try {
610 return readIntData2D(reader);
611 } finally {
612 reader.close();
613 }
614 }
615
644 public static String[][] readCSVData(Reader input, char colDelim, char stringDelim) throws IOException {
645 // Using a buffered reader is important here for performance
646 // LineNumberReader is a subclass of BufferedReader
647 LineNumberReader inb = new LineNumberReader(input);
648 StringBuffer sb = new StringBuffer();
649 boolean stringMode = false;
650 String[][] data = new String[5][];
651 int numRows = 0;
652 int numColumns = 0;
653 boolean newRow = false, newColumn = false;
654 int ich = -1;
655 char ch = ' ';
656 boolean readDone = false;
657 while (!readDone) {
658 if (ich == -2)
659 // A character is pending
660 ich = 0;
661 else {
662 ich = inb.read();
663 if (ich == -1)
664 // End of stream: process the last column and row, and exit
665 newRow = newColumn = readDone = true;
666 else
667 ch = (char) ich;
668 }
669 if (ich != -1) {
670 if (stringMode) {
671 if (ch == stringDelim) {
672 // Check if there is a second string delimiter
673 int ichNext = inb.read();
674 if (ichNext >= 0) {
675 char chNext = (char) ichNext;
676 if (chNext == stringDelim)
677 // Append the quoted string delimiter
678 sb.append(stringDelim);
679 else {
680 // Indicate the end of the string, and a new pending character
681 stringMode = false;
682 ich = -2;
683 ch = chNext;
684 }
685 }
686 } else
687 sb.append(ch);
688 } else {
689 if (ch == '\n' || ch == '\r') {
690 int ichNext = inb.read();
691 if (ichNext >= 0) {
692 char chNext = (char) ichNext;
693 if (ch == '\r' && chNext == '\n') {
694 ichNext = inb.read();
695 if (ichNext >= 0) {
696 chNext = (char) ichNext;
697 ich = -2;
698 ch = chNext;
699 newRow = true;
700 }
701 } else {
702 ich = -2;
703 ch = chNext;
704 newRow = true;
705 }
706 }
707 } else if (ch == colDelim)
708 newColumn = true;
709 else if (ch == stringDelim)
710 stringMode = true;
711 else
712 sb.append(ch);
713 }
714 }
715 if (newColumn || newRow) {
716 if (numColumns == 0) {
717 ++numRows;
718 numColumns = 1;
719 } else
720 ++numColumns;
721 if (data.length < numRows) {
722 String[][] newData = new String[2 * data.length][];
723 System.arraycopy(data, 0, newData, 0, data.length);
724 data = newData;
725 }
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;
732 }
733 data[numRows - 1][numColumns - 1] = sb.toString();
734 sb.delete(0, sb.length());
735 newColumn = false;
736 }
737 if (newRow) {
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;
742 }
743 numColumns = 0;
744 newRow = false;
745 }
746 }
747
748 if (stringMode)
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);
753 return data2;
754 }
755 return data;
756 }
757
770 public static String[][] readCSVData(URL url, char colDelim, char stringDelim) throws IOException {
771 Reader reader = new InputStreamReader(url.openStream());
772 try {
773 return readCSVData(reader, colDelim, stringDelim);
774 } finally {
775 reader.close();
776 }
777 }
778
789 public static String[][] readCSVData(File file, char colDelim, char stringDelim) throws IOException {
790 FileReader reader = new FileReader(file);
791 try {
792 return readCSVData(reader, colDelim, stringDelim);
793 } finally {
794 reader.close();
795 }
796 }
797
808 public static String[][] readCSVData(String file, char colDelim, char stringDelim) throws IOException {
809 FileReader reader = new FileReader(file);
810 try {
811 return readCSVData(reader, colDelim, stringDelim);
812 } finally {
813 reader.close();
814 }
815 }
816
817}
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 ...