ITeye博客

数据存储的另一种方式

wangstar · 2009-06-19 09:22 · android 学习 · Markdown

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);

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。