---
title: "数据存储的另一种方式"
author: "wangstar"
date: "2009-06-19T09:22:15Z"
tags: []
slug: "untitled"
draft: false
lastmod: "2009-06-19T09:23:58Z"
id: 411338
url: "https://wangstar.iteye.com/blog/411338"
---

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