【发布时间】: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