MainActivity如下:
package cc.cv; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import android.os.Bundle; import android.app.Activity; import android.content.Context; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; /** * Demo描述: * 文件存储详细示例 * * 可利用Context的openFileOutput()方法将数据存储到文件中. * 默认的存储路径为/data/data/<packageName>/files/ * 可利用Context的openFileInput()方法将数据从文件中读出 * * 在Android中常用的存储方式: * SharedPreferences,数据库,SD卡,文件存储. * 对于最后一种方式平时用的不多,现在此写个例子 * 整理和复习一下. * * 注意事项: * 文件存储是不需要权限的与在SD卡上进行文件存储是不一样的 */ public class MainActivity extends Activity { private Button mSaveButton; private Button mGetButton; private Context mContext; private String mFileName; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); init(); } private void init(){ mContext=this; mFileName="testData"; mSaveButton=(Button) findViewById(R.id.saveButton); mSaveButton.setOnClickListener(new ClickListenerImpl()); mGetButton=(Button) findViewById(R.id.getButton); mGetButton.setOnClickListener(new ClickListenerImpl()); } private class ClickListenerImpl implements OnClickListener{ @Override public void onClick(View view) { switch (view.getId()) { case R.id.saveButton: saveDataToFile(mContext,mFileName); break; case R.id.getButton: getDataFromFile(mContext,mFileName); break; default: break; } } } /** * 向File中保存数据 */ private void saveDataToFile(Context context,String fileName){ String data="Hello,All"; FileOutputStream fileOutputStream=null; OutputStreamWriter outputStreamWriter=null; BufferedWriter bufferedWriter=null; try { fileOutputStream=context.openFileOutput(fileName, Context.MODE_PRIVATE); outputStreamWriter=new OutputStreamWriter(fileOutputStream); bufferedWriter=new BufferedWriter(outputStreamWriter); bufferedWriter.write(data); } catch (Exception e) { e.printStackTrace(); }finally{ try { if (bufferedWriter!=null) { bufferedWriter.close(); } } catch (Exception e) { e.printStackTrace(); } } } /** * 从File中读取数据 */ private void getDataFromFile(Context context,String fileName){ FileInputStream fileInputStream=null; InputStreamReader inputStreamReader=null; BufferedReader bufferedReader=null; StringBuilder stringBuilder=null; String line=null; try { stringBuilder=new StringBuilder(); fileInputStream=context.openFileInput(fileName); inputStreamReader=new InputStreamReader(fileInputStream); bufferedReader=new BufferedReader(inputStreamReader); while((line=bufferedReader.readLine())!=null){ stringBuilder.append(line); } System.out.println("--->"+stringBuilder.toString()); } catch (Exception e) { e.printStackTrace(); }finally{ try { if (bufferedReader!=null) { bufferedReader.close(); } } catch (Exception e) { e.printStackTrace(); } } } }
main.xml如下:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" > <Button android:id="@+id/saveButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="向File中保存数据" android:layout_centerHorizontal="true" android:layout_marginTop="100dp" /> <Button android:id="@+id/getButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="从File中获取数据" android:layout_centerHorizontal="true" android:layout_marginTop="260dp" /> </RelativeLayout>
时间: 2024-10-10 07:56:04