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 6189261
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T02:23:28+00:00 2026-05-24T02:23:28+00:00

I want to parse a file based on a record layout provided in another

  • 0

I want to parse a file based on a record layout provided in another file.

Basically there will be a definition file, which is a comma delimited list of fields and their respective lengths. There will be many of these, a new one will be loaded each time I run the program.

firstName,text,20
middleInitial,text,1
lastName,text,20
salary,number,10

Then I will display a blank table with the supplied column headings, and an option to add data by clicking a button or whatever – I haven’t decided yet.

I also want to have an option to both load data from a file, or save data to a file, with the file matching the format described in the definition file.

For example, a file to load (or one produced by the save function) for the above definition file might look like this.

Adam                DSmith               50000
Brent               GWilliams            45000
Harry               TThompson            47500

What kind of patterns could be useful here, and can anyone give me pointers of a rough guide on how to structure the way data is internally stored and modeled.

I would like to think I can find my way around the java documentation alright, but if anyone can point me at somewhere to start looking, it would be greatly appreciated!

Thanks

  • 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-05-24T02:23:29+00:00Added an answer on May 24, 2026 at 2:23 am

    So it sounds like to me that you have a howToParse file and infoToParse file with the directions of how to parse information and the information to parse in these files respectively.

    First, I would read in the howToParse file and create some sort of dynamic Parser object. It looks like each line in this file is a different ParsingStep object. Then you just need to read the line which will be stored as a String object and just split the ParsingStep into its 3 parts: field name, type of data, length of data.

    // Create new parser to hold parsing steps.
    Parser dynamicParser = new Parser();
    
    // Create new scanner to read through parse file.
    Scanner parseFileScanner = new Scanner(howToParseFileName);
    
    // *** Add exception handling as necessary *** this is just an example    
    
    // Read till end of file.
    while (parseFileScanner.hasNext()) {
        String line = parseFileScanner.nextLine(); // Get next line in file.
    
        String[] lineSplit = line.split(","); // Split on comma
        String fieldName   = lineSplit[0];
        String dataType    = lineSplit[1];
        String dataLength  = lineSplit[2];  // Convert to Integer with Integer.parseInt();
    
        ParsingStep step = new ParsingStep(fieldName, dataType, dataLength);
        dynamicParser.addStep(step);
    }
    
    parseFileScanner.close();
    

    Then you would have how to parse a line, then you just need to parse through the other file and store the information from that file, probably in an array.

    // Open infoToParse file and start reading.
    Scanner infoScanner = new Scanner(infoToParseFileName);
    
    // Add exception handling.
    while (infoScanner.hasNext()) {
        String line = infoScanner.nextLine();
    
        // Parse line and return a Person object or maybe just a Map of field names to values
        Map<String,String> personMap = dynamicParser.parse(line);
    } 
    
    infoScanner.close();
    

    Then the only other code is just making sure the parser is parsing in the correct order.

    public class Parser {
        private ArrayList<ParsingStep> steps;
    
        public Parser() {
            steps = new ArrayList<ParsingStep>();
        }
    
        public void addStep(ParsingStep step) {
            steps.add(step);
        }
    
        public Map<String,String> parse(String line) {
            String remainingLine = line;
    
            for (ParsingStep step : steps) {
                remainingLine = step.parse(remainingLine);
            }
    
            return map; // Somehow convert to map.
        }
    }
    

    Personally, I would add some error checking in the parse steps just in case the infoToParse file is not in the proper format.

    Hope this helps.

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

Sidebar

Related Questions

I want to parse a config file sorta thing, like so: [KEY:Value] [SUBKEY:SubValue] Now
I want to parse the html file, pdf file, csv file and text file.Now
I want to be able to parse file paths like this one: /var/www/index.(htm|html|php|shtml) into
I want to parse an Apache access.log file with a python program in a
I want to parse out each modified file that is reported during FCIV's verification
I want to parse a large XML file and I have two options: Perl
I want to parse the tabular information from a .pdf file,and want to display
Scenario I want to parse a large CSV file and inserts data into the
A file name will be passed in from standard in. I want to open
I have a class that parses in data from a comma delimited text file.

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.