1

I am trying to write/read files in JavaFX while using the Gluon plugin, and I am not having any luck. If i run the program as a Desktop App, it works fine, I've even used the same functions in Android IDE and it works fine, however if I send the program to my phone with Gluon, it does not work. I wanting to know how to save/read to files in Gluon, and what have I done wrong, and why does it work as Desktop and on Android but not Gluon. The two fcns I used are:

public void writeToFile() {

  try {

      FileOutputStream f = new FileOutputStream("Meal1_Protein.txt");
      f.write(choice.getBytes()); //Choice is input from a dropdown
         f.close();

  } catch (FileNotFoundException e) {
      e.printStackTrace();
  } catch (IOException e) {
      e.printStackTrace();
  }
  Toast toast = new Toast(choice);
  toast.show();

 }

public void readFromFile(){

try {
    FileInputStream f = new FileInputStream("Meal1_Protein.txt");
    InputStreamReader is = new InputStreamReader(f);
    BufferedReader br = new BufferedReader(is);
    StringBuffer buff = new StringBuffer();

    String lines;

    while((lines = br.readLine())!=null){

        buff.append(lines);
        buff.append("\n");
    }


   carbInput.setText(buff.toString()); // inserting into a txtfield 
   //to see if it I can read strings back in. 
    Toast toast = new Toast(buff.toString());
    toast.show();

} catch (FileNotFoundException e) {
    e.printStackTrace();
} catch (IOException e) {
    e.printStackTrace();
}

}

Squatch
  • 11
  • 2
  • 2
    When you say it doesn't work on your phone, what behaviour/errors are you seeing? – craigcaulfield Aug 19 '18 at 23:30
  • Basically it should show what I selected on my dropdown menu in a textfield. And it is not doing that, so I did a Toast with buff.toString() to see what is being read in, and it shows nothing. – Squatch Aug 19 '18 at 23:33
  • When you run it on Android, do you get any exception? (use `adb logcat` from a terminal to see the output of the process). To write on mobile, you may want to have a look at the `Storage` service: `Services.get(StorageService.class).ifPresent(s -> s.getPrivateStorage())`. That will give you a File/Directory in a private environment, related to the installed app. Have a look at this [sample](https://github.com/gluonhq/gluon-samples/tree/master/gluon-SQLite) for a similar case. – José Pereda Aug 22 '18 at 18:36
  • I tried using services from an example I saw earlier, I attempted to use logcat and looked for the file name using FINDSTR in Windows and it replies with "No such file or directory" – Squatch Aug 23 '18 at 20:10
  • Tried using, if (!f.exists()) { f.mkdirs(); } but that did not work either and I have the internal and external permissions set in the manifest – Squatch Aug 23 '18 at 20:49
  • Did you run the Gluon SQLite sample? Does it work for you? Note that the private storage is _private_ and you won't be able to access it externally, but only through your app. – José Pereda Aug 25 '18 at 19:51

0 Answers0