覆盖文本文件编程 [英] Overwrite text file programmatically

查看:63
本文介绍了覆盖文本文件编程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图从覆盖android应用一个文本文件,我做了什么却又是,

I'm trying to overwrite a text file from android application, what I have done yet is,

我一直在使用创建一个文本文件中的一个活动:

I have created one text file in one activity using:

FileOutputStream create_file = null;
OutputStreamWriter osw = null;
create_file = openFileOutput("filename.txt", Context.MODE_WORLD_WRITEABLE);
osw = new OutputStreamWriter(create_file);
osw.write("text goes here");
osw.close();
create_file.close();

和我已经打开,在另一个活动文件中使用逐行读取内容行:

and I have opened that file in another activity read the contents line by line using:

FileInputStream open_file = openFileInput("filename.txt");
InputStreamReader isr = new InputStreamReader(open_file);
BufferedReader inRd = new BufferedReader(isr);
while ((getText = inRd.readLine()) != null)
{
    Toast.makeText(getApplicationContext(), getText, Toast.LENGTH_SHORT).show();
}

通过这个我已经验证的内容是否存储与否,并确保该文件与内容存在,但是当我尝试使用另一个活动覆盖该文件:

through this I have verified whether the content is stored or not, and made sure that the file exist with the content, but when I try to overwrite that file from another activity using:

FileOutputStream create_file = null;
OutputStreamWriter osw = null;
create_file = new FileOutputStream(new File(PasswordUtil.pswrd_file), false);
osw = new OutputStreamWriter(create_file);
osw.write(getString);

我得到一个例外,

I'm getting one exception,

java.io.FileNotFoundException:/
  FILENAME.TXT(只读文件系统)

注:文本文件存储在内部存储

Note: The text file is stored in Internal storage.

任何帮助,先谢谢了。

推荐答案

我发现,之所以 FileNotFoundException异常,其原因不是下面一行:

I found the reason for that FileNotFoundException, its because instead of the following line:

create_file = new FileOutputStream(new File("filename.txt", false));

我们需要指定该文件的路径,对我来说,我已经存储在它的内部存储,我给了路径

we have to specify the path for that file, for me, I have stored it in internal storage and I gave the path

/数据/数据​​/<包装,名称> /文件/+FILENAME.TXT

和我已经改变了覆盖编码这个样子,

and I've changed the overwrite coding like this,

FileOutputStream overWrite = new FileOutputStream("/data/data/<Package-Name/files/" + "filename.txt", false);
overWrite.write(getString.getBytes());
overWrite.flush();
overWrite.close();

现在一切都运作良好。

这篇关于覆盖文本文件编程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆