数据存储的另一种方式
Writing a 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();
Reading the file
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; // 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);