We are about to switch to a new forum software. Until then we have removed the registration on this forum.
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"
withdataPath("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.
@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:
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 writemonFichier+"AccountInfo.csv"
in saveTable() and loadTable()!@Night I'm completely lost on how you got this to work, I keep getting the same error about the path separator, even though I'm using
path = this.getActivity().getFilesDir();
This function also doesn't add a / to the end of the file it returns, so when I do
path+"save.csv"
, it returnsdata/user/0/com.d3f4alt.tap/filessave.csv
saveTable(table, path+"save.csv");
is how I'm saving it, am I doing something wrong?EDIT: I managed to fix it somehow, but now the app crashes when it's relaunched, but doesn't report any errors
Hopefully the following sketch provides some insight of how to manage resources. The main purpose of this demo is to show how to save or open files. When the application runs for the first time, it shows the content of the folder where any new files are created (either by
saveStrings()
,saveBytes()
, etc) as well as the content of the assets folder. Notice that any created file by Processing function is not created in the assets folders as this folder is read only and any of these assets are only access viasurface.getAssets().open(filename)
function.As I was describing, when the application runs the first time, it list the content of resources. Then you will have a split screen. Do the following:
When the green message box is shown, it last for 2 seconds. For those two seconds you are not allowed to neither open nor create any file. However, if you click on the screen before the green box disappears, you will get a report of your resource folders and your current file storing strings will be deleted.
Notice that when you save a file, and if you are not using the external storage, you do not need to specify the folder "data" inside this path. This is handled by Processing either if you are running your sketch in Java or in Android mode. Check the code and ask any question you have or if you have any idea to improve it or provide additional comment.
Last point: No permissions required for this sketch. Notice that "write on external device" permission is required if you decide to use external storage to save your files.
Kf
Keyword: kf_keyword android_assets android_sketchpath android_savefile save_files savefiles