Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
170 views
in Technique[技术] by (71.8m points)

Java Android Cant seem to update text file using FileOutputStream

I am a beginner at Android Java development but I have a few years of school + uni experience in Java. I am trying to write to a text file in an assets folder in my app using FileOutputStream but it doesn't seem to write to it at all since I am using InputStream to read the file after and there haven't any updates. I am able to read from the same file using inputstream, I just cant write to the file using outputsteam. Here is my code

private void updateTextFile(String update) {
    FileOutputStream fos = null;

    try
    {
        fos = openFileOutput("Questions",MODE_PRIVATE);
        fos.write("Testing".getBytes());
    } 
    catch (FileNotFoundException e) 
    {
        e.printStackTrace();
    } 
    catch (IOException e) 
    {
        e.printStackTrace();
    } 
    finally 
    {
        if(fos!=null)
        {
            try 
            {
                fos.close();
            } 
            catch (IOException e) 
            {
                e.printStackTrace();
            }
        }
    }

    String text = "";

    try
    {
        InputStream is = getAssets().open("Questions");
        int size = is.available();
        byte[] buffer = new byte[size];
        is.read(buffer);
        is.close();
        text = new String(buffer);
    } 
    catch (IOException e) 
    {
        e.printStackTrace();
    }
    System.out.println("Tesing output " + text);
}

There is nothing in the text file and it just outputs

I/System.out: Tesing output 

Help would be greatly appreciated

question from:https://stackoverflow.com/questions/65921735/java-android-cant-seem-to-update-text-file-using-fileoutputstream

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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"/>
...

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...