1

I'm writing an Android application in which I want to create text files in a particular folder and afterwards I want to read the files from my device. I'm doing this way:

 File sd = Environment.getExternalStorageDirectory(); File f; FileWriter fw = null; String path = sd.getAbsolutePath() + "/Samples/"; f = new File(path+File.separator+"filename.txt"); if (!f.exists()) { f.mkdirs();//Creates the directory named by this file, creating missing parent directories if necessary try { f.createNewFile(); //fw = new FileWriter(f, true); } catch (IOException e) { Log.e("ERROR","Exception while creating file:"+e.toString()); } 

The problem is that in this way I create another folder instead of a text file. What can I do? Thanks

2 Answers 2

2

Instead of:

f.mkdirs(); 

do:

path.mkdirs(); 
Sign up to request clarification or add additional context in comments.

1 Comment

Hi, thanks for your answer and sorry if I don't have answered before. I'm afraid I can't do path.mkdirs() because path is a string, while mkdirs() is a method of File class
0

I found the solution and I want to share it with you:

 File sd = Environment.getExternalStorageDirectory(); File folder; String path = sd.getAbsolutePath() ; folder = new File(path, dirName); if (!folder.exists()){ folder.mkdirs();} try{ File file = new File(folder, fileName+".txt"); file.createNewFile(); } catch (IOException e) { Log.e("ERROR", "Exception while creating file:" + e.toString()); } 

I hope this could help other people having the same problem. Good luck

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.