2

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.

Like:

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";

if(line1.substring(7,10).equals(line2Value)){
// 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.
{1:TES001}{2:TEST002}{3:
:03:192sscE8wre
:04:TESTLIne
:05C:23434/23423
:06:/
:07:343434,434343//3434343434
: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 :/

Kate Paulk
31.5k8 gold badges56 silver badges109 bronze badges
asked Feb 11, 2023 at 5:34
1
  • 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

1 Answer 1

0

see if the text is in a particular format

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

https://www.baeldung.com/regular-expressions-java

(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")
answered Feb 11, 2023 at 7:07
1
  • 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? Commented Feb 11, 2023 at 13:00

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.