TikTik
TikTik

Reputation: 357

Efficient way to read a property file in java

I want to read a property file which has only the values (i.e. Not as a key value pair). My property file will contain only the list of strings (more than 1000 words).

I am just using the IOUtils to read the file as below:

InputStream inputStream = ReadProperty.class.getClassLoader().getResourceAsStream(FILE_NAME);
keywords = IOUtils.toString(inputStream);

What would be the efficient way to maintain the property file.

  1. Maintaining the words as comma separated

EG:

Good,Bad,Better,Best,Could,Would


  1. Maintaining the words in each line

EG:

Good
Bad
Better
Best
Could
Would


I feel the second option is readable but i want to understand that is there any performance issue occurs due to new line character (\n)

Upvotes: 2

Views: 1158

Answers (1)

ParkerHalo
ParkerHalo

Reputation: 4430

If you go with the newline representation you could use this easy way to read the lines:

ArrayList<String> values = new ArrayList<>();
try (BufferedReader br = new BufferedReader(new FileReader("false"))) {
    String value;
    while ((value = br.readLine()) != null) {
        values.add(value);
    }
} catch (FileNotFoundException e) {
    e.printStackTrace();
} catch (IOException e) {
    e.printStackTrace();
}

Regarding performance:

The character ',' and the character '\n' do use the same space on disc, unless you write the lines with a file-writer which is aware of the platform you're working on (it'll write "\r\n" on windows systems). Performance won't be influenced very much (especially if you only have about 1000 entrys)

Upvotes: 1

Related Questions