android保存资源文件File的方法

来源:互联网 发布:感情炮 知乎 编辑:程序博客网 时间:2024/05/17 13:46


  
  在Android上面没有的File就是J2se中的纯种File了,可见功能之强大,这里就算是走马观花地严重路过了。
//创建文件

            file = new File(FILE_PATH , FILE_NAME);

            file.createNewFile();

         

            //打开文件file的OutputStream

            out = new FileOutputStream(file);

            String infoToWrite = "纸上得来终觉浅,绝知此事要躬行";

            //将字符串转换成byte数组写入文件

            out.write(infoToWrite.getBytes());

            //关闭文件file的OutputStream

            out.close();

         

            //打开文件file的InputStream

            in = new FileInputStream(file);

            //将文件内容全部读入到byte数组

            int length = (int)file.length();

            byte[] temp = new byte[length];

            in.read(temp, 0, length);

            //将byte数组用UTF-8编码并存入display字符串中

            display =  EncodingUtils.getString(temp,TEXT_ENCODING);

            //关闭文件file的InputStream

            in.close();

        } catch (IOException e) {

            //将出错信息打印到Logcat

            Log.e(TAG, e.toString());

            this.finish();

        }

 

//从资源读取

 

InputStream is=getResources().getRawResource(R.raw.文件名)

0 0
原创粉丝点击