This will create a folder called TestFolder in /storage/emulated/0/ and write
to a text file called MyTest.txt in that folder.
File newFolder;//path to folder File file;//path to file public void writeData() { createFolderIfNoneExists(); FileWriter writer = null; try { writer = new FileWriter(file); // Writes the content to the file writer.write("This\n is\n an\n example\n"); writer.flush(); writer.close(); } catch (IOException e) { e.printStackTrace(); } } public void createFolderIfNoneExists(){ try { newFolder = new File(Environment.getExternalStorageDirectory(), "TestFolder"); if (!newFolder.exists()) { newFolder.mkdir(); } try { file = new File(newFolder, "MyTest" + ".txt"); file.createNewFile(); } catch (Exception ex) { Log.i("TAG", "Exception: " + ex); } } catch (Exception e) { Log.i("TAG", "Exception 2: " + e); } }
To view the contents of the file you will need a reference to the folders path (The ‘folder’ variable in the above code) and the name of the file.
then use this code:
Retrieve file from SD card