文件读写

本文介绍如何在Android应用中实现对SDCard的文件读写操作,包括获取SDCard状态、读取文件内容及保存文件等。示例代码展示了如何使用FileInputStream和FileOutputStream进行读写,并检查存储权限。
摘要由CSDN通过智能技术生成
Android SDCard操作(文件读写,容量计算) :[url]http://zhuyonghui116.blog.hexun.com/56778119_d.html[/url]
Android中读写文件: [url]http://blog.csdn.net/cocodehouse/article/details/5974288[/url]

增加权限:<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
String sDStateString = android.os.Environment.getExternalStorageState();


// 拥有可读可写权限
if (sDStateString.equals(android.os.Environment.MEDIA_MOUNTED)) {
... ...
}

然后用FileInputStream和FileOutputStream进行读写。


<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.file.rw"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk android:minSdkVersion="10" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:name=".ReadWriteFilesActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>

</manifest>



<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<TextView
android:id="@+id/textview"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />

<EditText
android:id="@+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="a.txt" />


<Button
android:id="@+id/button1"
android:layout_width="70dp"
android:layout_height="wrap_content"
android:text="Load" />

<EditText
android:id="@+id/editText2"
android:layout_width="match_parent"
android:layout_height="wrap_content" >

<requestFocus />
</EditText>


<Button
android:id="@+id/button2"
android:layout_width="68dp"
android:layout_height="wrap_content"
android:text="Save" />

</LinearLayout>



package com.file.rw;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class ReadWriteFilesActivity extends Activity {
/** Called when the activity is first created. */
private Button button1,button2;
private EditText editText1,editText2;
private TextView textview;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
button1=(Button)findViewById(R.id.button1);
button2=(Button)findViewById(R.id.button2);
editText1 = (EditText)findViewById(R.id.editText1);
editText2 = (EditText)findViewById(R.id.editText2);
textview =(TextView)findViewById(R.id.textview);
//load file from sd card
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
textview.setText("Load............");
String sdStatus = android.os.Environment.getExternalStorageState();
if(sdStatus.equalsIgnoreCase(android.os.Environment.MEDIA_MOUNTED)){
textview.setText(editText1.getText());
File rootDir = android.os.Environment.getExternalStorageDirectory();
File readFile = new File(rootDir.getAbsolutePath()+File.separator+editText1.getText());
if(!readFile.exists()){
textview.setText(editText1.getText()+"不存在.");
}else{
try {
FileInputStream in = new FileInputStream(readFile);
byte[] buffer = new byte[in.available()];
in.read(buffer);
in.close();
editText2.setText(new String(buffer));
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
}//end of android.os.Environment.MEDIA_MOUNTED

}
});

button2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(editText1.getText()==null||editText1.getText().toString().trim().length()==0){
textview.setText("第一个输入框没有输入文件名.");
return;
}
if(editText2.getText()==null||editText2.getText().toString().trim().length()==0){
textview.setText("第而个输入框没有输入文件内容.");
return;
}
String sdStatus = android.os.Environment.getExternalStorageState();
if(android.os.Environment.MEDIA_MOUNTED.equalsIgnoreCase(sdStatus)){
File root = android.os.Environment.getExternalStorageDirectory();
File newFile = new File(root.getAbsolutePath()+File.separator+editText1.getText());
try {
if(!newFile.exists()){
newFile.createNewFile();
}
FileOutputStream out = new FileOutputStream(newFile);
out.write(editText2.getText().toString().getBytes());
out.close();
textview.setText("写文件成功.");
} catch (Exception e) {
// TODO: handle exception
}

}

}
});

}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值