Olá Pessoal boa tarde, adoraria saber se alguém pode me ajudar com esse código abaixo, na minha aplicação o usuário com frequência irá subir alguns arquivos e eles tem duas informações, numero e nome separados por -> ;
porem no arquivo eu simulo alguns possíveis problemas como números sem nome e meu código não se comporta bem contra esses possíveis problemas.
Outro detalhe é que meu SOUT esta duplicando e eu não sei porque.
Se alguém puder me ajudar, agradeço.
public void adicionaArquivo(MultipartFile file) throws IOException {
InputStream inputStream = file.getInputStream();
List<ContatoDTO> contato = new ArrayList<>();
//cria um scanner para ler o arquivo
Scanner leitor = new Scanner(inputStream);
//variavel que armazenara as linhas do arquivo
String linhasDoArquivo = new String();
//percorre todo o arquivo
while (leitor.hasNext()) {
try {
//recebe cada linha do arquivo
linhasDoArquivo = leitor.nextLine();
//separa os campos entre as virgulas de cada linha
String[] valoresEntreVirgulas = linhasDoArquivo.split(";");
for (int i = 0; i < valoresEntreVirgulas.length; i++) {
ContatoDTO c = new ContatoDTO();
c.setTelefone(valoresEntreVirgulas[0]);
c.setNome(valoresEntreVirgulas[1]);
contato.add(c);
}
} catch (Exception e) {
e.printStackTrace();
}
}
for (int i = 0; i < contato.size(); i++) {
System.out.println("telefone : " + contato.get(i).getTelefone());
System.out.println("Nome : " + contato.get(i).getNome());
}
System.out.println("qt de linhas : " + contato.size());
}
meu arquivo
[telefone removido];nome1
[telefone removido];nome2
[telefone removido];nome3
[telefone removido];nome4
[telefone removido];nome5
[telefone removido];nome6
[telefone removido];nome7
[telefone removido];nome8
[telefone removido];nome9
[telefone removido];nome10
[telefone removido];nome11
[telefone removido]
[telefone removido];
[telefone removido];nome14
[telefone removido];nome15
meu log
