Versão utilizada no artigo de introdução ao profiler, com as otimizações finais
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.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