3

There are several examples in Java about how to save a value in a properties file - for example:

How to write values in a properties file through java code

https://www.mkyong.com/java/java-properties-file-examples/

You will notice that they all use the "import java.io.FileOutputStream" code, however when I try and use the same code I get this error ....

error: cannot find symbol
import java.io.FileOutputStream;
symbol:   class FileOutputStream
location: package java.io

I believe that this is a standard Java library and should be available right? What am I doing wrong?

Thanks

Community
  • 1
  • 1
Steven Mark
  • 265
  • 2
  • 9
  • 2
    Have you added JRE to your classpath? Looks like class is unable to find class FileOutputStream – Atul Feb 18 '17 at 06:33
  • Thanks for the advice - but wouldn't this then fail for every import? Why is is just this one? – Steven Mark Feb 18 '17 at 06:37
  • 1
    This is just an assumption because program is failing at importing Class. If you can paste your program it would be easier to analyze. – Atul Feb 18 '17 at 06:45

1 Answers1

2

Codename One doesn't support full Java API (The Standard Libraries) and here is why... Why we don't support the full Java API.

If you need the Properties Object, read about it in this blog.

Codename One has another properties file which is used to guide your app building process, and you can add some data that Codename One supports by Right-clicking your project, Going to Properties, switching to Build Hint Tab and entering the key-value pair there.

Diamond
  • 7,428
  • 22
  • 37
  • Thank you for this. I browsed to this location in the documentation https://www.codenameone.com/javadoc/com/codename1/io/Properties.html#setProperty-java.lang.String-java.lang.String- And it talks about the setProperty() function being able to update a value. However when I use it the value doesn't change. Here is my code ... switch (evt.getCommand().toString()) { case "SaveValue": try{ valueProperties.setProperty("current_Value", txtValue.getText()); txtValue.setEditable(false); } – Steven Mark Feb 18 '17 at 23:46
  • Ahhh I've just realised that the setProperty() function only changes the running value of the property - it doesn't actually save it back to the file. I need to figure out how to do that. Ok Ok we're getting somewhere now :-) – Steven Mark Feb 18 '17 at 23:56
  • That's the same behavior as JavaSE you can use Properties.store() to save the file. However, Codename One has a better approach that's more seamless than JavaSE to store a simple variable: `Preferences`. Use `String val = Preferences.get("MyVariableName", defaultValue);` and `Preferences.set("MyVariableName", actualValue);` – Shai Almog Feb 19 '17 at 06:03