Search code examples
javaiostextiocodenameone

CodenameOne - routine to read text file does work on CN1 simulator, does not work on iOS


I am testing my CodenameOne app on iOS and I have troubles with this method:

 public void readFile() {
  
    JSONText=new String();

    InputStream is = null;
    try {
        is = FileSystemStorage.getInstance().openInputStream(Utils.getRootPath()+DATA_FILE_NAME);

    InputStreamReader br = new InputStreamReader(is);
        int numChars=is.available();
        System.out.println("numchars "+String.valueOf(numChars)); // = 1
        char[] b=new char[numChars];
           
            br.read(b, 0,numChars);

            JSONText = new String(b);

        br.close();
    } catch (IOException e) {
     
    }

    System.out.println("read "+JSONText); // '{'
}

The above mentioned method does work on the CN1 simulator but

it does not work in iOS.

CodenameOne is sort of cross-platform but not as well as Java itself. Some methods have to be tested on each platform.

I am interested in iOS platform.

That method seems not to be working on iOS, indeed it reads just 1 char only.

What is the correct routine to just read a simple text file to be used in a CN1 app on iOS?


Solution

  • You don't need all this code. You can just use Util.readToString() to read the InputStream directly into a String.

    String jsonText=Util.readToString(is);
    System.out.println(jsonText);