1、main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" > <EditText
android:id="@+id/numET"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="请输入文件名" /> <EditText
android:id="@+id/contentET"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="请输入文件内容"//提示文本
android:inputType="textMultiLine"
android:minLines="3" />//最小为3行,多了自动变大 <LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" > <Button android:id="@+id/sdcBT"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="保存到sd卡"
android:onClick="onClick"
/> <Button android:id="@+id/romBT"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="保存到手机"
android:onClick="onClick"
/>
</LinearLayout> </LinearLayout>
2、MainActivity
package com.njupt.file1; import android.os.Bundle;
import android.os.Environment;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast; public class MainActivity extends Activity { private EditText numET;
private EditText contentET; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main); numET = (EditText) findViewById(R.id.numET);
contentET = (EditText) findViewById(R.id.contentET); } @Override
protected void onResume() {
super.onResume(); //Environment.getExternalStorageState().获取sd卡的状态,判断是安装,还是移除
findViewById(R.id.sdcBT).setEnabled(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED));
} @Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
} public void onClick(View v){
try{
String name = numET.getText().toString();
String content = contentET.getText().toString(); FileService service = new FileService(); switch(v.getId()){
case R.id.sdcBT: service.saveToSDCard(name,content);
break; case R.id.romBT: service.saveToRom(name,content);
break;
} Toast.makeText(getApplicationContext(), "保存成功", Toast.LENGTH_SHORT).show();
}catch(Exception e){
e.printStackTrace();
Toast.makeText(getApplication(), "dacard出异常", Toast.LENGTH_SHORT).show();
}
} }
3、FileService
package com.njupt.file1; import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException; import android.os.Environment; public class FileService { public void saveToSDCard(String name, String content) { FileOutputStream fos = null; try{ //Environment.getExternalStorageDirectory()。获取sd卡的路径
File file = new File(Environment.getExternalStorageDirectory(),name);
fos = new FileOutputStream(file); fos.write(content.getBytes());
}catch(Exception e){ e.printStackTrace(); }finally{ try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
} public void saveToRom(String name, String content) {
// TODO Auto-generated method stub } }
4、AndroidManifest.xml
最后在AndroidManifest.xml配上
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>