Iohannes 31 de mai. de 2020
Exemplo:
1 - Classe Usuario (para encapasular os dados)
public class Usuario {
private int id ;
private String nome ;
private String dataNascimento ;
public void setId ( int id ){
this . id = id ;
}
public int getId (){
return id ;
}
public void setNome ( String nome ){
this . nome = nome ;
}
public String getNome (){
return nome ;
}
public void setDataNascimento ( String dataNascimento ){
this . dataNascimento = dataNascimento ;
}
public String getDataNascimento (){
return dataNascimento ;
}
}
2 - Classe ManipulacaoArquivo
import java.util.Map ;
import java.util.List ;
import java.util.HashMap ;
import java.util.ArrayList ;
import java.io.FileReader ;
import java.io.BufferedReader ;
import java.io.IOException ;
import java.time.LocalDate ;
import java.time.format.DateTimeFormatter ;
import java.time.format.DateTimeParseException ;
public class ManipulacaoArquivo {
private static final String path = "C:/Users/JFSJUNIOR/Desktop/Dados.txt" ;
private Map < Integer , Usuario > mapeamento = new HashMap <> ();
private DateTimeFormatter formato = DateTimeFormatter . ofPattern ( "dd/MM/uuuu" );
public static void main ( String [] args ){
ManipulacaoArquivo manipulacao = new ManipulacaoArquivo ();
List < Integer > listagem ;
manipulacao . lerDados ();
manipulacao . exibirDados ();
listagem = manipulacao . verificarDados ();
for ( int i = 0 ; i < listagem . size (); i ++ ){
System . out . println ( "Data errada na linha: " + listagem . get ( i ));
}
}
private void lerDados (){
String [] valores ;
try {
int numeroLinha = 0 ;
int contador = 0 ;
String linhaLida = null ;
FileReader leitorDeArquivo = new FileReader ( path );
BufferedReader bufferDeLeitura = new BufferedReader ( leitorDeArquivo );
do {
linhaLida = bufferDeLeitura . readLine ();
if ( linhaLida != null ){
Usuario usuario = new Usuario ();
numeroLinha ++ ;
valores = linhaLida . split ( ";" );
usuario . setId ( Integer . valueOf ( valores [ 0 ] ). intValue ());
usuario . setNome ( valores [ 1 ] );
usuario . setDataNascimento ( String . valueOf ( valores [ 2 ] ));
mapeamento . put ( numeroLinha , usuario );
}
} while ( linhaLida != null );
} catch ( IOException ioex ){
System . out . println ( "ERRO: " + ioex . getMessage ());
}
}
private void exibirDados (){
System . out . println ( "Usuários" );
for ( Integer i = 1 ; i < mapeamento . size () + 1 ; i ++ ){
System . out . println ( "ID: " + mapeamento . get ( i ). getId ()
+ "\nNome: " + mapeamento . get ( i ). getNome ()
+ "\nData de Nascimento: " + mapeamento . get ( i ). getDataNascimento ()
+ "\n" );
}
}
private List < Integer > verificarDados (){
List < Integer > datasInvalidas = new ArrayList <> ();
LocalDate dataLocal = null ;
LocalDate dataRetorno = null ;
for ( Integer i = 1 ; i < mapeamento . size () + 1 ; i ++ ){
try {
dataRetorno = dataLocal . parse ( mapeamento . get ( i )
. getDataNascimento (), formato );
} catch ( DateTimeParseException dtpe ){
//System.out.println("\tData errada" + dtpe.getMessage());
datasInvalidas . add ( i );
}
}
return datasInvalidas ;
}
}
3 - Arquivo:
Rodando…