Reputation: 7
When the button is clicked a file should be created. But in my code file is not being created.
bcheck.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
p=1;//app installed
try {
FileOutputStream fout = new FileOutputStream("abc.txt");
String s = "Installed";
byte b[]=s.getBytes();
fout.write(s.getBytes());
fout.close();
System.out.println("FILE CREATED");
}
catch (Exception e)
{
System.out.println("FILE NOT CREATED");
}
}
});
Upvotes: 0
Views: 4453
Reputation: 1006724
FileOutputStream fout = new FileOutputStream("abc.txt");
That will not work on Android. Always use a method to get at a valid directory for which you can read and write.
For example, this would work:
FileOutputStream fout = new FileOutputStream(new File(getFilesDir(), "abc.txt"));
(assuming that this line is in a method in some subclass of Context
, like an Activity
or Service
)
getFilesDir()
returns to you a directory in internal storage. You can also use external storage, if you prefer, though that can get more complicated due to permissions and such.
Upvotes: 1