2

I wrote a code in java to run some scripts which can return different result depending on the environment setup. I would like to store the result of every execution. I try with properties file but every time it executes, it overwrites the previous result in config.properties. I did a research but not find any most likely example. This is my code to return properties file. The value which will be different are TCpassed and TCfailed on every execution.

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Properties;

public class ExecutionProperties {

    public void setConfigProperties(int tcPassed, int tcFailed){
        Properties prop = new Properties();
        OutputStream output = null;

        try {

            output = new FileOutputStream("config.properties");

            // set the properties value
            prop.setProperty("TCpassed", ""+ tcPassed);
            prop.setProperty("TCfailed", ""+ tcFailed);

            // save properties to project root folder
            prop.store(output, null);

        } catch (IOException io) {
            io.printStackTrace();
        } finally {
            if (output != null) {
                try {
                    output.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

        }
    }

}

Is it possible to get the execution time and store it in config.properties in order to differentiate with the previous result?

Thanks before

Peanut
  • 3,753
  • 3
  • 31
  • 45
user3922604
  • 131
  • 1
  • 10

2 Answers2

0

You can use append mode using constructor FileOutputStream("config.properties", true) Sample properties file after couple of execution

#Mon May 04 13:03:29 IST 2015
TCpassed=1
TCfailed=1
#Mon May 04 13:04:03 IST 2015
TCpassed=1
TCfailed=1
barunsthakur
  • 1,196
  • 1
  • 7
  • 18
0

Property file are usually key value pairs, e.g.

TCpassed=9
TCfailed=1

So if you want to store the result of every execution, you need a different key for every execution. And if you want to append to the property file, you can:

  1. Load the property file as Properties object;
  2. Add new entry to the Properties object;
  3. Write the Properties Object back to the file;

Here is an example:

  public static void appendTestResult(File propertyFile, int tcPassed, int tcFailed) {
        try {
            Properties properties = loadProperties(propertyFile);
            String testId = getTestID();
            properties.setProperty("TCpassed_" + testId, String.valueOf(tcPassed));
            properties.setProperty("TCfailed_" + testId, String.valueOf(tcFailed));
            saveProperties(propertyFile, properties);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void saveProperties(File propertyFile, Properties properties) throws IOException {
        OutputStream outputStream = null;
        try {
            outputStream = FileUtils.openOutputStream(propertyFile);
            properties.store(outputStream, "new test");
        } finally {
            IOUtils.closeQuietly(outputStream);
        }
    }

    public static Properties loadProperties(File propertyFile) throws IOException {
        InputStream inputStream = null;
        try {
            inputStream = FileUtils.openInputStream(propertyFile);
            Properties properties = new Properties();
            properties.load(inputStream);
            return properties;
        } finally {
            IOUtils.closeQuietly(inputStream);
        }
    }

    public static String getTestID() {
        return new SimpleDateFormat("yyyyMMddHHmmss").format(new Date());
    }
Harry.Chen
  • 1,582
  • 11
  • 12