把文件存入SD卡中
首先加入读写权限
SD卡创建与删除文件权限
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"></uses-permission>
SD卡写入数据权限
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>
点击保存前:
点击保存:
SD卡根目录多了123.txt文件
点击读取:
读取成功。
代码:
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import android.app.Activity;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import com.luku.file.service.FileService;
public class fileActivity extends Activity
{
private EditText editText=null;
private Button button =null;
private TextView content =null;
private Button read=null;
private TextView textView1=null;
private Button exit=null;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
textView1=(TextView)findViewById(R.id.textView1);
content=(TextView)findViewById(R.id.content);
editText=(EditText)findViewById(R.id.filename);
button=(Button)findViewById(R.id.baocun);
read=(Button)findViewById(R.id.read);
exit=(Button)findViewById(R.id.exit);
button.setOnClickListener(listener);
read.setOnClickListener(listener);
exit.setOnClickListener(listener);
}
private OnClickListener listener=new OnClickListener()
{
int resId = R.string.success;
@Override
public void onClick(View v)
{
Button btn=(Button)v;
File file=new File(Environment.getExternalStorageDirectory(),//得到SD卡根目录
editText.getText().toString()); //保存文件的文件名
switch (btn.getId())
{
case R.id.baocun:
if(Environment.getExternalStorageState().
equals(Environment.MEDIA_MOUNTED)) //判断SD卡是否存在,并且可以可以读写
{
try
{
FileOutputStream stream=new FileOutputStream(file);
FileService.save(stream, content.getText().toString());
}
catch (IOException e)
{
e.printStackTrace();
resId = R.string.error;
Toast.makeText(fileActivity.this, resId, Toast.LENGTH_LONG).show();
}
Toast.makeText(fileActivity.this, resId, Toast.LENGTH_LONG).show();
}
else
{
Toast.makeText(fileActivity.this, "SD卡不存在或写保护", Toast.LENGTH_LONG).show();
}
break;
case R.id.read:
try
{
InputStream instream=new FileInputStream(file);
String string=FileService.read(instream);
System.out.println(string);
textView1.setText(string.toString());
} catch (Exception e)
{
e.printStackTrace();
Toast.makeText(fileActivity.this, "读取失败", Toast.LENGTH_LONG).show();
}
break;
case R.id.exit:
android.os.Process.killProcess(android.os.Process.myPid());
break;
default:
break;
}
}
};
}
FileService文件
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
public class FileService
{
/**
* 保存数据
* @param stream
* @param content
* @throws IOException
*/
public static void save(OutputStream stream , String content) throws IOException
{
stream.write(content.getBytes());
stream.close();
}
/**
* 读取数据
* @param inStream
* @return
* @throws IOException
*/
public static String read(InputStream inStream) throws IOException
{
ByteArrayOutputStream outputStream=new ByteArrayOutputStream();
int len=-1;
byte[] buffer =new byte[1024];
while( (len=inStream.read(buffer))!=-1 )
{
outputStream.write(buffer, 0, len);
}
byte[] data = outputStream.toByteArray();
inStream.close();
outputStream.close();
return new String(data);
}
}
main.xml文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="90px"
android:layout_height="wrap_content"
android:text="文件名称:"
android:textSize="21px"
android:id="@+id/filenamelebal"
/>
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/filenamelebal"
android:layout_alignTop="@id/filenamelebal"
android:id="@+id/filename"
></EditText>
></RelativeLayout>
<TextView
android:layout_width="90px"
android:layout_height="wrap_content"
android:text="内容:"
android:textSize="21px"
/>
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:minLines="3"
android:id="@+id/content"
></EditText>
<LinearLayout android:id="@+id/linearLayout1" android:layout_width="match_parent" android:layout_height="wrap_content">
<Button
android:layout_height="wrap_content"
android:id="@+id/baocun"
android:text="保存"
android:layout_width="wrap_content"></Button>
<Button
android:text="读取"
android:id="@+id/read"
android:layout_width="wrap_content"
android:layout_height="wrap_content"></Button>
<Button
android:text="退出"
android:id="@+id/exit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"></Button>
</LinearLayout>
<TextView
android:text="读取内容显示"
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</TextView>
</LinearLayout>
配置文件
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.luku.file"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="8" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"></uses-permission>
<application android:icon="@drawable/icon" android:label="@string/app_name">
<uses-library android:name="android.test.runner"></uses-library>
<activity android:name=".fileActivity"
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>
<instrumentation
android:name="android.test.InstrumentationTestRunner"
android:targetPackage="com.luku.file"
android:label="Test for my app"
></instrumentation>
</manifest>