Androidstudio将字符串写入本地的操作方法

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

Androidstudio将字符串写⼊本地的操作⽅法
File 类的操作:
1.⾸先需要添加相关权限:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
注意6.0以上需要动态申请:
private void checkPermission(){
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {//6.0以上
int permission = ActivityCompat.checkSelfPermission(getApplication(), Manifest.permission.RECEIVE_SMS);
int permission1 = ActivityCompat.checkSelfPermission(getApplication(), Manifest.permission.WRITE_EXTERNAL_STORAGE);
if(permission != PackageManager.PERMISSION_GRANTED && permission1 != PackageManager.PERMISSION_GRANTED) {
Log.e(TAG,"没有获取权限,请申请");
// 申请⼀个(或多个)权限,并提供⽤于回调返回的获取码(⽤户定义)
if (ActivityCompat.shouldShowRequestPermissionRationale(this,
Manifest.permission.RECEIVE_SMS) && ActivityCompat.shouldShowRequestPermissionRationale(this,
Manifest.permission.WRITE_EXTERNAL_STORAGE)) {//这⾥可以写个对话框之类的项向⽤户解释为什么要申请权限,并在对话框的确认键后续再次申请权限 Log.e(TAG,"提⽰");
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.RECEIVE_SMS,Manifest.permission.WRITE_EXTERNAL_STORAGE}, CODE_READ_SMS);
} else {
//申请权限,字符串数组内是⼀个或多个要申请的权限,1是申请权限结果的返回参数,在onRequestPermissionsResult可以得知申请结果
Log.e(TAG,"您已禁⽌");
Toast.makeText(MainActivity.this,"没有获取读取⼿机权限,请到应⽤中⼼⼿动打开该权限",Toast.LENGTH_SHORT).show();
}
}else{
Log.e(TAG,"获取到了权限");
}
}else{
Log.e(TAG,"获取到了权限");
}
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if(requestCode == CODE_READ_SMS){
if(grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Log.e(TAG,"获取到了权限");
} else{
// 没有获取到权限,做特殊处理
Log.e(TAG,"没有获取到权限");
Toast.makeText(MainActivity.this,"没有获取读取⼿机权限,请到应⽤中⼼⼿动打开该权限",Toast.LENGTH_SHORT).show();
}
}
}
2.saveToFile
a)BufferedWriter 追加
//保存⽂件到sd卡
public void saveToFile(String content) {
BufferedWriter out = null;
//获取SD卡状态
String state = Environment.getExternalStorageState();
//判断SD卡是否就绪
if (!state.equals(Environment.MEDIA_MOUNTED)) {
Toast.makeText(this, "请检查SD卡", Toast.LENGTH_SHORT).show();
return;
}
//取得SD卡根⽬录
File file = Environment.getExternalStorageDirectory();
try {
Log.e(TAG, "======SD卡根⽬录:" + file.getCanonicalPath());
if(file.exists()){
LOG.e(TAG, "file.getCanonicalPath() == " + file.getCanonicalPath());
}
/*
输出流的构造参数1:可以是File对象也可以是⽂件路径
输出流的构造参数2:默认为False=>覆盖内容; true=>追加内容
*/
out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file.getCanonicalPath() + "/readMsg.txt",true)));
out.newLine();
out.write(content);
Toast.makeText(this, "保存成功", Toast.LENGTH_SHORT).show();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (out != null) {
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
b)FileWriter追加
/**
* 使⽤FileWriter进⾏⽂本内容的追加
* @param file
* @param content
*/
private void addTxtToFileWrite(File file, String content){
FileWriter writer = null;
try {
//FileWriter(file, true),第⼆个参数为true是追加内容,false是覆盖
writer = new FileWriter(file, true);
writer.write("\r\n");//换⾏
writer.write(content);
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if(writer != null){
writer.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
3.readFromFile()
//从SD卡读取⽂件
public String readFromFile() {
//读的时候要⽤字符流万⼀⾥⾯有中⽂
BufferedReader reader = null;
FileInputStream fis;
StringBuilder sbd = new StringBuilder();
String state = Environment.getExternalStorageState();
if (!state.equals(Environment.MEDIA_MOUNTED)) {
Toast.makeText(this, "SD卡未就绪", Toast.LENGTH_SHORT).show(); return "";
}
File root = Environment.getExternalStorageDirectory();
try {
fis = new FileInputStream(root + "/readMsg.txt");
reader = new BufferedReader(new InputStreamReader(fis));
String row;
while ((row = reader.readLine()) != null) {
sbd.append(row);
}
} catch (FileNotFoundException e) {
Toast.makeText(this, "⽂件不存在", Toast.LENGTH_SHORT).show(); //e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return sbd.toString();
}
4.removeFromFile()
//删除SD卡⽂件
public void removeFromFile() {
String state = Environment.getExternalStorageState();
if (!state.equals(Environment.MEDIA_MOUNTED)) {
Toast.makeText(this, "SD卡未就绪", Toast.LENGTH_SHORT).show();
return;
}
//取得SD卡根⽬录
File root = Environment.getExternalStorageDirectory();
File myFile=new File(root+"/sd.txt");
//File myFile=new File(root,"sd.txt");
if (myFile.exists()) {
myFile.delete();
Toast.makeText(this,"⽂件已删除",Toast.LENGTH_SHORT).show();
}else {
Toast.makeText(this,"⽂件不存在",Toast.LENGTH_SHORT).show();
}
}
}
总结
以上所述是⼩编给⼤家介绍的Android studio 将字符串写⼊本地的操作⽅法,希望对⼤家有所帮助,如果⼤家有任何疑问请给我留⾔,⼩编会及时回复⼤家的。

在此也⾮常感谢⼤家对⽹站的⽀持!。

相关文档
最新文档