Reputation: 75
I am new to android. By the following code, I am able to successfully create the files in the emulator, but when i run this on my device i get a file not found exception.
String l = pwd;
FileOutputStream fos, fos2, fos3;
// Log.d("spt", pwd);
File registrationinfo = new File(
"data/data/com.example.spotter/files/pwd.txt");
File registrationinfo2 = new File(
"data/data/com.example.spotter/files/uname.txt");
File registrationinfo3 = new File(
"data/data/com.example.spotter/files/sph.txt");
fos = new FileOutputStream(registrationinfo);
fos2 = new FileOutputStream(registrationinfo2);
fos3 = new FileOutputStream(registrationinfo3);
byte buf[] = l.getBytes();
for (int i = 0; i < buf.length; i++) {
fos.write(buf[i]);
}
fos.close();
byte buf2[] = uname.getBytes();
for (int i = 0; i < buf2.length; i++) {
fos2.write(buf2[i]);
}
fos2.close();
byte buf3[] = sph.getBytes();
for (int i = 0; i < buf3.length; i++) {
fos3.write(buf3[i]);
}
This is my logcat when i run this code on my phone:
05-12 13:06:11.425: W/System.err(19066): java.io.FileNotFoundException: /data/data/com.example.spotter/files/pwd.txt (No such file or directory)
05-12 13:06:11.425: W/System.err(19066): at org.apache.harmony.luni.platform.OSFileSystem.open(Native Method)
05-12 13:06:11.425: W/System.err(19066): at dalvik.system.BlockGuard$WrappedFileSystem.open(BlockGuard.java:232)
05-12 13:06:11.425: W/System.err(19066): at java.io.FileOutputStream.<init>(FileOutputStream.java:94)
05-12 13:06:11.425: W/System.err(19066): at java.io.FileOutputStream.<init>(FileOutputStream.java:66)
05-12 13:06:11.425: W/System.err(19066): at com.example.spotter.Register.onClick(Register.java:114)
05-12 13:06:11.435: W/System.err(19066): at android.view.View.performClick(View.java:2533)
05-12 13:06:11.435: W/System.err(19066): at android.view.View$PerformClick.run(View.java:9320)
05-12 13:06:11.435: W/System.err(19066): at android.os.Handler.handleCallback(Handler.java:587)
05-12 13:06:11.435: W/System.err(19066): at android.os.Handler.dispatchMessage(Handler.java:92)
05-12 13:06:11.435: W/System.err(19066): at android.os.Looper.loop(Looper.java:150)
05-12 13:06:11.435: W/System.err(19066): at android.app.ActivityThread.main(ActivityThread.java:4389)
05-12 13:06:11.435: W/System.err(19066): at java.lang.reflect.Method.invokeNative(Native Method)
05-12 13:06:11.485: W/System.err(19066): at java.lang.reflect.Method.invoke(Method.java:507)
05-12 13:06:11.485: W/System.err(19066): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:849)
05-12 13:06:11.485: W/System.err(19066): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:607)
05-12 13:06:11.485: W/System.err(19066): at dalvik.system.NativeStart.main(Native Method)
Upvotes: 0
Views: 716
Reputation: 9590
I think you need to use
FileOutputStream fos = openFileOutput("pwd.txt", Context.MODE_APPEND | Context.MODE_WORLD_READABLE);
instead of
File registrationinfo = new File("data/data/com.example.spotter/files/pwd.txt");
to read file data.
From the above code you can use your application's files folder.
Also you can get help from THIS link.
Upvotes: 0
Reputation: 669
I am not sure that you are solve your problem. But you can try add permission in AndroidMenifest.xml
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
Upvotes: 0
Reputation: 6353
You shouldn't use direct call to new File("data/data/com.example.spotter/files/pwd.txt");
Instead of that, call getFilesDir: File registrationinfo = context.getFilesDir("pwd.txt")
Upvotes: 0
Reputation: 10079
Check whether the specified file path exists. If not then create it
File registrationInfoFolder = new File(
"data/data/com.example.spotter/files");
if(!registrationInfoFolder.exists()){
registrationInfoFolder.mkdirs(); // It will create directory's if not exists
}
Then add your file with FileOutputStream. This happened because there is no files on specified path.
Upvotes: 1