Read and write file | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Read and write file

How to advice file path on android complier? Following gave error results only. package test; import java.io.File; import java.io.FileNotFoundException; import java.util.Formatter; import java.util.Scanner; public class Testing { public static void main(String[] args) { try { File x = new File("Path:\\sdcard\\other.txt"); Scanner sc = new Scanner(x); while(sc.hasNext()) { System.out.println(sc.next()); } sc.close(); } catch (FileNotFoundException e) { System.out.println("File not found\nError"); } try { Formatter f = new Formatter("storage/emulated/O/test.txt"); f.close(); } catch (Exception e) { System.out.println("Error"); } }

12th May 2020, 5:16 AM
rjstha
rjstha - avatar
1 Answer
+ 2
You can try this: File x = new File("/storage/sdcard/other.txt"); If it doesn't work, check all the answers on this SO link: https://stackoverflow.com/questions/5858107/how-to-get-file-path-from-sd-card-in-android Because the folder name that points to the storage, can vary in each android device.
12th May 2020, 7:38 AM
Tibor Santa
Tibor Santa - avatar