|
I am a novice at any kind of data i/o. What I want to do:
My Java program generates a series of data values (actually these are pairs of values, let’s call them dataA and dataB.), all as Double type. I want to output these pairs of values to a file, and then plot the data in graphs using an external package. The data are floating-point, e.g dataA =10.1, dataB = 1.075 could be one pair.
My program goes through the multiple entries in an ArrayList, each of which contains unique values of dataA and dataB.
None of the values can be negative.
Previously I have been taking a file produced from another source that had similar data pairs in an ASCII, tab-delimited set of columns, which I could process easily, either automatically into a graphing program, or by exporting / cut-pasting into Excel, etc. If I could reproduce this file format (ASCII, tab-delimited) I would be very happy.
What I’ve attempted so far:
--------------------------------
try{
DataOutputStream output = new DataOutputStream (new FileOutputStream ( "myFile.dat"));
for(int i = 0; i < myArrayList.size(); i++ ) {
simOutput.writeDouble( dataA (i) );
//simOutput.writeDouble( dataB (i) );
}
simOutput.close();
}
catch (IOException e) { }
-----------------------------------
This generates a file that has no formatting (or at least I don’t understand its formatting), and I don’t know how to interpret the gobbledygook in the file. Maybe it contains what I want, but I don’t know.
Any help would be appreciated.
------------------------------------------------------------------------------
|
|
|
DataOutputStream writes data in binary. Using it requires java's DataInputStream to read the data back in; maybe not a good choice for use with other apps. For future reference the tutorial has an example of using/formatting with these streams: How to Use DataInputStream and DataOutputStream.
You could probably use the convenience FileWriter class to write out your data.
Here's a more general–case writer/reader demo that may work for you. It is a little more hardy than the FileWriter and avoids the binary–readability problem.
import java.io.*;
import java.text.NumberFormat;
import java.util.Random;
public class IOTest
{
static NumberFormat nf;
public static void main(String[] args)
{
nf = NumberFormat.getInstance();
nf.setMaximumFractionDigits(2);
// make up some data to write to file
int dataPoints = 16;
double[] data = new double[2*dataPoints];
Random seed = new Random();
for(int j = 0; j < data.length; j++)
data[j] = seed.nextDouble() * 26.0; // [0.0 < d < 26.0]
print(data);
// write data to file
writeToFile(data);
// read it back in
double[] extracted = readData();
print(extracted);
}
private static void writeToFile(double[] d)
{
NumberFormat nf = NumberFormat.getInstance();
nf.setMinimumFractionDigits(16); // pad data
//nf.setMaximumFractionDigits(8); // truncate
// in case other apps have trouble with recognizing
// the java/unix line separator you could try this
String newline = System.getProperty("line.separator");
try
{
BufferedWriter bw = new BufferedWriter(
new OutputStreamWriter(
new FileOutputStream("/forum/ioTest.txt")));
for(int j = 0; j < d.length; j+=2)
{
String s = String.valueOf(d[j]) + "\t" + d[j+1] + "\n";
// if you need the columns to be aligned for easy
// reading you could try some kind of formatting
//nf.format(d[j]) + "\t" + nf.format(d[j+1]) + "\n";
bw.write(s, 0, s.length());
}
bw.close();
}
catch(IOException ioe)
{
System.out.println("write error: " + ioe.getMessage());
}
}
private static double[] readData()
{
double[] data = new double[0];
int count = 0;
try
{
BufferedReader br = new BufferedReader(
new InputStreamReader(
new FileInputStream("/forum/ioTest.txt")));
br.mark(2048);
String line;
// count lines in data file
while((line = br.readLine()) != null)
count++;
// instantiate data array to correct size
data = new double[2*count];
// read file
br.reset();
count = 0;
while((line = br.readLine()) != null)
{
String[] s = line.split("\t");
data[count++] = Double.parseDouble(s[0]);
data[count++] = Double.parseDouble(s[1]);
}
br.close();
}
catch(IOException ioe)
{
System.out.println("read error: " + ioe.getMessage());
}
return data;
}
private static void print(double[] d)
{
for(int j = 0; j < d.length; j++)
{
System.out.print(nf.format(d[j]));
if(j < d.length-1)
System.out.print(", ");
}
System.out.println();
}
private static void findLimits()
{
Random r = new Random();
double max = Double.MIN_VALUE;
double min = Double.MAX_VALUE;
double seed = 100.0;
for(int j = 0; j < 1000000; j++)
{
double next = r.nextDouble() * seed;
if(next < min)
min = next;
if(next > max)
max = next;
}
System.out.printf("min %f %s max %f%n", min, "\t", max);
}
}
|
|
|
|
|
crwood, thanks for your excellent help, which is very appreciated.
|
|
|
|
|
|
|
|