随手记的片段代码

来源:互联网 发布:联系打字速度软件 编辑:程序博客网 时间:2024/05/22 03:35

getDir(“wojiuhsi”,0);//在android中获取目录的一个有效快捷方式 在data/data/包名/files/目录下新建目录wojiushi

try {
//直接会在data/data/com.shiwenjie.myandroid/files/缪录下建立d.txt 文件
FileOutputStream fileOutputStream = openFileOutput(“d.txt”, 0);
fileOutputStream.write(“我就是”.getBytes());
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
//文件代表目录 获取该目录下空间
long totalSpace = Environment.getExternalStorageDirectory().getTotalSpace();
String s = Formatter.formatFileSize(context, totalSpace);
System.out.println(s);

    long usableSpace = Environment.getExternalStorageDirectory().getUsableSpace();    if (usableSpace<1024*1024*200){    }    String use = Formatter.formatFileSize(context, usableSpace);    Toast.makeText(context,use,Toast.LENGTH_LONG).show();

随手记工具类

public class SPutils {

private static SharedPreferences userInfo;private static SharedPreferences.Editor edit;{}//存储字符串public static void putString(Context context, String nameKey, String nameValue) {    userInfo = context.getSharedPreferences("UserInfo", 0);    edit = userInfo.edit();    edit.putString(nameKey, nameValue);    edit.commit();}public static String getString( Context context,String nameKey) {    userInfo = context.getSharedPreferences("UserInfo", 0);    String aNull = userInfo.getString(nameKey, "");    return aNull;}//存储布尔值public static void putBoolean( Context context,String isTrueKey, boolean isTrueValue) {    userInfo = context.getSharedPreferences("UserInfo", 0);    edit = userInfo.edit();    edit.putBoolean(isTrueKey, isTrueValue);    edit.commit();}public static boolean getBoolean(Context context ,String isTrueKey) {    userInfo = context.getSharedPreferences("UserInfo", 0);    boolean aBoolean = userInfo.getBoolean(isTrueKey, false);    return aBoolean;}

}

获取用户信息

private String getUserInfo() {
file = new File(getFilesDir() + File.separator + “userInfo.txt”);
try {
if (!file.exists()) {
file.createNewFile();
}

    } catch (IOException e) {        e.printStackTrace();    }    try {        FileInputStream fileInputStream = new FileInputStream(file);        BufferedReader buff=new BufferedReader(new InputStreamReader(fileInputStream));        String s = buff.readLine();        System.out.println(s);        fileInputStream.close();        usetinfo=s;    } catch (FileNotFoundException e) {        e.printStackTrace();    } catch (IOException e) {        e.printStackTrace();    }    return usetinfo;}private void saveUserInfo(String trim, String trim1) {    file = new File(getFilesDir() + File.separator + "userInfo.txt");    try {        if (!file.exists()) {            file.createNewFile();        }    } catch (IOException e) {        e.printStackTrace();    }    try {        FileOutputStream fileOutputStream = new FileOutputStream(file);        fileOutputStream.write((trim + "##" + trim1).getBytes());        fileOutputStream.close();    } catch (FileNotFoundException e) {        e.printStackTrace();    } catch (IOException e) {        e.printStackTrace();    }}
0 0
原创粉丝点击