1

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?

P5music
  • 3,197
  • 2
  • 32
  • 81
  • You're relying on the response of the `available()` method which is a common bug. `available()` makes no guarantees to return the size of the file. This code won't work in many places. @eric gave the right response – Shai Almog May 17 '21 at 03:26

1 Answers1

1

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);
Eric
  • 124
  • 1
  • 6