Android studio 将字符串写入本地的操作方法
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 将字符串写入本地的操作方法,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对我们网站的支持!
赞 (0)