How to load and save table on android without error "File contains a path separator"?

edited December 7 in Android Mode

Hey! I'm trying to develop an app on android with processing that needs to save information in a table for next time use and then retrieve it later on. (Sort of like a leaderboard record). The problem is that saveTable() and loadTable() don't work because the error java.lang.IllegalArgumentException: File contains a path separator is returned. Here is bascially what I'm trying to do (Which works fine on normal PC): In setup:

try { PersonalDetails = loadTable("AccountInfo.csv", "header"); // PersonalDetails is my table variable. if (PersonalDetails.getRowCount() > 0) { Registered = true; LocalScore = PersonalDetails.getRow(0).getInt("Score"); } } catch (NullPointerException e) { Table createCSV = new Table(); createCSV.addColumn("ID", Table.INT); createCSV.addColumn("Score", Table.INT); createCSV.addColumn("Name", Table.STRING); saveTable(createCSV, "data/AccountInfo.csv"); PersonalDetails = loadTable("AccountInfo.csv", "header"); if (PersonalDetails.getRowCount() > 0) { Registered = true; LocalScore = PersonalDetails.getRow(0).getInt("Score"); } }

And then in draw basically continuously save as score updates:

void draw() { if (Start) { if (Registered) { PersonalDetails.setInt(0, "Score", LocalScore); saveTable(PersonalDetails, "data/AccountInfo.csv"); } } background(255); }

As for android I continued to get the error stated above. I've tried multiple solutions to try to fix this including an InputStream and BufferedReader, this read the file perfectly fine:

BufferedReader br; String line; void readInfo() { try { InputStream is = createInput("AccountInfo.txt"); br = new BufferedReader(new InputStreamReader(is, "UTF-8")); while ((line=br.readLine())!=null) { System.out.println(line); } } catch (IOException e) { System.out.println("IOException when trying to read AccountInfo.txt:\n"+e); System.exit(0); } }

But, every approach i've taken so far to try to write the file failed. I'm assuming read works better because it will auto search the /files/ directory when not specified with a path. So far i've tried to use a BufferedWriter and PrintWriter in the same way as readInfo() above, hoping it would work, but it didn't:

PrintWriter saveInformation; BufferedWriter writer = null; void saveInfo() { try { writer = new BufferedWriter(new FileWriter("accountinfo.csv")); saveInformation = new PrintWriter("accountinfo.csv"); saveInformation.println("Changed Text File..."); writer.write("Changed Text file..."); } catch ( IOException e) { } finally { try { if ( writer != null ) writer.close( ); if (saveInformation != null) saveInformation.close(); } catch ( IOException e) { } } }

I have also tried to use the AssetManager, couldn't get that working. Also, I tried to use all above listed methods with relative file path and absolute file path. Each of these would similarly return the same error "java.lang.IllegalArgumentException: File contains a path separator".

Any help or feedback on the matter would be immensely very much greatly incredibly much appreciated!

Answers

  • Not tested but try replacing "AccountInfo.csv" with dataPath("AccountInfo.csv")

    Kf

  • Hey Kf, I tried this but sadly it didn't seem to fix the issue :( Still keeps getting the same path separator error. Thanks, Night.

  • edited December 7 Answer ✓

    @Night===

    the error is a standard one with Android: the name of the created file cannot contain any separator(/); so you have to change your:

    saveTable(createCSV, "data/AccountInfo.csv");

    Instead write:

    String chemin = dataPath();
    saveTable(chemin+"AccountInfo.csv");
    

    it could be also possible to use File for doing the same; in this case you get your files dir with File monFichier = this.getActivity().getFilesFir() and use this as path adding a"/" to it.

  • @akenaton WOW Thankyou SOOO much! Your answer was the only one that actually worked! I've been searching and trying every single solution on the forum for agesss and none of them worked!

    Your first method actually still returned the same error using dataPath(). But, the second worked perfectly!

    Just to clarify for anyone coming into this: Use this.getActivity().getFilesDir(); for the directory in the file variable, you don't need a "/", just write monFichier+"AccountInfo.csv" in saveTable() and loadTable()!

Sign In or Register to comment.