This subpackage provides tools for exporting data to text and binary files, as well as for importing data from files.
More...
This subpackage provides tools for exporting data to text and binary files, as well as for importing data from files.
Each of the write() methods takes a field label as their first argument. This label can always be set to null, in which case an anonymous field will be written. The write() methods that take one-dimensional array argument can also take an additional integer argument, for convenience, to specify the number of elements to write in the array.
For a quick start, consult the following examples and the documentation for umontreal.ssj.util.io.DataWriter and umontreal.ssj.util.io.DataReader, as well as the constructors of implementing classes ( umontreal.ssj.util.io.TextDataWriter, umontreal.ssj.util.io.BinaryDataWriter and umontreal.ssj.util.io.BinaryDataReader ).
Example of how to write data to a file:
public static void writerExample() throws IOException {
String filename = "test.dat";
out.
write(
"zerotxt",
"ZERO");
out.
write(
"n",
new int[]{1,2,3,4,5});
out.
write(
"pi", Math.PI);
out.
write(
"str",
new String[]{
"text1",
"text2"});
out.
write(
"real",
new double[]{2.5, 3.7, 8.9});
out.
write(
"real2",
new float[]{2.5f, 3.7f, 8.9f});
}
void write(String label, String s)
Writes an atomic string field.
void close()
Flushes any pending data and closes the output stream.
Example of how to read data from a file — specific fields:
public static void readerExample1() throws IOException {
String filename = "test.dat";
System.out.print("[n] (int[]) ");
for (int i = 0; i < n.length; i++)
System.out.print(" " + n[i]);
System.out.println();
}
double asDouble()
Returns the value as double or 0 if it is not of type double See isDouble.
void close()
Closes the input stream.
int[] readIntArray(String label)
Reads the first field labeled as label and returns its value as a one-dimensional array of int’s.
DataField readField(String label)
Reads the first field labeled as label.
Example of how to read data from a file — list all fields:
public static void readerExample2() throws IOException {
String filename = "test.dat";
Set<String> allKeys = new TreeSet<String>(fields.keySet());
for (String key : allKeys) {
System.out.print("[" + key + "]");
System.out.print(
" (String) " + d.
asString());
System.out.print(
" (int) " + d.
asInt());
System.out.print(
" (float) " + d.
asFloat());
System.out.print(
" (double) " + d.
asDouble());
System.out.print(" (String[]) ");
for (int i = 0; i < a.length; i++)
System.out.print(" " + a[i]);
}
System.out.print(" (int[]) ");
for (int i = 0; i < a.length; i++)
System.out.print(" " + a[i]);
}
System.out.print(" (float[]) ");
for (int i = 0; i < a.length; i++)
System.out.print(" " + a[i]);
}
System.out.print(" (double[]) ");
for (int i = 0; i < a.length; i++)
System.out.print(" " + a[i]);
}
System.out.println();
}
}
This class represents a data field from a file read by an instance of a class implementing DataReader...
int[] asIntArray()
Returns the value as one-dimensional int array or null if it is not of type int[].
String asString()
Returns the value as String, or null if it is not of type String.
boolean isFloat()
Returns true if the field value is an atomic float.
double[] asDoubleArray()
Returns the value as one-dimensional double array or null if it is not of type double[].
float asFloat()
Returns the value as float or 0 if it is not of type float See isFloat.
String[] asStringArray()
Returns the value as one-dimensional String array or null if it is not of type String[].
boolean isString()
Returns true if the field value is an atomic String.
boolean isDouble()
Returns true if the field value is an atomic double.
int asInt()
Returns the value as int or 0 if it is not of type int See isInt.
float[] asFloatArray()
Returns the value as one-dimensional float array or null if it is not of type float[].
boolean isInt()
Returns true if the field value is an atomic int.
Map< String, DataField > readAllFields()
Reads all fields in the file and returns a hashmap indexed by field labels.