6 写文件到SD卡

来源:互联网 发布:淘宝买家等级查询 编辑:程序博客网 时间:2024/04/29 21:05

权限 <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>


package com.itheima27.qqlogin.utils;


import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.InputStreamReader;


import android.content.Context;
import android.os.Environment;


public class UtilsOfSDCard {


/**
* 保存用户信息
* @param number
* @param password
*/
public static boolean saveUserInfo(Context context, String number, String password) {
try {


// 判断sdcard是否可用
if(!Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) {
// 如果sd卡的状态等于mounted, 就可用
return false;
}


// /mnt/sdcard/itheima27.txt

File file = new File(Environment.getExternalStorageDirectory(), "itheima27");

FileOutputStream fos = new FileOutputStream(file);

String text = number + "##" + password;// 123##abc  ##

fos.write(text.getBytes());// 写入

fos.flush();
fos.close();
return true;
} catch (Exception e) {
e.printStackTrace();
}
return false;
}

/**
* 获取用户信息
* @return
*/
public static String getUserInfo(Context context) {
try {

// 判断sdcard是否可用
if(!Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) {
// 如果sd卡的状态等于mounted, 就可用
return null;
}


File file = new File(Environment.getExternalStorageDirectory(), "itheima27.txt");

// 字节流
FileInputStream fis = new FileInputStream(file);

// 转换流
InputStreamReader isr = new InputStreamReader(fis);

// 字符流
BufferedReader br = new BufferedReader(isr);

String result = br.readLine();

br.close();
isr.close();
fis.close();
return result;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}

0 0
原创粉丝点击