I have made a textual minesweeper game (here you can find the program). I want to initialize some data by the help of a file. The file should be readable for humans and easy to modify with a simple text editor. The file should look for example like this and it should allow comments:

# columns and rows
# describes how the field is selected
# 1 - <o x x> (for example e 5 7 which means explore field with coordinates 5 7)
# 2 - easy entering format
format: 1 
# possibility that a field is bombed (1 / possibility)
possibility: 8

In which part of the program and in which manner should I read such a file? What is the most efficient way to read such a file? Thank you for all answers!

  • Could someone tell me why my question has got a downvote? – Henry Weinert Feb 28 '18 at 13:17
  • 1
    this question is very broad, and seems like a homework question. – TheCatWhisperer Feb 28 '18 at 13:25
  • perhaps it would behoove you to go to interpersonal.stackexchange.com and ask how to effectively express disagreement, and how to not take criticism of your question as a personal attack. Hope you have a good day, best of luck with your software engineering endeavors :) – TheCatWhisperer Mar 1 '18 at 22:00

Raw Java world uses Java propeties format to handle similar configuration tasks. Dedicated class Properties makes this trivial.

 try(Reader reader = Files.newBufferedReader(Path.get("config.properties"), StandardCharsets.UTF_8)) {
     Properties properties = new Properties();
     int size = Integer.valueOf(properties.getProperty("size"));
     int format = Integer.valueOf(properties.getProperty("format"));
| improve this answer | |
  • I agree. Why reinvent the wheel? – Neil Feb 28 '18 at 13:06
  • 1
    If you dont like properties there is still yaml, json, xml ... all of which wont re-invent wheels though some might need external libraries. – Newtopian Feb 28 '18 at 14:35
  • XML, YAML, JSON model tree structures, while OP requests a key-value storage, which is a simpler subset. – Basilevs Feb 28 '18 at 15:48

if you use Java (and you do), work with Spring. it makes your life so much easier in such cases!

and specifically in this case, you just use something like

    location="classpath:default.properties" />

and you have all the properties ready for use (it reads everything upon startup)

| improve this answer | |
  • 5
    For the OP's needs, Spring is like using a cannon on a squirrel. Heavy to lift, hard to aim, and leaves a mess behind. – BobDalgleish Feb 28 '18 at 13:25

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