In my Selenium+Java automation framework, I need to open a .txt file or any other extension file and then see if the text is in a particular format. Now, I need to see if all lines are in this format and also, say in line 2, from substring (7,10) is 2000. Now, is there any method in FileUtils API?

I can open the file and assert that it exists. I am not able to validate File content. I need to do this as a part of my Automation project. this is indeed the purpose of my project to validate all file format contents.


0000000TEst  123  234   TEST22 refreence 0002343
00000002000     2343   23343  TEST22000000000000000000
000000003534   3434    34343  3434 343 343 TEst 0000000 000 000              001

Update in response to answers:

Thanks! this is very helpful, i will use this method to verify all the file formats with regex.

Also, once i validate the format, then i will validate some substrings. Now, i have the expected result of Substring stored in String line2Value = "2000";


// syso("This is correct....."

Now, I have such kind of a file in which some section of records, some regex repeats n number of times.
:89:?This is test line / EXTRA TEXT LINE
My file will have hundreds of these sections, that all start from {1:... and end with -89 }..

how i can check not just 1 section, but all of these groups are in the same format :/

  • 1
    "see if the text is in a particular format" Which format are you looking for exactly? For people to build a regex to help you, they need to know what is the format. Commented Feb 11, 2023 at 7:04

2 Answers 2


see if the text is in a particular format

For this, you can use regular expressions. Here is a tutorial:


(I will try to add the exact regex when we know what format are we looking for)

also, say in line 2, from substring (7,10) is 2000

For this, you can use Substring:

assertThat(myString.substring(7, 10)).equals("2000")
  • Farias, by file format i mean that some files have multiple records with empty spaces between strings... like the one shown above.... I need to make sure that file format is not changed, i.e There are lines starting a specific string, say: "00000TEST" and there are whitespaces between strings in one line. Also, should i store substrings from my line and then compare with the actual value in the original file?
    – test0023
    Commented Feb 11, 2023 at 13:00

I AM Milin Patel, Thanks for the answer. The issue is that all lines might not be having same format. Say in Line 2 there are 30 empty spaces, Line 3 and 4 have just 3 whitespaces, Line 5,6 start from any string.

then, in that case, how can we have just one regex matching against all lines. Also, I just want to see if Line[2] has substring(7,10) as 2000, Not for all lines.

Could you please give some more examples. I know nothing about regular expressions, I will start reading a tutorial.

  • I've updated my answer with enhancements. Hope that helps you. Commented Feb 15, 2023 at 6:08
  • This isn't an answer but more comments about a previous answer and should be deleted or added to that previous answer.
    – Lee Jensen
    Commented Feb 15, 2023 at 16:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.