l
文件
Context.MODE_PRIVATE:为默认操作模式,代表该文件是私有数据,只能被应用本身访问,在该模式下,写入的内容会覆盖原文件的内容,如果想把新写入的内容追加到原文件中。可以使用Context.MODE_APPEND
Context.MODE_APPEND:模式会检查文件是否存在,存在就往文件追加内容,否则就创建新文件。
Context.MODE_WORLD_READABLE和Context.MODE_WORLD_WRITEABLE用来控制其他应用是否有权限读写该文件。
MODE_WORLD_READABLE:表示当前文件可以被其他应用读取;MODE_WORLD_WRITEABLE:表示当前文件可以被其他应用写入。
如果希望文件被其他应用读和写,可以传入:
openFileOutput("itcast.txt", Context.MODE_WORLD_READABLE + Context.MODE_WORLD_WRITEABLE);
/** * @author * @文件操作类:新建文件、删除、读写 * */ package com.gogler.gps.infor; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.PrintStream; import android.content.Context; import android.os.Environment; public class FileHelper { private final Context context; /** SD卡是否存在 **/ private boolean hasSD = false; /** SD卡的路径 **/ private final String SDPATH; /** 当前程序包的路径 **/ private final String FILESPATH; /** * 从调用者传入 * @param Context like this.getApplicationContext() * * * */ public FileHelper ( Context context ) { this.context = context; hasSD = Environment.getExternalStorageState().equals( android.os.Environment.MEDIA_MOUNTED); SDPATH = Environment.getExternalStorageDirectory().getPath(); FILESPATH = this.context.getFilesDir().getPath(); } /** * * 在SD卡上创建文件 * * * * @throws IOException */ public File createSDFile(String fileName) throws IOException { final File file = new File(SDPATH + "//" + fileName); if (!file.exists()) { file.createNewFile(); } return file; } /** * * 删除SD卡上的文件 * * * @param fileName */ public boolean deleteSDFile(String fileName) { final File file = new File(SDPATH + "//" + fileName); if ((file == null) || !file.exists() || file.isDirectory()) { return false; } return file.delete(); } public String getFILESPATH() { return FILESPATH; } public String getSDPATH() { return SDPATH; } public boolean hasSD() { return hasSD; } /** * * 读取SD卡中文本文件 * * * * @param fileName * * @return String */ public String readSDFile(String fileName) { final StringBuffer sb = new StringBuffer(); final File file = new File(SDPATH + "//" + fileName); try { final FileInputStream fis = new FileInputStream(file); int c; while ((c = fis.read()) != -1) { sb.append((char) c); } fis.close(); } catch (final FileNotFoundException e) { e.printStackTrace(); } catch (final IOException e) { e.printStackTrace(); } return sb.toString(); } /** * * * 写SD卡上的文件 * * * @param String * fileName,String content * * * @return boolean * * * @author Administrator */ public boolean writeFileData(String fileName, String content) { try { final FileOutputStream fout = new FileOutputStream(SDPATH + "//" + fileName, true); final PrintStream ps = new PrintStream(fout); ps.println(content); ps.close(); } catch (final IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return true; } }
l
SharedPreferences
创建可跨程序使用的Preferrence
package com.gogler.SharePreferences; import android.app.Activity; import android.content.Context; import android.content.SharedPreferences; import android.content.SharedPreferences.Editor; import android.content.res.Resources; import android.os.Bundle; public class SharePreferencesTestActivity extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); // 使用可跨程序读写的Preference SharedPreferences sp = getSharedPreferences("hello", Context.MODE_WORLD_READABLE+Context.MODE_WORLD_WRITEABLE); Editor editor = sp.edit(); String str = "SharePreferencesTestActivity: "+getResources().getString(R.string.hello); editor.putString("helloStr", str ); editor.commit(); } }
读取其他APP的Preference
package sharePreferences.gogler.com; import android.app.Activity; import android.content.Context; import android.content.SharedPreferences; import android.content.pm.PackageManager.NameNotFoundException; import android.os.Bundle; import android.widget.TextView; public class UseSharePreferencesTestActivity extends Activity { final String packageName = "com.gogler.SharePreferences"; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); // 访问其他APP的Preference try { Context otherAppContext = createPackageContext(packageName, Context.CONTEXT_IGNORE_SECURITY); SharedPreferences sp = otherAppContext.getSharedPreferences("hello", Context.MODE_WORLD_READABLE); TextView tv=(TextView)findViewById(R.id.tv); tv.setText(sp.getString("helloStr", "")); } catch (NameNotFoundException e) { e.printStackTrace(); } } }