- Call
openFileOutput()
with the name of the file and the operating mode. This returns aFileOutputStream
.通过openFileOutput()建立FileoutputStream对象
- Write to the file with
write()
.创建Write对象并进行数据读写操作 - Close the stream with
close()
.最后关闭链接
以上就是讲数据文件保存到内部储存的基本步骤
下面用代码进行一下讲解
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" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" > <Button android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignBaseline="@+id/button1" android:layout_alignBottom="@+id/button1" android:layout_marginLeft="51dp" android:layout_toRightOf="@+id/button1" android:text="读取信息" /> <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:layout_marginLeft="31dp" android:layout_marginTop="142dp" android:text="保存信息" /> <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:layout_marginTop="47dp" android:text="保存数据到内部储存" android:textAppearance="?android:attr/textAppearanceMedium" /> </RelativeLayout>
package com.example.internalstorage; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import android.app.Activity; import android.content.Context; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.Button; import android.widget.Toast; public class MainActivity extends Activity { private Button saveData, getData; private static final String FILENAME = "flyou.txt"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); saveData=(Button)this.findViewById(R.id.button1); getData=(Button)this.findViewById(R.id.button2); saveData.setOnClickListener(new View.OnClickListener() {//保存信息 @Override public void onClick(View v) { // TODO Auto-generated method stub FileOutputStream out = null; try { String infoString = "风飞雪未扬"; out = openFileOutput(FILENAME, Context.MODE_PRIVATE); out.write(infoString.getBytes()); Toast.makeText(MainActivity.this, "数据保存成功", 2).show(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { try { if (out!=null) { out.close(); } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }); // 读取信息 getData.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub FileInputStream input=null; try { input=openFileInput(FILENAME); byte[] data=new byte[1024]; int len=input.read(data); input.close(); // System.out.println(new String(data,0,len)); Toast.makeText(MainActivity.this, new String(data,0,len), 2).show(); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }); } }
打开信息
使用文件浏览器查看信息
将文件导出
用记事本打开
以上操作可以很容易的实现文件的内部存储过程
官方api当中还有关于如何存储缓存文件的说明,大家可以自行实验一下
下节预报: