Created
March 9, 2023 15:38
-
-
Save anthonylgf/95f9195ea6ce2fb053522f09a2d75ab3 to your computer and use it in GitHub Desktop.
Programa utilizado para o artigo de introdução ao uso de ferramentas de profiling com o Java e o VSCode.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.BufferedReader; | |
import java.io.File; | |
import java.io.FileReader; | |
import java.io.IOException; | |
import java.nio.charset.StandardCharsets; | |
import java.util.regex.Pattern; | |
public class Main { | |
public static void main(String[] args) { | |
if (args.length != 1) { | |
System.err.println("Deve-se passar o caminho do arquivo com os emails como parametro!"); | |
System.exit(1); | |
} | |
try (var fileReader = getFileReader(args[0])) { | |
String line; | |
while ((line = fileReader.readLine()) != null) { | |
boolean isValidEmail = isValidEmail(line); | |
printEmailLine(line, isValidEmail); | |
} | |
} catch (IOException e) { | |
System.err.println("Erro enquanto inicializa o arquivo"); | |
} | |
} | |
private static void printEmailLine(String line, boolean isValidEmail) { | |
if (isValidEmail) { | |
System.out.println("O email eh valido: " + line); | |
} else { | |
System.out.println("O email eh invalido: " + line); | |
} | |
} | |
private static boolean isValidEmail(String line) { | |
return Pattern | |
.compile("^(?=.{1,64}@)[A-Za-z0-9_-]+(\\.[A-Za-z0-9_-]+)*@[^-][A-Za-z0-9-]+(\\.[A-Za-z0-9-]+)*(\\.[A-Za-z]{2,})$") | |
.matcher(line) | |
.matches(); | |
} | |
public static BufferedReader getFileReader(String path) throws IOException { | |
var file = new File(path); | |
if (!file.exists() || !file.canRead() || file.isDirectory()) { | |
System.err.println("Arquivo invalido: " + path); | |
System.exit(1); | |
} | |
return new BufferedReader(new FileReader(file, StandardCharsets.UTF_16)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment