简体   繁体   中英

Java Reading file with settings

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);

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!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM