Search code examples
javaintellij-ideajava-11

Make writePropertyValueInPropertyFile() return 'void'


I am trying to import a method from one package to another but am unable to do so. So I have a method of Properties, where I have to write information on certain sections and retrieve them.

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;
import static co.spotify.utilities.Log.error;

public class PropertiesUtils {
    public static void writePropertyValueInPropertyFile(String fileName, String propertyName, String propertyValue)
            throws IOException {
        FileInputStream in = new FileInputStream(fileName);
        Properties props = new Properties();
        props.load(in);
        in.close();
        props.setProperty(propertyName, propertyValue);
        FileOutputStream out = new FileOutputStream(fileName);
        props.store(out, null);
        out.close();
    }
}

I have another package with name of utils, now here, I am unable to import that writePropertyValueInPropertyFile method. I am using following approach:

import static co.spotify.utilities.PropertiesUtils.writePropertyValueInPropertyFile;

public class UserInformationConstants {
       writePropertyValueInPropertyFile(userPropertiesFileName, "registeredEmail", userRegisteredEmail);
}

and IntelliJ is indicating that Make writePropertyValueInPropertyFile() return 'void'

What exactly is the problem?


Solution

  • You can't write code directly into the body of a class. It has to be within a method.

    Try something like the following instead:

    import static co.spotify.utilities.PropertiesUtils.writePropertyValueInPropertyFile;
    
    public class UserInformationConstants {
        public static void yourMethodNameHere() {
            writePropertyValueInPropertyFile(userPropertiesFileName, "registeredEmail", userRegisteredEmail);
        }
    }