中文字幕在线观看,亚洲а∨天堂久久精品9966,亚洲成a人片在线观看你懂的,亚洲av成人片无码网站,亚洲国产精品无码久久久五月天

android中文件操作

2018-07-20    來源:open-open

容器云強勢上線!快速搭建集群,上萬Linux鏡像隨意使用
android開發(fā)中涉及到的文件操作主要是往sd卡中或是內(nèi)在中讀取文件數(shù)據(jù),大概的操作如下:
public class FileOperator {  
    private Context context; //上下文 
      public FileOperator(Context c) {  
        this.context = c;  
    }  
  
  //首先是個公用的方法:
  private String dealStream(InputStream is) throws IOException {  
        int buffersize = is.available();// 取得輸入流的字節(jié)長度  
        byte buffer[] = new byte[buffersize];  
        is.read(buffer);// 數(shù)據(jù)讀入數(shù)組  
        is.close();// 讀取完畢關(guān)閉流。  
        String result = EncodingUtils.getString(buffer, "UTF-8");// 防止亂碼  
        return result;  
    }  

    // 讀取sd中的存在的文件  
    public String readSDCardFile(String path) throws IOException {  
        File file = new File(path);  
        FileInputStream fis = new FileInputStream(file);  
        String resStr = dealStream(fis);  
        return resStr ;  
    }  
  
    // res目錄下新建raw資源文件夾,只能讀不能寫入  
    public String readRawFile(int fileId) throws IOException {  
        // 取得輸入流  
        InputStream is = context.getResources().openRawResource(fileId);  
        String resultStr = dealStream(is);// 返回一個字符串  
        return resultStr;  
    }  
   
    // 在assets下的文件,只能讀取不能寫入  
    public String readAssetsFile(String filename) throws IOException {  
        // 取得輸入流  
        InputStream is = context.getResources().getAssets().open(filename);  
        String resStr = dealStream(is);// 返回一個字符串  
        return resStr;  
    }  
  
    // 往sd卡中寫入文件  
    public void writeToSDCard(String path, byte[] buffer) throws IOException {  
        File file = new File(path);  
        FileOutputStream fos = new FileOutputStream(file);  
        fos.write(buffer);// 寫入buffer數(shù)組。如果想寫入一些簡單的字符,可以將String.getBytes()再寫入文件;  
        fos.close();  
    }  
  
    // 將文件寫入應(yīng)用的data/data的files目錄下  
    public void writeToDateFile(String fileName, byte[] buffer) throws Exception {  
        byte[] buf = fileName.getBytes("iso8859-1");  
        fileName = new String(buf, "utf-8");  
        FileOutputStream fos = context.openFileOutput(fileName,  
                Context.MODE_APPEND);// 打開模式,有幾種,此處表示添加在文件后面  
        fos.write(buffer);  
        fos.close();  
    }  
  
    // 讀取應(yīng)用的data/data的files目錄下文件數(shù)據(jù)  
    public String readDateFile(String fileName) throws Exception {  
        FileInputStream fis = context.openFileInput(fileName);  
        String resultStr = dealStream(fis);// 返回一個字符串  
        return resultStr;  
    }  
}  

最后我們不要忘記在清單文件AndroidManfiest.xml中加上sd卡操作權(quán)限:
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" /> //在SDCard中創(chuàng)建與刪除文件權(quán)限

標簽: 權(quán)限

版權(quán)申明:本站文章部分自網(wǎng)絡(luò),如有侵權(quán),請聯(lián)系:west999com@outlook.com
特別注意:本站所有轉(zhuǎn)載文章言論不代表本站觀點!
本站所提供的圖片等素材,版權(quán)歸原作者所有,如需使用,請與原作者聯(lián)系。

上一篇:AWK命令快速入門

下一篇:C++ STL Set 快速入門