0

I'm trying to write a program, now I am stuck at a problem of importing previous settings from a file. I am just a beginner so I was thinking about saving settings in a text file, and then at the beginning of program initialize those. The txt file is supposed to look like this:

0:0 //number of My Custom Panels to be generated.

1:0 //Custom panel name(it will be an argument customFunction(panelName)

1:1 //This is panel position (from 1(top) to 5(bottom))

1:2 //This will hold String with Icon.png depending on panelName.

This is my first Approach(Just part of the code to give you the idea), but I have a feeling there is some faster/more code "economical" way. Without tons of if() 's...

try {
        FileReader fileReader = 
            new FileReader(fileName);

        BufferedReader bufferedReader = 
            new BufferedReader(fileReader);

        while((line = bufferedReader.readLine()) != null) {
            if(line.equals("")!= true)
                if(line.charAt(0) == 1){
                    for(int i = 0; i < 4; i++){
                        if(i == 0){
                            Name = line.
                        }
                        else if(i == 1){

                        }
                        else if(i == 2){

                        }
                        else if(i == 3){

                        }
                    }
                }
                else if(line.charAt(0) == 2){

                }

If only I have some ability to read line by line. Not whole file at all. Somehow tell the program when to get to the next line. I don't like the part "while((line = bufferedReader.readLine()) != null)" I was trying to use line = bufferedReader.readLine() in a loop but .println of this was always showing "null". If anyone can help I will be grateful. (If something is unclear I will try my best to explain);

  • You might want to consider [Properties](https://docs.oracle.com/javase/7/docs/api/java/util/Properties.html) instead. That way you don't have to write any of the loading/saving code. – Kayaman Feb 17 '16 at 10:37
  • The problem is I also want the program to generate such text file. At the end or when the user wants to save his/her settings. And I'm not familiar with Properties. – Joseph Stack Feb 17 '16 at 10:42
  • There's no problem. Properties has `store()` for saving and `load()` for loading. What kind of an excuse is "I'm not familiar with Properties"? Do some reading, then you'll be familiar with Properties and you won't attempt the absolutely stupid way that you've written in your example. Gives you a nicer format for the file too, human readable. – Kayaman Feb 17 '16 at 10:46

1 Answers1

0

Please check below URL from where I got this answer : https://stackoverflow.com/a/1318391/3226981

You can pass an InputStream to the Property, so your file can pretty much be anywhere, and called anything.

Properties properties = new Properties();
try {
  properties.load(new FileInputStream("path/filename"));
} catch (IOException e) {
  ...
}
Iterate as:

for(String key : properties.stringPropertyNames()) {
  String value = properties.getProperty(key);
  System.out.println(key + " => " + value);
}

I hope this helps!

Community
  • 1
  • 1
Suyash
  • 525
  • 1
  • 6
  • 10
  • It destroys my earlier intentions a little, but I will try to read how to use the properties. Otherwise, I'll be pestering you on. – Joseph Stack Feb 17 '16 at 10:54
  • If your configurations are not static and will change time to time thorugh application by user then you can even think of keeping separate table in DB. You can refer : http://crunchify.com/java-properties-file-how-to-read-config-properties-values-in-java/ – Suyash Feb 17 '16 at 11:00
  • So if I have 5 slots, each contains a Name, Icon_path, and so on I would have to do something like this: n - slot number `prop.setProperty("n_Name", ""); prop.setProperty("n_Icon", "/bg/IconX.png"); prop.setProperty("n_Hotkey1", "Shift"): prop.setProperty("n_Hotkey2", "F2");` – Joseph Stack Feb 17 '16 at 14:03
  • Yes, for more details you can refer to : http://stackoverflow.com/questions/15605107/how-can-i-use-parameters-in-a-messages-properties-file – Suyash Feb 18 '16 at 04:45