Question

I have a question, I happen to read a flat arhivo few codes, but the query is how to capture each data organized, I mean that there is always values ​​in some columns, if they are empty I'm going to save as NULL on an object.

Input:

19150526    1   7       1
19400119    2   20  1   1
19580122    2   20  9   1
19600309    1   20  7   1
19570310    2   20  5   1
19401215    1   10  1   1
19650902    2   20  0   1
19510924    1   20  3   1
19351118    2   30      1
19560118    1   20  0   1
19371108    2   7       1
19650315    1   30  6   1
19601217    2   30  4   1

Code Java:

FileInputStream fstream = new FileInputStream("C:\\sppadron.txt");
DataInputStream entrada = new DataInputStream(fstream);
BufferedReader buffer = new BufferedReader(new InputStreamReader(entrada));
String strLinea;
List<Sppadron> listSppadron = new ArrayList<Sppadron>();
while ((strLinea = buffer.readLine()) != null){
        Sppadron spadron= new Sppadron();
        spadron.setSpNac(strLinea.substring(143, 152).trim());   
        spadron.setSpSex(strLinea.substring(152, 154).trim());
        spadron.setSpGri(strLinea.substring(154, 157).trim());
        spadron.setSpSec(strLinea.substring(157, 158).trim());
        spadron.setSpDoc(strLinea.substring(158, strLinea.length()).trim());  
        listSppadron.add(spadron);
}
entrada.close();

Originally I had the idea of doing it this way, but in practice happens is that the position of each string is not fixed as it looks, so I happened to use a split (), but there are different spaces between each data and the latest to use a replaceAll (), but leaves all the data together, is there any way to separate each data regardless of the spacing between each data. Whereas each row penultimate data can come see it empty as the input data file that printable.

Was it helpful?

Solution

try following

strLinea = strLinea.trim().replaceAll("\\s"," ");
String stArr[] = strLinea.split(" ");

then use strArr for further as per your requirement.

if you want it as list you can use Arrays.asList(strArr);

OTHER TIPS

try this way..

replace wherever you see contiguous spaces with a single space

strLinea.replaceAll("\\s+"," ")

Then do your splits

OR

something like

String[] tokensVal = strLinea.split("\\s+");

You're on the right lines using a StringBuffer to read the file line by line. Once you have a line in the buffer, try using the StringTokenizer class to pull out each field. StringTokenizer will by default split on white space and you can iterate through the columns.

consider the below:

public static void main(String[] args) {
    String s = "hello\t\tworld   some spaces \tbetween       here";
    StringTokenizer st = new StringTokenizer(s);
    while(st.hasMoreTokens())
    {
        System.out.println(st.nextToken());
    }
}

This will output:

hello
world
some
spaces
between
here

You could base your solution on this. Maybe have a builder pattern that can return the objects you need given the current line..

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top