一 Android——数据存储(二十一)( 三 )


范例:向SD卡上保存文件
现在虽然程序写好了,既然是对SD card的操作,那么一定需要具备操作的权限 。

现在我们只需要使用此类去判断是否存在 。
if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){//表示存储目录能够进行你读写操作System.out.println("表示sdcard存在");}
package com.example.filesave;import java.io.File;import java.io.FileOutputStream;import java.io.PrintStream;import android.app.Activity;import android.os.Bundle;import android.os.Environment;import android.widget.Toast;public class FileSaveActivity extends Activity {public static final String FILENAME = "/mnt/sdcard/wanczy/jjm/wanczy.doc";@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);super.setContentView(R.layout.activity_file_save);if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {// 表示存储目录能够进行你读写操作FileOutputStream output = null;PrintStream out = null;try {File file = new File(FILENAME);if (!file.getParentFile().exists()) {// 如果文件夹不存在file.mkdirs();}output = new FileOutputStream(file, true);// 表示创建了一个文件的字节输出流//,这种写法不能进行文件内容的追加// 在IO操作中,打印流操作是最方便的out = new PrintStream(output);out.print("《幽窗小记》 宠若不惊 闲看庭前花开花落 去留无意 漫随天外云卷云舒");} catch (Exception e) {} finally {if (null != out) {out.close();}}}else{Toast.makeText(this, "sdcard不存在,请使用其他保存路径", Toast.LENGTH_SHORT).show();}}}
这个程序更加适合在真机上进行操作 。现在已经完成输出的功能,现在进行一些输入的显示 。
package com.example.filesave;import java.io.File;import java.io.FileInputStream;import java.util.Scanner;import android.app.Activity;import android.os.Bundle;import android.os.Environment;import android.widget.TextView;import android.widget.Toast;public class FileSaveActivity extends Activity {private static final String FILENAME = "wanczy.doc";private static final String DIR = "CSDN/mlz";private TextView msg = null;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);super.setContentView(R.layout.activity_file_save);this.msg = (TextView) super.findViewById(R.id.msg);if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {FileInputStream input = null;Scanner sc = null;try {File file = new File(Environment.getExternalStorageDirectory().toString()+File.separator+DIR+File.separator+FILENAME);input = new FileInputStream(file);sc = new Scanner(input);while(sc.hasNext()){this.msg.append( sc.next());}} catch (Exception e) {} finally {sc.close();}}else{Toast.makeText(this, "sdcard不存在,请使用其他保存路径", Toast.LENGTH_SHORT).show();}}}
以上保存在的程序与之前的不保存在并没有本质的区别 。
package com.example.filesave;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;public class FileSaveActivity extends Activity {private TextView msg = null;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);super.setContentView(R.layout.activity_file_save);this.msg = (TextView) super.findViewById(R.id.msg);Resources res = super.getResources();//取得Resources对象InputStream in = res.openRawResource(R.raw.wanczy);//将资源文件加入到字节输入流、Scanner sc = null;StringBuffer sb = new StringBuffer();try {sc = new Scanner(in);while(sc.hasNext()){sb.append(sc.next()+"\n");}}catch(Exception e){}finally{sc.close();}this.msg .setText(sb.toString());}}