Android 数据存储之 FileInputStream 工具类及FileInputStream类的使用
作者:mrr 时间:2023-08-13 01:22:07
安卓的三种本地的典型数据存储方式
SharedPreferences
以文件格式保存在本地存储中
SQL数据库
IDE : Android Studio
参考文章:https://www.jb51.net/article/74215.htm
絮叨一下:本来文件操作这一块上周就想把其弄懂,然后继续进一步的学习。但是因为官方的 Android Training 之中的概念太过于繁杂。导致我认为存储到内部之中要查询空间,得到返回之类。结果是因为我把保存在内部空间(data目录下的包名下的file文件夹)与外部空间(存储空间)的概念混淆。所以耽误了大量时间并且不得法。最后还是看到参考文章才知道应该怎么去写。然后自己跟着参考文章过了一遍。
同样的,我采用了分离写法,也就是创建一个工具类,模块化方便使用。希望能帮助到别人,也是给自己建立一种概念。
话不多说,上代码:
import android.content.Context;
import android.util.Log;
import java.io.FileInputStream;
import java.io.FileOutputStream;
/**
* Created by zhizhao on 2015/11/1 0001 in 16:00.
*/
public class UsingFileInputStream {
private Context fileContext;
private String fileName;
private String fileUserName;
private String filePassword;
public UsingFileInputStream(String name, Context context, String userName, String password) {
this.fileName = name;
this.fileContext = context;
this.fileUserName = userName;
this.filePassword = password;
}
//保存的时候是在文件内容中连续写入,也就是在之前保存的数据基础上再次写入。
public void writeFileInputStream() {
try {
FileOutputStream fileOutputStream = fileContext.openFileOutput(fileName,
fileContext.MODE_PRIVATE);
byte[] byteUserName = fileUserName.getBytes();
byte[] bytePassword = filePassword.getBytes();
fileOutputStream.write(byteUserName);
fileOutputStream.write(bytePassword);
Log.v("FileInputStream保存结果 ", "UserName = " + fileUserName + " Password = " + filePassword);
fileOutputStream.flush();
fileOutputStream.close();
} catch (Exception e) {
e.printStackTrace();
}
}
//读取文件是把整个文件的内容进行读取。如果要加入解析,则保存的时候保存为特殊格式。
public void readFileInputStream() {
try {
FileInputStream fileInputStream = fileContext.openFileInput(fileName);
int len = fileInputStream.available();
byte[] buffer = new byte[len];
fileInputStream.read(buffer);
Log.v("读取到的文件为:", ""+new String(buffer));
fileInputStream.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
下面是使用:
private void writeFile(){
UsingFileInputStream fileInputStream = new UsingFileInputStream("account",
MySaveDataActivity.this, userName, userPass);
fileInputStream.writeFileInputStream();
tvReadInformation.setText("保存成功!" + "\n UserName = " + userName + "\n UserPass = " + userPass);
}
private void readFile(){
UsingFileInputStream fileInputStream = new UsingFileInputStream("account",
MySaveDataActivity.this, userName, userPass);
fileInputStream.readFileInputStream();
tvReadInformation.setText("读取成功!"+"\n UserName = "+userName+"\n UserPass = "+userPass);
}
总结一下:
我觉得自己目前写的也不是很对,很完美。因为在调用过程中要反复去填写文件名,传入值。并且在返回值之中很难知道成功失败。
况且我并没有把文件异常捕获并进行操作,因为如果是没有文件的情况下去操作的话,必然会报错:空指针异常。
不过既然是练习也就没考虑那么多,因为这种时候只要在 try{}catch(){} 代码块里面加入自己的操作手段就可以了。
下面还有点时间接着给大家介绍Android FileInputStream类的使用
1.FileInputStream类概述
继承关系:
java.io.FileInputStream->java.io.InputStream->java.lang.Object
实现接口:
Closeable
类的功能:
FileInputStream 从文件系统中的某个文件中获取输入字节。哪些文件可用取决于主机环境。
FileInputStream 用于读取诸如图像数据之类的原始字节流。要读取字符流,请考虑使用 FileReader。
2.类的属性和行为
(1) public void close() throws IOException
功能: 关闭此文件输入流并释放与此流有关的所有系统资源。
如果此流有一个与之关联的通道,则关闭该通道。
指定者:接口 Closeable 中的 close
覆盖: 类 InputStream 中的 close
抛出: IOException - 如果发生 I/O 错误。
(2) public int read() throws IOException
功能: 从此输入流中读取一个数据字节。如果没有输入可用,则此方法将阻塞。
指定者:类 InputStream 中的 read
返回: 下一个数据字节;如果已到达文件末尾,则返回 -1。
抛出: IOException - 如果发生 I/O 错误。
(3) public int read(byte[] b) throws IOException
功能:从此输入流中将最多b.length个字节的数据读入一个字节数组中。在某些输入可用前,此方法将阻塞
覆盖:类 InputStream 中的 read
参数:b - 存储读取数据的缓冲区
返回:读入缓冲区的字节总数,如果因为已经到达文件末尾而没有更多的数据,则返回 -1。
抛出:IOException - 如果发生 I/O 错误。
(4) public int read(byte[] b, int off, int len) throws IOException
功能:从此输入流中将最多len个字节的数据读入一个字节数组中。在某些输入可用之前,此方法将阻塞。
覆盖:类 InputStream 中的 read
参数:b - 存储读取数据的缓冲区。
off - 数据的起始偏移量。
len - 读取的最大字节数。
返回:读入缓冲区的字节总数,如果因为已经到达文件末尾而没有更多的数据,则返回 -1。
抛出:IOException - 如果发生 I/O 错误。
3.常见错误
在eclipse下使用FileInputStream,提示找不到指定文件
代码:
filename = "abc.txt" ;
FileInputStream fis = new FileInputStream(filename);
错误显示:
java.io.FileNotFoundException: dbconfig.properties (系统找不到指定的文件。)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(FileInputStream.java:106)
at java.io.FileInputStream.<init>(FileInputStream.java:66)
解决方法:
因为eclipse下运行main程序时,eclipse会自动将发布目录作为其根目录,所以会提示找不到文件,将filename改为绝对目录即可
filename = "\sdcard\...\abc.txt" ;
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
用C#生成不重复的随机数的代码
Java Base64 加密与解密示例代码
详解Spring Boot + Mybatis 实现动态数据源
![](https://img.aspxhome.com/file/2023/6/76586_0s.png)
java读写二进制文件的解决方法
JVM内存参数配置详解
![](https://img.aspxhome.com/file/2023/7/74987_0s.png)
Flutter系列重学Container示例详解
![](https://img.aspxhome.com/file/2023/2/111742_0s.png)
C#深拷贝方法探究及性能比较(多种深拷贝)
![](https://img.aspxhome.com/file/2023/5/77235_0s.png)
java泛型的局限探究及知识点总结
Java+Selenium实现文件上传下载功能详解
![](https://img.aspxhome.com/file/2023/5/129285_0s.png)
项目为什么引入log4j而不是logback代码
![](https://img.aspxhome.com/file/2023/1/67971_0s.png)
Java多线程 CompletionService
解决微服务中关于用户token处理到的坑
java:无法访问org.springframework.boot.SpringApplication的解决方法
![](https://img.aspxhome.com/file/2023/1/67701_0s.jpg)
关于国际化、OGNL表达式语言
![](https://img.aspxhome.com/file/2023/7/84257_0s.jpg)
Android自定义软键盘的设计与实现代码
![](https://img.aspxhome.com/file/2023/0/99790_0s.jpg)
详解Android 中的文件存储
Android仿微信清理内存图表动画(解决surfaceView屏幕闪烁问题)demo实例详解
![](https://img.aspxhome.com/file/2023/7/111737_0s.gif)
Java的优先队列PriorityQueue原理及实例分析
Android内置SQLite的使用详细介绍
![](https://img.aspxhome.com/file/2023/2/139372_0s.png)
浅析Spring工厂的反射和配置文件
![](https://img.aspxhome.com/file/2023/4/77964_0s.jpg)