Android(安卓)将asserts文件夹内文件写入SD卡中
16lz
2022-01-25
参考:
http://www.cnblogs.com/wainiwann/p/3274386.html
#######################################################################
private void copyBigDataToSD(String strOutFileName) { try { String path = PATH + "/" + strOutFileName; InputStream myInput; OutputStream myOutput = new FileOutputStream(path); myInput = this.getAssets().open(strOutFileName); byte[] buffer = new byte[1024]; int length = myInput.read(buffer); while (length > 0) { myOutput.write(buffer, 0, length); length = myInput.read(buffer); } myOutput.flush(); myInput.close(); myOutput.close(); } catch (IOException e) { e.printStackTrace(); } }
//从assets 文件夹中获取文件并读取数据 public void getFromAssets(String fileName){ try { File file = new File(PATH, fileName); if (file.exists()) { //if the file is exist, return file.delete(); } file.createNewFile(); InputStream in = getResources().getAssets().open(fileName); //获取文件的字节数 int lenght = in.available(); //创建byte数组 byte[] buffer = new byte[lenght]; //将文件中的数据读到byte数组中 in.read(buffer); FileOutputStream out = new FileOutputStream(file); out.write(buffer); out.flush(); out.close(); Log.i(TAG, "------------------------------------load ok " + fileName); //result = EncodingUtils.getString(buffer, ENCODING); } catch (Exception e) { e.printStackTrace(); Log.i(TAG, "--------------------------"+e.getMessage()); } }
public void getFromAssets(String fileName){ try { File file = new File(PATH, fileName); if (file.exists()) { //if the file is exist, return file.delete(); } file.createNewFile(); BufferedWriter bw = new BufferedWriter(new FileWriter(file)); InputStreamReader inputReader = new InputStreamReader( getResources().getAssets().open(fileName) ); BufferedReader bufReader = new BufferedReader(inputReader); String line=""; String Result=""; while((line = bufReader.readLine()) != null) { Result = line; Result += "\n"; bw.write(Result); bw.flush(); } bw.close(); inputReader.close(); bufReader.close(); Log.i(TAG, "------------------------------------load ok "+fileName); } catch (Exception e) { e.printStackTrace(); Log.i(TAG, "--------------------------"+e.getMessage()); } // return "can not access assets"; }
更多相关文章
- Android(安卓)APK反编译详解(附图)
- Android与(Servlet)服务器交互
- Android(安卓)系统操作
- Android(安卓)ProtoBuf
- Android(安卓)Network数据存储
- mybatisplus的坑 insert标签insert into select无参数问题的解决
- python起点网月票榜字体反爬案例
- NPM 和webpack 的基础使用
- 【阿里云镜像】使用阿里巴巴DNS镜像源——DNS配置教程