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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T12:48:07+00:00 2026-05-11T12:48:07+00:00

I have a comma delimited configuration file. The empty lines are ignored and there

  • 0

I have a comma delimited configuration file. The empty lines are ignored and there need to be errors on invalid lines:

foo,bar

foo2, bar3

I want to read this file into a HashMap where the key (foo) is mapped with a value (bar).

What is the best way to do this?

  • 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. 2026-05-11T12:48:07+00:00Added an answer on May 11, 2026 at 12:48 pm

    If you can use x = y instead of x, y then you can use the Properties class.

    If you do need to have x, y then look at the java.util.Scanner you can set the delimiter to use as a separator (the javadoc shows examples of doing that).

    import java.io.File; import java.io.FileNotFoundException; import java.util.Scanner;  class Main {     public static void main(final String[] argv)     {         final File file;          file = new File(argv[0]);          try         {             final Scanner scanner;              scanner = new Scanner(file);              while(scanner.hasNextLine())             {                 if(scanner.hasNext('.*,'))                 {                     String key;                     final String value;                      key = scanner.next('.*,').trim();                      if(!(scanner.hasNext()))                     {                         // pick a better exception to throw                         throw new Error('Missing value for key: ' + key);                     }                      key   = key.substring(0, key.length() - 1);                     value = scanner.next();                      System.out.println('key = ' + key + ' value = ' + value);                 }             }         }         catch(final FileNotFoundException ex)         {             ex.printStackTrace();         }     } } 

    and the Properties version (way simpler for the parsing, as there is none)

    import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; import java.io.Reader; import java.util.Properties;  class Main {     public static void main(final String[] argv)     {         Reader reader;          reader = null;          try         {             final Properties properties;              reader = new BufferedReader(                             new FileReader(argv[0]));             properties = new Properties();             properties.load(reader);             System.out.println(properties);         }         catch(final IOException ex)         {             ex.printStackTrace();         }         finally         {             if(reader != null)             {                 try                 {                     reader.close();                 }                 catch(final IOException ex)                 {                     ex.printStackTrace();                 }             }         }     } } 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a comma-delimited list of directories: foo,bar,baz,qux I want to convert it to
I have a comma delimited list of record id's: string = 2207,117,90,2168,120,118,113,112,17 I need
I need to split comma delimited string into a second columns I have the
I have a comma delimited list of numbers. I need to match everything up
I have a csv file that is comma delimited. I have no problem with
Possible Duplicate: Reading csv file I have a comma delimited file: Some Text, More
I have a problem reading a comma-delimited TXT file. This is what I am
I have a string comma delimited. I need to select with t-sql just first
I have a comma delimited CSV file being loaded into a String value generated
I have a string in the following format in a comma delimited file: someText,

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.