You don’t need to read each separate snippet based on positions. An easier approach is to simply read the whole line and then use substring
to get each section of the line separately:
String nomeArquivo = "arquivo.txt";
try (BufferedReader br =
new BufferedReader(new InputStreamReader(new FileInputStream(nomeArquivo), "UTF-8"))) {
String linha; // para cada linha
while ((linha = br.readLine()) != null) {
String cpf = linha.substring(0, 11);
String nome = linha.substring(11, 22);
String agencia = linha.substring(22, 26);
String conta = linha.substring(26);
System.out.printf("CPF=%s, nome=%s, agencia=%s, conta=%s\n", cpf, nome, agencia, conta);
}
} catch (IOException e) {
// trate o erro como achar melhor
}
Note that I used the syntax of Try-with-Resources, which ensures that the file will be closed at the end of the execution. It still took a catch
, for the FileInputStream
can launch an exception if the file does not exist, the readLine()
may launch another if any error occurs while reading, etc.
With that I use readLine()
to read the entire file line, and then use substring
to get the snippets that interest me. In the first 3 cases, the initial and final index is used, and the String
always starts at zero, and the final index is not included.
For example, substring(0, 11)
take the index zero to 10 (i.e., the first 11 characters, which in this case correspond to the digits of the CPF). Already in the last case, I put only the initial index (26), and in this case substring
takes everything from this index to the end of the String
.
I also set up the encoding of the file as UTF-8, but you can switch to the encoding that your file is using. The output is:
CPF=41976127992, nome=Jhonatan , agencia=3129, conta=176513
From Java 8 it is also possible to use streams:
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.stream.Stream;
try (Stream<String> stream = Files.lines(Paths.get(nomeArquivo), StandardCharsets.UTF_8)) {
stream.forEach(linha -> {
String cpf = linha.substring(0, 11);
String nome = linha.substring(11, 22);
String agencia = linha.substring(22, 26);
String conta = linha.substring(26);
System.out.printf("CPF=%s, nome=%s, agencia=%s, conta=%s\n", cpf, nome, agencia, conta);
});
} catch (IOException e) {
// trate o erro como achar melhor
}
Based in his comment, if each line has different information, just keep a line counter, and according to the line number, read the respective information. Example:
try (BufferedReader br = ...) {
String linha; // para cada linha
int numeroLinha = 0; // em qual linha estou
while ((linha = br.readLine()) != null) {
numeroLinha++; // atualiza número da linha
switch (numeroLinha % 2) {
case 1: // primeira linha
String cpf = linha.substring(0, 11);
String nome = linha.substring(11, 22);
String agencia = linha.substring(22, 26);
String conta = linha.substring(26);
System.out.printf("CPF=%s, nome=%s, agencia=%s, conta=%s\n", cpf, nome, agencia, conta);
break;
case 0: // segunda linha
// ler endereço
break;
}
}
In this case, I am assuming that the file has one line with CPF/Name/etc and another with address, then another with CPF/Name/etc and another with address and so on. That’s why I used numeroLinha % 2
(the remainder of the division of the line number by 2). When the rest of the division is 1, I am on an odd line, and so is a line that contains the CPF/Name/etc. When the rest is zero, I am on an even line, and so is the address.
Adapt the code to the format of your file. If you have more different line types, just update the values. For example, if it is the first line with name/CPF, the second with address, the third with phone, the fourth with credit card, just do something like:
while ((linha = br.readLine()) != null) {
numeroLinha++; // atualiza número da linha
switch (numeroLinha % 4) {
case 1: // primeira linha
// ler nome/CPF
break;
case 2: // segunda linha
// ler endereço
break;
case 3: // terceira linha
// ler telefone
break;
case 0: // quarta linha
// ler cartão de crédito
break;
}
And so on and so forth. Again, I’m assuming that your file format follows this pattern of always having N lines, each with a type of information, repeating itself without breaking.
If the format is different, please edit question and update information with file format.
Related (or duplicate): Read java txt file data and perform Java operations
– user28595
Just read a file I’ve found here, but perform reading based on positions I did not find, so this case is not a duplicate question, I already cassei on the net this but not found, so I decided to create an account and ask, thanks.
– Jhonatan Prats Bulgari