Android的:不能写入到一个txt文件 [英] Android: can not write to a txt file

查看:307
本文介绍了Android的:不能写入到一个txt文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的程序中包含名为size.txt,它包含只是一个字15的文件。如果用户选择从微调的值,比如17,那么选择的值应该存储在文件中并替换15。

我已经添加了许可程序

 <使用许可权的android:NAME =android.permission.WRITE_EXTERNAL_STORAG​​E>< /使用许可权>

但不知何故,我不能做出选择的值写入文件。这里是我的code

  {尝试
    FOS的FileOutputStream = openFileOutput(大小,Context.MODE_PRIVATE);
    OutputStreamWriter OSW =新OutputStreamWriter(FOS);
    osw.write(17);
    osw.flush();
    osw.close();
}赶上(java.io.IOException异常五){
//如果发生IOException做一些事情。
}

有人能帮助我吗?感谢youvery了!


解决方案

也许你应该寻找一种方式来解决您的code前阅读本。

根据共享preferences |由西Geetha M N 的Andr​​oid开发者教程(第13部分),


  

许多应用可以提供一种方式来捕捉用户preferences
  一个特定的应用程序或活动的设置。对于支持
  对此,Android提供了一个简单的API。


  
  

preferences是典型的名称值对。它们可以被存储为
  在应用程序中(注意共享preferences在各种活动
  目前,它不能跨进程共享)。或者它可以是
  东西,需要被存储特定于活动



  1. 共享preferences:共享preferences可以通过所有组件(活动,服务等)关闭应用程序中使用


  2. 活动处理preferences:这些preferences只能与在活动中使用,并且不能由应用程序的其他组件使用


共享preferences:

共享preferences与 getShared preferences 的方法上下文的帮助下管理类。的preferences存储在默认文件(1)也可以指定(2)被用来指preferences一个文件名。

(1)这是在您指定的文件名,你怎么弄实例

 公共静态最后弦乐preF_FILE_NAME =prefFile
   共享preferences preferences = getShared preferences(preF_FILE_NAME,MODE_PRIVATE);

MODE_PRIVATE 为preferences的操作模式。它是默认模式和装置创建的文件将仅调用应用程序进行访问。支持另外两种模式是 MODE_WORLD_READABLE MODE_WORLD_WRITEABLE 。在 MODE_WORLD_READABLE 其他应用程序可以读取创建的文件,但不能修改它。在 MODE_WORLD_WRITEABLE 其他应用程序的情况下也有创建的文件的写权限。

(2)推荐的方法是默认的模式中使用,而不指定文件名

 共享preferences preferences = preferencesManager.getDefaultShared preferences(背景);

最后,一旦你有preferences例如,下面是你怎么能从preferences检索存储的值

  INT存储preference = preferences.getInt(storedInt,0);

要在preference文件的存储值 共享preference.Editor 对象已被使用。 编辑共享preference 类的嵌套接口。

 共享preferences.Editor编辑器= preferences.edit();
editor.putInt(storedInt,存储preference); //值存储
editor.commit();

编辑器还支持像方法删除()清()删除从preference价值该文件。

活动preferences:

共享preferences可以由其他应用程序组件中使用。但是,如果你不需要与其他组件preferences,并希望有活动专用preferences。你可以做到这一点与的get preferences()活动的方法的帮助。在的get preference 方法使用 getShared preferences()方法,活动类的名称为preference文件名。

以下是code得到preferences

 共享preferences preferences = GET preferences(MODE_PRIVATE);
诠释存储preference = preferences.getInt(storedInt,0);

在code存储值也是相同的共享preferences的情况。

 共享preferences preferences = GET preference(MODE_PRIVATE);
共享preferences.Editor编辑器= preferences.edit();
editor.putInt(storedInt,存储preference); //值存储
editor.commit();

您也可以使用其他方法,比如存储数据库中的活动状态。注意的Andr​​oid还含有一种叫机器人。preference 包。该包定义类来实现应用程序preferences UI。

要看到更多的例子检查Android的的数据存储开发商网站上发布。

My program contains a file with the name "size.txt", which contains just a word "15". If a user choose a value from a spinner,say 17, then the chosen value should be stored in the file and replace "15".

I have added the permission to the program

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>

But somehow i can not make the chosen value be written to the file. Here is my code

try {
    FileOutputStream fos = openFileOutput("size", Context.MODE_PRIVATE);
    OutputStreamWriter osw = new OutputStreamWriter(fos);
    osw.write("17");
    osw.flush();
    osw.close();
} catch (java.io.IOException e) {
//do something if an IOException occurs.
}

Can anybody help me? Thank youvery much!

解决方案

Maybe you should read this, before looking for a way to fix your code.

According to Shared Preferences | Android Developer Tutorial (Part 13) by Sai Geetha M N,

Many applications may provide a way to capture user preferences on the settings of a specific application or an activity. For supporting this, Android provides a simple set of APIs.

Preferences are typically name value pairs. They can be stored as "Shared Preferences" across various activities in an application (note currently it cannot be shared across processes). Or it can be something that needs to be stored specific to an activity.

  1. Shared Preferences: The shared preferences can be used by all the components (activities, services etc) off the applications.

  2. Activity handled preferences: These preferences can only be used with in the activity and can not be used by other components of the application.

Shared Preferences:

The shared preferences are managed with the help of getSharedPreferences method of the Context class. The preferences are stored in a default file(1) or you can specify a file name(2) to be used to refer to the preferences.

(1) Here is how you get the instance when you specify the file name

public static final String PREF_FILE_NAME = "PrefFile";
   SharedPreferences preferences = getSharedPreferences(PREF_FILE_NAME, MODE_PRIVATE);

MODE_PRIVATE is the operating mode for the preferences. It is the default mode and means the created file will be accessed by only the calling application. Other two mode supported are MODE_WORLD_READABLE and MODE_WORLD_WRITEABLE. In MODE_WORLD_READABLE other application can read the created file but can not modify it. In case of MODE_WORLD_WRITEABLE other applications also have write permissions for the created file.

(2) The recommended way is to use by the default mode, without specifying the file name

SharedPreferences preferences = PreferencesManager.getDefaultSharedPreferences(context);

Finally, once you have the preferences instance, here is how you can retrieve the stored values from the preferences:

 int storedPreference = preferences.getInt("storedInt", 0);

To store values in the preference file SharedPreference.Editor object has to be used. Editor is the nested interface of the SharedPreference class.

SharedPreferences.Editor editor = preferences.edit();
editor.putInt("storedInt", storedPreference); // value to store
editor.commit();

Editor also support methods like remove() and clear() to delete the preference value from the file.

Activity Preferences:

The shared preferences can be used by other application components. But if you do not need to share the preferences with other components and want to have activities private preferences. You can do that with the help of getPreferences() method of the activity. The getPreference method uses the getSharedPreferences() method with the name of the activity class for the preference file name.

Following is the code to get preferences

SharedPreferences preferences = getPreferences(MODE_PRIVATE);
int storedPreference = preferences.getInt("storedInt", 0);

The code to store values is also same as in case of shared preferences.

SharedPreferences preferences = getPreference(MODE_PRIVATE);
SharedPreferences.Editor editor = preferences.edit();
editor.putInt("storedInt", storedPreference); // value to store
editor.commit();

You can also use other methods like storing the activity state in database. Note Android also contains a package called android.preference. The package defines classes to implement application preferences UI.

To see some more examples check Android's Data Storage post on developers site.

这篇关于Android的:不能写入到一个txt文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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