android 大文本存储,Android操作文件存储信息 利用SharedReference存储信息(获取SDCARD大小)...

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();

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值