Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7953053
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T03:02:57+00:00 2026-06-04T03:02:57+00:00

Text File(First three lines are simple to read, next three lines starts with p)

  • 0

Text File(First three lines are simple to read, next three lines starts with p)

ThreadSize:2
ExistingRange:1-1000
NewRange:5000-10000
p:55 - AutoRefreshStoreCategories  Data:Previous    UserLogged:true    Attribute:1    Attribute:16      Attribute:2060  
p:25 - CrossPromoEditItemRule      Data:New         UserLogged:false     Attribute:1      Attribute:10107   Attribute:10108
p:20 - CrossPromoManageRules       Data:Previous    UserLogged:true      Attribute:1      Attribute:10107   Attribute:10108

Below is the code I wrote to parse the above file and after parsing it I am setting the corresponding values using its Setter. I just wanted to know whether I can improve this code more in terms of parsing and other things also by using other way like using RegEx? My main goal is to parse it and set the corresponding values. Any feedback or suggestions will be highly appreciated.

private List<Command> commands;
private static int noOfThreads = 3;
private static int startRange = 1;
private static int endRange = 1000;
private static int newStartRange = 5000;
private static int newEndRange = 10000;
private BufferedReader br = null;
private String sCurrentLine = null;
private int distributeRange = 100;
private List<String> values = new ArrayList<String>();
private String commandName;
private static String data;
private static boolean userLogged;
private static List<Integer> attributeID =  new ArrayList<Integer>();

    try {
        // Initialize the system
        commands = new LinkedList<Command>();
        br = new BufferedReader(new FileReader("S:\\Testing\\Test1.txt"));

        while ((sCurrentLine = br.readLine()) != null) {
            if(sCurrentLine.contains("ThreadSize")) {
                noOfThreads = Integer.parseInt(sCurrentLine.split(":")[1]);
            } else if(sCurrentLine.contains("ExistingRange")) {
                startRange = Integer.parseInt(sCurrentLine.split(":")[1].split("-")[0]);
                endRange = Integer.parseInt(sCurrentLine.split(":")[1].split("-")[1]);
            } else if(sCurrentLine.contains("NewRange")) {
                newStartRange = Integer.parseInt(sCurrentLine.split(":")[1].split("-")[0]);
                newEndRange = Integer.parseInt(sCurrentLine.split(":")[1].split("-")[1]);
            } else {
                allLines.add(Arrays.asList(sCurrentLine.split("\\s+")));
                String key = sCurrentLine.split("-")[0].split(":")[1].trim();
                String value = sCurrentLine.split("-")[1].trim();
                values = Arrays.asList(sCurrentLine.split("-")[1].trim().split("\\s+"));
                for(String s : values) {
                    if(s.contains("Data:")) {
                        data = s.split(":")[1];
                    } else if(s.contains("UserLogged:")) {
                        userLogged = Boolean.parseBoolean(s.split(":")[1]);
                    } else if(s.contains("Attribute:")) {
                        attributeID.add(Integer.parseInt(s.split(":")[1]));
                    } else {
                        commandName = s;
                    }
                }

                Command command = new Command();
                command.setName(commandName); 
                command.setExecutionPercentage(Double.parseDouble(key));
                command.setAttributeID(attributeID);
                command.setDataCriteria(data);
                command.setUserLogging(userLogged);
                commands.add(command);

            }
        }
    } catch(Exception e) {
        System.out.println(e);
    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T03:02:59+00:00Added an answer on June 4, 2026 at 3:02 am

    To answer a comment:

    p:55 - AutoRefreshStoreCategories  Data:Previous    UserLogged:true    Attribute:1    Attribute:16      Attribute:2060  
    

    to parse above with regex (and 3 times Attribute:):

    String parseLine = "p:55 - AutoRefreshStoreCategories  Data:Previous    UserLogged:true    Attribute:1    Attribute:16      Attribute:2060";
        Matcher m = Pattern
                .compile(
                        "p:(\\d+)\\s-\\s(.*?)\\s+Data:(.*?)\\s+UserLogged:(.*?)\\s+Attribute:(\\d+)\\s+Attribute:(\\d+)\\s+Attribute:(\\d+)")
                .matcher(parseLine);
        if(m.find()) {
            int p = Integer.parseInt(m.group(1));
            String method = m.group(2);
            String data = m.group(3);
            boolean userLogged = Boolean.valueOf(m.group(4));
            int at1 = Integer.parseInt(m.group(5));
            int at2 = Integer.parseInt(m.group(6));
            int at3 = Integer.parseInt(m.group(7));
            System.out.println(p + " " + method + " " + data + " " + userLogged + " " + at1 + " " + at2 + " "
                    + at3);
        }
    

    EDIT looking at your comment you still can use regex:

    String parseLine = "p:55 - AutoRefreshStoreCategories  Data:Previous    UserLogged:true    "
                + "Attribute:1    Attribute:16      Attribute:2060";
        Matcher m = Pattern.compile("p:(\\d+)\\s-\\s(.*?)\\s+Data:(.*?)\\s+UserLogged:(.*?)").matcher(
                parseLine);
        if(m.find()) {
            for(int i = 0; i < m.groupCount(); ++i) {
                System.out.println(m.group(i + 1));
            }
        }
    
        Matcher m2 = Pattern.compile("Attribute:(\\d+)").matcher(parseLine);
        while(m2.find()) {
            System.out.println("Attribute matched: " + m2.group(1));
        }
    

    But that depends if thre is no Attribute: names before “real” attributes (for example as method name – after p)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have text file with something like first line line nr 2 line three
I have a text file with few first few lines not required and then
I have the first few lines of a text file fx.txt with the following
Below is a fairly simple C program to open a text file (input.txt), read
Currently I have a a text file with data at the first row is
I need to insert string to text file, for example, before first line from
I am trying to write text to my txt file. After the first write
My text file contains 2 lines: <IMG SRC=/icons/folder.gif ALT=[DIR]> <A HREF=yahoo.com.jp/>yahoo.com.jp/</A> </PRE><HR> In my
I'm trying to implement a simple method to read new lines from a log
I am trying to read in a simple flat file, it has 738,627 records

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.