Hi
I have read through numerous tutorials, which all tell me a little bit but I am lost on the overall picture. So how about I start at the start.
I have a Java app I wrote that saves a string of data as a csv file: data.txt - I want to be able to open this file in my android app, so the first thing I did was use the 'push' option in eclipse to save it onto the device. i could save it and view it on the sdcard, but when trying to dave to the app data folder (data/data/package) it seems to work but I cannot see the file there.
So after reading around I saw this piece of code:
which works fine, though I cannot seem to see the file (I presume this is because it is located in the data/data/package/ folder and I cannot see that through eclipse DDMS.
>> Aside: I seem to have a lot of trouble accessing the file explorer using eclipse. It seems v. tempermental - sometimes I can see all the files and folders, other times I cannot open any of the folders (though it does list data, sdcard and system), with no discernable reason. I just did a test: loaded up the emulator on its own with wipe data set to 'on' and I can access the data folder but no subfolders of it
Anyway, When I try to use this same code to read my data.txt file from the sdcard I get the error: File sdcard/data.txt contains a path separator (I also tried the path /sdcard/data.txt but got the same error.
I read somewhere that openFileInput will not work with the sdcard.
This is really a kind of kludge just to test the data, since in the finished product the app wont read data from the sdcard. Am I missing something, should I serialise the data as XML? What is the best practice here?
Regards
ed
I have read through numerous tutorials, which all tell me a little bit but I am lost on the overall picture. So how about I start at the start.
I have a Java app I wrote that saves a string of data as a csv file: data.txt - I want to be able to open this file in my android app, so the first thing I did was use the 'push' option in eclipse to save it onto the device. i could save it and view it on the sdcard, but when trying to dave to the app data folder (data/data/package) it seems to work but I cannot see the file there.
So after reading around I saw this piece of code:
Code:
try { // catches IOException below
final String TESTSTRING = new String("Hello Android");
// ##### Write a file to the disk #####
/* We have to use the openFileOutput()-method
* the ActivityContext provides, to
* protect your file from others and
* This is done for security-reasons.
* We chose MODE_WORLD_READABLE, because
* we have nothing to hide in our file */
FileOutputStream fOut = openFileOutput("samplefile.txt",
MODE_WORLD_READABLE);
OutputStreamWriter osw = new OutputStreamWriter(fOut);
// Write the string to the file
osw.write(TESTSTRING);
/* ensure that everything is
* really written out and close */
osw.flush();
osw.close();
// ##### Read the file back in #####
/* We have to use the openFileInput()-method
* the ActivityContext provides.
* Again for security reasons with
* openFileInput(...) */
FileInputStream fIn = openFileInput("samplefile.txt");
InputStreamReader isr = new InputStreamReader(fIn);
/* Prepare a char-Array that will
* hold the chars we read back in. */
char[] inputBuffer = new char[TESTSTRING.length()];
// Fill the Buffer with data from the file
isr.read(inputBuffer);
// Transform the chars to a String
String readString = new String(inputBuffer);
// Check if we read back the same chars that we had written out
boolean isTheSame = TESTSTRING.equals(readString);
// WOHOO lets Celebrate =)
Log.i("File Reading stuff", "success = " + isTheSame);
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
>> Aside: I seem to have a lot of trouble accessing the file explorer using eclipse. It seems v. tempermental - sometimes I can see all the files and folders, other times I cannot open any of the folders (though it does list data, sdcard and system), with no discernable reason. I just did a test: loaded up the emulator on its own with wipe data set to 'on' and I can access the data folder but no subfolders of it
Anyway, When I try to use this same code to read my data.txt file from the sdcard I get the error: File sdcard/data.txt contains a path separator (I also tried the path /sdcard/data.txt but got the same error.
I read somewhere that openFileInput will not work with the sdcard.
This is really a kind of kludge just to test the data, since in the finished product the app wont read data from the sdcard. Am I missing something, should I serialise the data as XML? What is the best practice here?
Regards
ed