1.sdcard文件的保存
step1. 编写如下代码:
package com.activity;
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;
import android.widget.Toast;
/*sdcar文件内容的输出
* */
public class FileSdCardSaveActivity extends Activity {
/** Called when the activity is first created. */
private static final String FILENAME = "mymn.txt";// 设置文件的名称
private static final String DIR = "mndata";// 操作文件夹得名称
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// 判断sdscard是否存在
if (Environment.getExternalStorageState().equals(
Environment.MEDIA_MOUNTED)) {
//
File file = new File(Environment.getExternalStorageDirectory()+File.separator+DIR+File.separator+FILENAME);// 定义要操作的文件
if (!file.getParentFile().exists()) {
file.getParentFile().mkdirs();// 创建父文件夹路径
}
PrintStream outStream = null;
try {
outStream = new PrintStream(new FileOutputStream(file));
outStream.println("3Q大战又来了,嘿嘿");
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
if (outStream != null) {
outStream.close();
}
}
}else {
Toast.makeText(this,"保存失败,sd卡不存在",Toast.LENGTH_LONG ).show();
}
}
}
step2: 配置sdcard写权限:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.activity"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="15" />
<!-- 配置sdcard写权限 -->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:name=".FileSdCardSaveActivity"
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>
step3.查看是否写进去。
Enviroment定义的常量和方法:
2.sdcard文件的读取
step1.在res,下增加raw文件夹,添加要读取的mymldn.txt文件
step2.在main.xml中编写布局文件
<?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:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/msg" />
</LinearLayout>
step3.编写如下代码
package com.activity;
import java.io.InputStream;
import java.util.Scanner;
import android.app.Activity;
import android.content.res.Resources;
import android.os.Bundle;
import android.widget.TextView;
/**
* sdcard文件的读取
* @author Administrator
*
*/
public class SdcardFileReadActivity extends Activity {
private TextView msgView = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
this.msgView = (TextView) findViewById(R.id.msg);
Resources resources = super.getResources();// 资源类操作
InputStream inputStream = resources.openRawResource(R.raw.mymldn);// 为读取的内容设置输入流
Scanner scanner = new Scanner(inputStream);
StringBuffer buffer = new StringBuffer();
while (scanner.hasNext()) {
buffer.append(scanner.next()).append("\n");
}
scanner.close();
try {
inputStream.close();
} catch (Exception e) {
// TODO: handle exception
}
this.msgView.setText(buffer);
}
}
读取资源文件
在android系统中,进行资源的读取,资源文件的id都会自动通过R.java这个类生成,可以通过android.content.res.Resources 类读取。使用Resources类的public InputStream openRawResource(int id)方法;