Skip to content

Instantly share code, notes, and snippets.

@anthonylgf
Created March 9, 2023 22:36
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save anthonylgf/4b40b8920c678a80b307a75612c7717c to your computer and use it in GitHub Desktop.
Versão utilizada no artigo de introdução ao profiler, com as otimizações finais
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.nio.charset.StandardCharsets;
import java.util.regex.Pattern;
public class Main {
private static final BufferedWriter WRITER = new BufferedWriter(new OutputStreamWriter(System.out));
private static final Pattern PATTERN =
Pattern.compile("^(?=.{1,64}@)[A-Za-z0-9_-]+(\\.[A-Za-z0-9_-]+)*@[^-][A-Za-z0-9-]+(\\.[A-Za-z0-9-]+)*(\\.[A-Za-z]{2,})$");
public static void main(String[] args) throws IOException {
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");
} finally {
WRITER.flush();
WRITER.close();
}
}
private static void printEmailLine(String line, boolean isValidEmail) throws IOException {
if (isValidEmail) {
WRITER.write("O email eh valido: " + line);
WRITER.newLine();
} else {
WRITER.write("O email eh invalido: " + line);
WRITER.newLine();
}
}
private static boolean isValidEmail(String line) {
return PATTERN
.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