4
I have the following situation: I have a txt file of defined positions (type CSV, CNAB, etc) to process and extract values. For the purposes of understanding, follow the code I made and it’s working perfectly:
public class Extract {
final int[] CPF = new int[]{0, 10};
final int[] NOME = new int[]{15, 45};
public static void main(String args[]) {
FileInputStream fis = new FileInputStream("C:\\cnab.txt");
String line;
while ((line = fis.readLine()) != null) {
System.out.println(getValue(line, CPF));
System.out.println(getValue(line, NOME));
}
}
private String getValue(String line, int[] slice) {
return line.substring(slice[0], slice[1]);
}
}
So far ok, but I’d like something more readable, elegant, such as:
System.out.println(line.getValue(CPF));
But we all know that String is final. Does anyone have any suggestions?
Change the language to C# and be happy :) Otherwise, it is not possible, but it also changes very little. I am glad that
String
is final, otherwise people would do atrocities as they do with most classes that are not.– Maniero
Even if you come to use C#, use Extension Methods here would be a great gambiarra. Imagine a String, one of the most cohesive objects of the language, with a method string.getValue(int[])! Defining a new class is a better solution.
– Caffé