Added file name.csv to the res / raw folder

You need to copy this file from the resources to the phone / emulator memory. How to do it?

  • So far I’m thinking of opening to read and write to another file, but somehow this is wrong and unnecessary - Alexander Tymchuk

1 answer 1

I did something like this:

Activity resourceProvider; private String fileName = "sprayballs.csv"; InputStream sourceFile; File destFile; public Sprayballs(Activity activity) { this.resourceProvider = activity; this.sourceFile = resourceProvider.getResources().openRawResource(R.raw.sprayballs); this.destFile = new File(Environment.getExternalStorageDirectory().getPath() + "/" + fileName); if(!destFile.exists()) { try { this.copyCSV(); } catch (IOException e) { e.printStackTrace(); } } } public void copyCSV() throws IOException { InputStreamReader isr = new InputStreamReader(sourceFile); BufferedReader reader = new BufferedReader(isr); String line; StringBuilder builder = new StringBuilder(); try { while ((line = reader.readLine()) != null) { builder.append(line + "\n"); } } catch (IOException e) { e.printStackTrace(); } try { sourceFile.close(); } catch (IOException e) { e.printStackTrace(); } try { FileOutputStream fos = new FileOutputStream(destFile); fos.write(builder.toString().getBytes()); fos.close(); } catch (IOException e) { e.printStackTrace(); } }