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();
}
}
總結
以上是生活随笔為你收集整理的android 大文本存储,Android操作文件存储信息 利用SharedReference存储信息(获取SDCARD大小)...的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: c语言编写队列元素逆置,数据结构与算法实
- 下一篇: Flash如何制作漂亮的三维水纹特效字体