We have the header but we need to read the data. Our ASCII file is organized as a set of columns and rows. We want to create a function able to read our file but a little bit more general. We want that our function to read any column from an ASCII file by selecting a column and then by reading the data from some rows.
We decide to add to the class Support a function (readData) to do the job. To select the column from inside the row, we split the line in several fields by using a regular expression. This is the code, which is included in the previous class (Support)
We omitted the detail of the function createHeader listed in the previous chapter. Our function creates a Vector with the values. So, the class needs to import java.util.* and since it reads values of a file, the function needs to import java.io.* as well as throwing an IOException.
To test the code, we create a second program (SupportTest2.java)
Since our function reads n values in the column col from the row row, we can use it to create a program that extracts these values. We are impatient to check the results with the file sig.asc created in the previous chapter
It is a nice function but we would need much more checking if we wanted to use it as a general tool to extract data from a file.
The function does not detect when the file does not contain enough values. The function even works (although not properly) with negative indexes. We have to maintain the control when we use the function.
But with all the failures of the function, we have a way to read a set of values from an ASCII file. In the next chapter we want to create a set of binary little-endian short integers to be included in our EDF+ file by using the data read by the function
import java.util.*;
import java.io.*;
class Support{
public Vector readData (String filename,
int col,
int row,
int n) throws IOException{
String line=""; // line read from the file
String[] fields ; // fields in the line
int nLine=0; // number of line
Vector vector = new Vector();
// Opening of the file
FileInputStream fis = new FileInputStream(filename);
InputStreamReader isr = new InputStreamReader(fis);
LineNumberReader lnr = new LineNumberReader(isr);
while(true){
line = lnr.readLine();
// detection of EOF
if (line == null) break;
if ((nLine > row-1) & (nLine < row+n)){
fields = line.split("\\s+");
vector.addElement(fields[col]);
}
nLine++;
}
return vector;
}
public String createHeader(){
// creates a string with spaces
String spaces ="";
....
return result;
}
}
import java.util.*;
import java.io.*;
public class SupportTest2{
public static void main(String[] args) throws IOException{
Support mySupport = new Support();
String filename = args[0];
int col = Integer.parseInt(args[1]);
int row = Integer.parseInt(args[2]);
int n = Integer.parseInt(args[3]);
Vector v = mySupport.readData(filename, col ,row ,n);
for (int i=0; i< v.size(); i++){
System.out.println(v.get(i));
}
}
}
[j@localhost ReadingASCII]$ java SupportTest2 sig.asc 0 0 3
0.0
0.01
0.02
[j@localhost ReadingASCII]$ java SupportTest2 sig.asc 0 497 3
4.97
4.98
4.99
[j@localhost ReadingASCII]$ java SupportTest2 sig.asc 1 0 3
0.0
6.279046656224248
12.533312825768572
[j@localhost ReadingASCII]$ java SupportTest2 sig.asc 1 497 3
-18.74072239995222
-12.535945486835878
-6.281695007562094
[j@localhost ReadingASCII]$ java SupportTest2 sig.asc 0 497 3
4.97
4.98
4.99
[j@localhost ReadingASCII]$ java SupportTest2 sig.asc 0 498 3
4.98
4.99
[j@localhost ReadingASCII]$ java SupportTest2 sig.asc 0 499 3
4.99
[j@localhost ReadingASCII]$ java SupportTest2 sig.asc 0 500 3
[j@localhost ReadingASCII]$ java SupportTest2 sig.asc 0 -3 6
0.0
0.01
0.02
[j@localhost ReadingASCII]$
Next: Preparing the data
Up: Coding a sin wave
Previous: Creating the header
Contents
j
2003-05-28