能够把文件直接保存在设备的内部存储器上,默认情况下,保存在内部存储器上的文件是应用程序的私有数据,其他应用程序(或用户)不能够访问它们。当用户卸载应用程序时,这些文件也会被删除。
以下是在内部存储器中创建并写入一个私有文件的方法:
1.调用openFileOutput方法,这个方法需要指定文件的名称和操作模式。它会返回一个
FileOutputStream对象。
2.用FileOutputStream对象的write()方法把数据写入文件;3.用FileOutputStream对象的close()方法关闭输出流。
例如:
String FILENAME = "hello_file"; String string = "hello world!";
FileOutputStream fos = openFileOutput(FILENAME, Context.MODE_PRIVATE);
fos.write(string.getBytes());
fos.close();
MODE_PRIVATE参数指示要创建这个文件(或者,如果有同名文件存在,则会替换旧文件),并且让这个文件是应用程序的私有文件。其他可用的模式包括:MODE_APPEND、MODE_WORLD_READABLE和MODE_WORLD_WRITEABLE。
以下是从内部存储中读取一个文件的方法:
1.调用openFileInput()方法,把要读取的文件名传递给这个方法。这个方法会返回一个
FileInputStream对象。
2.用FileInputStream对象的read()方法从文件中读取字节。
3.用FileInputStream对象的close()方法关闭输入流。
提示:如果在编译时要把一个静态文件保存到应用程序中,那么要把这个文件保存在工程的res/raw/目录中。
能够用openRawResource()方法打开这个文件,这个方法需要把R.raw.<filename>的资源ID传递给它。这个方法会返回一个用于读取文件的InputStream对象(但是,不能对这个文件进行写入的操作)。
保存缓存文件
如果只是要缓存一些数据,而不是要持久的保存它,那么应该使用getCacheDir()方法来打开一个File对象,
它代表了应用程序要保存临时缓存文件的内部目录。
当设备的内部存储空间不足的时候,Android可能会删除这些缓存文件来回收存储空间。
但是,不应该依赖系统来给你清理这些文件,应该始终自己来维护缓存文件,并且要把存储空间的耗费限定在合理的范围内,
如1MB。当用户卸载应用程序时,这些文件会被删除。
其他有用的方法
getFileDir()
获取内部文件被保存的文件系统目录的绝对路径。
getDir()
在内部存储空间中创建(或打开既存的)目录。
deleteFile()
删除保存在内部存储空间上的文件。
fileList()
分享到:
相关推荐
概念2、 具体操作ExternalActivity(写入外部存储文件)ExternalActivity(读取外部存储文件)3、 动态权限(6.0以上)4、 获取外部存储的目录三、内部存储InternalStorage1、概念2、获取内存存储的目录3、代码演示I
android-storage Library to create, read, delete, append, encrypt files and more, on internal or external disk spaces with a really simple API. Latest Release dependencies { compile '...
所有Android设备都有两个文件存储区域:内部存储空间(internal Storage)和外部存储空间(external Storage)。这些名称是在Android早期确定的,那时候大部分设备都提供内置的非易失性内存(内部存储空间)以及可...
android sdk -26 带 com.android.internal.telephony 包的 jar, 下载后 将名称改为 android.jar 替换 anroid-sdk -> platform -> android-26 文件下的 android.jar 即可直接找到 telephony 下的类, 不会报错
Android Material FlowLayout简单使用
Paper is a fast NoSQL data storage for Android that lets you save/restore Java/Kotlin objects using efficient Kryo serialization. Class structure changes handled automatically. What's new in 2.1 Get...
Android存储空间包括内部存储空间(Internal Storage)和外部存储空间(External Storage),本文分别对Android内部存储空间(Internal Storage)和Android外部存储空间(External Storage)做了详细讲解
InternalStorage_edu-
手头最有价值的Android资料。包括平台架构和application framework启动过程的描述.
包括android内幕以及架构非常不错的资料
This jar enables to use Android's internal/hidden APIs in development. What is Android internal and hidden APIs? The internal API is located in the com.android.internal package which is available in ...
The log file is written to the tools, the priority SD card -> external memory -> internal storage, in order to automatically select the three Instructions: 1: Initialization FileLogUtils.init (Context...
android.jar的internal与hideApi 隐藏API
an android library for debugging what we care about directly in app. ...Pandora Pandora is a tool box that allows you...View the internal storage system of own app; View all databases, and support ADD
可以让你的手机变得瞬间畅通,它会及时优化网页相关数据,有着接入式的操作体验数据,各种类型的应用程序可享受,提高使用感受。本次带来的是安卓最新版,如果你的手机上缺少这个软件,那么就可以来本站下载。 软件...
解决 500 Internal Server Error 返回数据过大导致的问题
android system webview 适配系统>=5.0
在内部存储区 (internal) 存放文件1.3. 在外部存储区 (external) 存放文件2. 使用共享首选项(Shared Preferences)3. 序列化3.1. XML序列化3.2. XML解析4. SQLite数据库4.1. SQLite的命令行操作4.2. 在SQL数据库中...
Android Contains Sample Projects for Android Developement(Basics) RedirectOTP Lets the user query the SMS database ...The APK(s) in the Internal Storage can then be accessed using normal user access.