日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > Android >内容正文

Android

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

發布時間:2023/12/19 Android 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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大小)...的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。