package org.xin.demo;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.PrintStream;
import android.app.Activity;
import android.os.Bundle;
import android.os.Environment;
public class FileOperateDemo extends Activity {
private String FILENAME = "mytest.txt";
private String DIR = "srx";
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
if (Environment.getExternalStorageState().equals(
Environment.MEDIA_MOUNTED)) {// 判断外存卡是否就绪
String newfilename = Environment.getExternalStorageDirectory()
+ File.separator + DIR + File.separator + FILENAME;
System.out.println(newfilename);
File file = new File(newfilename);
if (!file.getParentFile().exists()) {// 判断路径是否存在,不存在就创建
file.getParentFile().mkdirs();// 创建路径
}
PrintStream out = null;
try{
out = new PrintStream(new FileOutputStream(file));
out.println("hello" + "\n");
out.println("努力学习Android");
} catch (FileNotFoundException e) {
e.printStackTrace();
}finally{
if(out!=null){//如果输出流不为空则关闭
out.close();
}
}
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="org.xin.demo"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="10" />
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".FileOperateDemo"
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>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
</manifest>