如何进行文件IO操作

  Android系统在文件IO操作上主要还是采用Java中的iava.io.FileInputStream和java.io.FileOutputStream来对文件进行读写操作,创建文件或文件夹使用java.io.File类来完成,同时读写文件需要相应的权限,否则将会出现Exception。


Android系统本身提供了2个方法用于文件的读写操作:


openFileInput(String name)方法返回FileIputStream上输入流和openFileOutput(String name,int mode)方法返回FileOutputStream输出流。

这两个方法只支持操作当前应用的私有目录下的文件,传入文件时只需传入文件名即可。对于存在的文件,默认使用覆盖私有模式(Context.MODE_PRIVATE)对文件进行写操作,如果想以增量方式写入一寸文件,需要指定输出模式为Context.MODE_APPEND.




下面为文件操作简单小例子:




FileActivity.java代码:


public class FileActivity extends Activity {


private static final String FILE_NAME="test.txt";

private EditText edittext;

private Button button1,button2;

private TextView text;

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_file);

edittext=(EditText)findViewById(R.id.edittext);

button1=(Button)findViewById(R.id.button1);

button2=(Button)findViewById(R.id.button2);

text=(TextView)findViewById(R.id.text);

button1.setOnClickListener(new View.OnClickListener() {

public void onClick(View arg0) {

// TODO Auto-generated method stub

saveDateToFile();

}

});

        button2.setOnClickListener(new View.OnClickListener() {

public void onClick(View arg0) {

// TODO Auto-generated method stub

readDateFile();

}

});

}

private void saveDateToFile(){

try {

//FileOutputStream fos=openFileOutput(FILE_NAME, Context.MODE_PRIVATE);

//构造输出流对象,使用覆盖私有模式

FileOutputStream fos=openFileOutput(FILE_NAME, Context.MODE_APPEND);

//构造输出流对象,使用增量模式

String textfile=edittext.getText().toString();

//获取输入内容

fos.write(textfile.getBytes());

//将字符串转换为byte数组写入文件

fos.close();

} catch (FileNotFoundException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

private void readDateFile(){

try {

FileInputStream fis=openFileInput(FILE_NAME);

byte[] buffer=new byte[1024];

//构建byte数组用于保存读入的数据

   fis.read(buffer);

//读取数据放在buffer中

   String textfile=EncodingUtils.getString(buffer,"UTF-8");

   text.setText(textfile);

   text.setTextColor(Color.RED);

} catch (FileNotFoundException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

}





写操作就是saveDateToFile()方法,读操作就是readDateFile()方法。






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"

    tools:context=".FileActivity" >


    <EditText

        android:id="@+id/edittext"

        android:layout_width="fill_parent"

        android:layout_height="wrap_content"

        android:hint="请输入..." />


    <Button

        android:id="@+id/button1"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:layout_alignParentLeft="true"

        android:layout_below="@+id/edittext"

        android:text="存入" />


    <TextView

        android:id="@+id/text"

        android:layout_width="fill_parent"

        android:layout_height="wrap_content"

        android:layout_alignParentLeft="true"

        android:layout_below="@+id/button1"

        android:text="文件内容:" />


    <Button

        android:id="@+id/button2"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:layout_below="@+id/edittext"

        android:layout_toRightOf="@+id/button1"

        android:text="读出" />


</RelativeLayout>





创建的test.txt文件就在SD卡目录下面。

你可能感兴趣的:(java,android,public,文件夹,如何,文件存取)