Your problem is because you write on a different file and read on a different file.
openFileOut()
will create a private file based on context.
getAssets.open()
will get you a file in your app's assets folder.
I think what you want is InputStream is = openFileInput("Questions");
.
EDIT
Here an example of FileInputStream
and FileOutputStream
.
String file = "/storage/emulated/0/test.txt";
OutputStream os = null;
InputStream in = null;
try {
//To write onto a file.
os = new FileOutputStream(new File(file));
os.write("This is a test".getBytes(StandardCharsets.UTF_8));
os.flush();
//To read a file
in = new FileInputStream(new File(file));
byte[] store = new byte[8192];
for(int i; (i=in.read(store, 0, 8192)) != -1; ) {
System.out.print(new String(store, 0, i, StandardCharsets.UTF_8));
}
System.out.println();
} catch(IOException e) {
} finally {
if(os != null) try { os.close(); } catch(Exception ee) {}
if(in != null) try { in.close(); } catch(Exception ee) {}
}
Dont forget set write permission in Manifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.mypackagename.io"
android:versionCode="1"
android:versionName="4.3" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18"/>
<uses-permission android:name="android.permission.VIBRATE"/>
<uses-permission
android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WAKE_LOCK"/>
...
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…