1、机身内存
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 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 map=new HashMap();
map.put("username", info[0]);
map.put("password", info[1]);
return map;
} catch (Exception e)
{
return null;
}
}
}
2、外部SDCARD(包含利用SharedReference存储信息)
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 getUserInfoToPF(Context context)
{
SharedPreferences SP=context.getSharedPreferences("config", Context.MODE_PRIVATE);
HashMap map = new HashMap();
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 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 map = new HashMap();
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();
}
}