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 :/

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. Feb 11 at 7:04

3 Answers 3

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")
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?
    – test0023
    Feb 11 at 13:00
0

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.

2
  • I've updated my answer with enhancements. Hope that helps you. Feb 15 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
    Feb 15 at 16:58
0

You can use Java's built-in file I/O classes to read the contents of the file and then parse the contents to check if it is in the expected format.

Here's an example code snippet:

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.util.List;

public class ValidateFileFormat {
    public static void main(String[] args) {
        File file = new File("path/to/file.txt");
        try {
            List<String> lines = Files.readAllLines(file.toPath());
            for (String line : lines) {
                if (!line.matches("^[0-9]{7}[a-zA-Z ]{7}[0-9]{3}[ ]+[0-9]{4}[ ]+[0-9]{5}[ ]+[a-zA-Z0-9 ]{7}[0-9]{7}$")) {
                    throw new Exception("Invalid file format: " + line);
                }
                if (line.substring(7, 10).equals("2000")) {
                    // Do something with the line that contains "2000"
                }
            }
            System.out.println("File format is valid.");
        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

This code reads the contents of the file line by line, and then checks each line against a regular expression to see if it matches the expected format. If a line does not match the format, it throws an exception with an error message indicating which line is invalid. It also checks if the substring from index 7 to 10 of the line is equal to "2000" and performs some action accordingly. You can integrate this code into your Selenium+Java automation framework to validate the file format.


Edit as per your concerns on the above:

You can modify the regular expression to match any variation of the expected format. For example, if the number of whitespace characters between each field can vary, you can use the \s character class in the regular expression to match any whitespace character, like this:

^[0-9]{7}[a-zA-Z\s]{7}[0-9]{3}\s+[0-9]{4}\s+[0-9]{5}\s+[a-zA-Z0-9\s]{7}[0-9]{7}$

This regular expression will match any line that has 7 digits, followed by 7 alphabetic or whitespace characters, followed by 3 digits, followed by one or more whitespace characters, followed by 4 digits, one or more whitespace characters, followed by 5 digits, one or more whitespace characters, followed by 7 alphanumeric or whitespace characters, followed by 7 digits.

To check only Line 2 for the substring (7,10) as 2000, you can simply check that substring only for Line 2, like this:

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.util.List;

public class ValidateFileFormat {
    public static void main(String[] args) {
        File file = new File("path/to/file.txt");
        try {
            List<String> lines = Files.readAllLines(file.toPath());
            for (int i = 0; i < lines.size(); i++) {
                String line = lines.get(i);
                if (i == 1) { // Check only Line 2
                    if (!line.substring(7, 10).equals("2000")) {
                        throw new Exception("Invalid substring in Line 2: " + line);
                    }
                }
                else {
                    if (!line.matches("^[0-9]{7}[a-zA-Z\\s]{7}[0-9]{3}\\s+[0-9]{4}\\s+[0-9]{5}\\s+[a-zA-Z0-9\\s]{7}[0-9]{7}$")) {
                        throw new Exception("Invalid file format in Line " + (i + 1) + ": " + line);
                    }
                }
            }
            System.out.println("File format is valid.");
        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

This code checks the substring (7,10) only for Line 2, and uses the modified regular expression to check the format of all other lines. If a line does not match the format, it throws an exception with an error message indicating which line is invalid.

Also note, if the format of every file is and each line in the file is going to be different, you will need to change your checking script and regular expression accordingly. It is also possible that you might also need to write more than one function to check different formats and file types.

Your Answer

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

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