1

I am making a android app in which i am going to use File handling but to do that first i need to create it but by using following code:

File logf = new File("log1.txt"); Boolean bb = logf.exists(); if(!bb) try { bb = logf.createNewFile(); } catch (IOException e) { msg.setText("not able to create file."); } 

'msg' is the TextView object which i am using to display error, and when i run this app.. it goes into IOException e catch.. Please tell me if i am doing something wrong or what?.. besides i am using Android 2.2 for my app.

5
  • What's the logcat telling you? Commented May 20, 2017 at 6:41
  • Share manifest? Commented May 20, 2017 at 6:43
  • as my previous textview was empty and when i run this.. then it changed to "not able to create file.".. that means its not creating a file and its going into catch Commented May 20, 2017 at 6:49
  • WRITE_EXTERNAL_STORAGE permission in your manifest file Commented May 20, 2017 at 7:06
  • WRITE_EXTERNAL_STORAGE was necessary if i was using external storage as i am only using internal storage which private for my app. Commented May 20, 2017 at 7:08

2 Answers 2

1

In app storage, please try this:

File file = new File(getFilesDir(), "file.txt"); 

Or if you want to append file name, try this:

File file = new File(getFilesDir() + File.separator + "file.txt"); 
Sign up to request clarification or add additional context in comments.

2 Comments

don't i have to use file separator between them?
No need, please see public File(File parent, String child)
1

you need to provide absolute path of file like below.

File file = new File(/data/packagename/files/ + "log1.txt"); 

2 Comments

but i am not using external storage, i want to use internal storage
rust fisher's was easy.. but yours is necessary when we have to share with apps

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.