您好,登錄后才能下訂單哦!
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 將字符串寫入本地的操作方法,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對億速云網站的支持!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。