一、機身內存 java
package com.pas.loginservice; import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStreamReader; import java.util.HashMap; import java.util.Map; import android.content.Context; public class LoginService { public static boolean saveUserInfo(Context context,String username,String password) { File file=new File(context.getFilesDir(),"info.txt"); try { FileOutputStream fos=new FileOutputStream(file); fos.write((username+"##"+password).getBytes()); fos.close(); return true; } catch (FileNotFoundException e) { return false; } catch (IOException e) { return false; } } public static Map<String,String> getUserInfo(Context context) { File file=new File(context.getFilesDir(),"info.txt"); FileInputStream fis; try { fis = new FileInputStream(file); BufferedReader br=new BufferedReader(new InputStreamReader(fis)); String[] info=br.readLine().split("##"); HashMap<String, String> map=new HashMap<String, String>(); map.put("username", info[0]); map.put("password", info[1]); return map; } catch (Exception e) { return null; } } }
二、外部SDCARD(包含利用SharedReference存儲信息) android
package com.pas.loginservice; import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStreamReader; import java.util.HashMap; import java.util.Map; import android.content.Context; import android.content.SharedPreferences; import android.content.SharedPreferences.Editor; import android.os.Environment; import android.os.StatFs; import android.widget.Toast; import android.text.format.Formatter; public class LoginService { /** * 使用sharedPreferences存儲數據 * @param context * @param username * @param password */ public static void saveUserInfoToPF(Context context, String username, String password) { SharedPreferences SP=context.getSharedPreferences("config", Context.MODE_PRIVATE); Editor ed=SP.edit(); ed.putString("username", username); ed.putString("password",password); ed.commit(); Toast.makeText(context, "保存信息成功", Toast.LENGTH_SHORT).show(); } public static Map<String, String> getUserInfoToPF(Context context) { SharedPreferences SP=context.getSharedPreferences("config", Context.MODE_PRIVATE); HashMap<String, String> map = new HashMap<String, String>(); map.put("username", SP.getString("username",null)); map.put("password", SP.getString("password",null)); return map; } public static boolean saveUserInfo(Context context, String username, String password) { if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) { File file = new File(context.getExternalFilesDir(null), "info.txt"); try { FileOutputStream fos = new FileOutputStream(file); fos.write((username + "##" + password).getBytes()); fos.close(); return true; } catch (FileNotFoundException e) { return false; } catch (IOException e) { return false; } } else { Toast.makeText(context, "SD卡未掛載", Toast.LENGTH_SHORT).show(); return false; } } public static Map<String, String> getUserInfo(Context context) { if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) { File file = new File(context.getExternalFilesDir(null), "info.txt"); FileInputStream fis; try { fis = new FileInputStream(file); BufferedReader br = new BufferedReader(new InputStreamReader(fis)); String[] info = br.readLine().split("##"); HashMap<String, String> map = new HashMap<String, String>(); map.put("username", info[0]); map.put("password", info[1]); return map; } catch (Exception e) { return null; } } else { Toast.makeText(context, "SD卡未掛載", Toast.LENGTH_SHORT).show(); return null; } } public static void getSDCardSize(Context context) { StatFs stat=new StatFs(Environment.getExternalStorageDirectory().getPath()); long blocksize=stat.getBlockSize(); long blockcount=stat.getBlockCount(); long availableblockcount=stat.getAvailableBlocks(); long sdsize=blockcount* blocksize; long avail_sdsize=availableblockcount*blocksize; String totalSize=Formatter.formatFileSize(context, sdsize); String availSize=Formatter.formatFileSize(context, avail_sdsize); Toast.makeText(context, "總共"+totalSize+"MB,可用"+availSize+"MB", 0).show(); } }