0

I am trying to validate and restrict email address format when getting output from a text file, using java. I am opening the file this way at the moment.

inputStream = new BufferedReader(new FileReader(filename));
while ((fileLine = inputStream.readLine()) != null) {
                    System.out.println(fileLine);}}

I tried "if (filename.matches("^[A-Za-z0-9+_.-]+@(.+)$")) " But is am pretty sure that is just checking the file's actual name.

If someone has a moment, could you please guide me on how to do this properly?

Thank you all!

  • 1
    Does this answer your question? [Mail address validation JAVA](https://stackoverflow.com/questions/23598387/mail-address-validation-java) Or does this? https://stackoverflow.com/questions/624581/what-is-the-best-java-email-address-validation-method – Abra Jun 26 '20 at 05:05

1 Answers1

1

You have an error in this line -> if (filename.matches("^[A-Za-z0-9+_.-]+@(.+)$"))
you are checking the filename instead of fileLine

  • Thank you for the reply. This is my code now. But it seems to still allow other characters inputStream = new BufferedReader(new FileReader(filename)); String fileLine = inputStream.readLine(); if (fileLine.matches("^[A-Za-z0-9+_.-]+@(.+)$")) – CreativeXtent Jun 26 '20 at 04:35