【问题标题】:Java Android Cant seem to update text file using FileOutputStreamJava Android 似乎无法使用 FileOutputStream 更新文本文件
【发布时间】:2021-05-01 10:34:16
【问题描述】:

我是 Android Java 开发的初学者,但我在 Java 方面有几年的学校和大学经验。 我正在尝试使用 FileOutputStream 写入我的应用程序的资产文件夹中的文本文件,但它似乎根本没有写入它,因为我使用 InputStream 之后读取文件并且没有任何更新。 我可以使用 inputstream 从同一个文件中读取数据,但无法使用 outputteam 写入文件。 这是我的代码

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);
}

文本文件中没有任何内容,它只是输出

I/System.out: Tesing output 

非常感谢您的帮助

【问题讨论】:

  • 你是在txt文件还是pdf文件中读写?
  • 我尝试写入一个 txt,但也没有成功,所以我只尝试了一个没有结果的常规文件

标签: java android fileoutputstream


【解决方案1】:

您的问题是因为您在不同的文件上写入并在不同的文件上读取。 openFileOut() 将根据上下文创建一个私有文件。 getAssets.open() 将在您的应用程序的资产文件夹中为您提供一个文件。 我想你想要的是InputStream is = openFileInput("Questions");

编辑

这里是FileInputStreamFileOutputStream 的示例。

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) {}
}   

不要忘记在 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"/>
...

【讨论】:

  • 有没有办法使用 InputStream 写入文件?我可以使用 inputstream 从同一个文件中读取数据,但无法使用 outputteam 写入文件。
  • Is there any way to write to the file using InputStream?。当然不是。 InputStream 用于读取,OutputStream 用于写入。好吧,我会用FileInputStreamFileOutputStream 的例子来更新我的答案。
  • 重读您的评论后,我认为您想写入应用程序资产文件夹中的文件,但不幸的是您不能。没有人可以。但是,您可以写入本地数据存储甚至内部存储,只需确保在应用的 msnifest 中设置 wtite 权限。
猜你喜欢
  • 1970-01-01
  • 2020-04-25
  • 1970-01-01
  • 2013-03-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-02-22
  • 1970-01-01
相关资源
最近更新 更多