博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Android App data write as file data with synchronous Demo
阅读量:6353 次
发布时间:2019-06-22

本文共 7168 字,大约阅读时间需要 23 分钟。

1 package com.android.utils;  2   3 import java.io.File;  4 import java.io.IOException;  5 import java.io.RandomAccessFile;  6 import java.util.Arrays;  7   8 import android.app.Activity;  9 import android.util.Log; 10  11  12 /** 13  * 在一些对数据实时性要求比较高的场合,如随时可能断电的场合下,同时需要将数据写入文件中, 14  * 这个时候,我们不希望数据在内存中呆太久,最好能够做到同步,这是我们的需求。
15 * 第一种方案:
16 * 1. RandomAccessFile
17 * 2. public RandomAccessFile(File file, String mode) throws FileNotFoundException
18 * Constructs a new RandomAccessFile based on file and opens it according to the access string in mode.
19 * 3. mode may have one of following values:
20 * 1. "r" The file is opened in read-only mode. An IOException is thrown if any of the write methods is called.
21 * 2. "rw" The file is opened for reading and writing. If the file does not exist, it will be created.
22 * 3. "rws" The file is opened for reading and writing. Every change of the file's content or metadata must be written synchronously to the target device.
23 * 4. "rwd" The file is opened for reading and writing. Every change of the file's content must be written synchronously to the target device.
24 * 4. 由于我们需要其中的数据同步功能,所以我们选择使用包装RandomAccessFile类,实现要求。
25 * 第二种方案:
26 * 1. FileDescriptor中有sync()方法
27 Ensures that data which is buffered within the underlying implementation is written out to the appropriate device before returning.
28 * 2. FileOutputStream中的 getFD()方法
29 Returns a FileDescriptor which represents the lowest level representation of an operating system stream resource.
30 * 3. 使用起来感觉没有RandomAccessFile方便,放弃时使用
31 */ 32 33 public class ZengjfRandomAccessFile { 34 /** 35 * 将整形数组写入文件 36 * 37 * @param filePath 文件路径 38 * @param data 整形数组 39 * @throws IOException 40 */ 41 static public void writeIntArray(String filePath, int[] data) throws IOException { 42 if (null == filePath || null == data) 43 return ; 44 45 if (filePath.trim().equals("")) 46 return ; 47 48 File file = new File(filePath); 49 if (!file.exists()) 50 file.createNewFile(); 51 52 if (!file.canWrite()) 53 throw new RuntimeException("Zengjf Utils writeIntArray(): no permission for file -- " + filePath + "."); 54 55 // write data 56 RandomAccessFile raf = new RandomAccessFile(file, "rws"); 57 for (int i = 0; i < data.length; i++) 58 raf.writeInt(data[i]); 59 60 raf.close(); 61 } 62 63 /** 64 * 将整形数组写入文件,文件目录被指定,作为使用者可以不用关心 65 * 66 * @param activity 调用这个函数的Activity 67 * @param data 要保存的的整形数组 68 * @throws IOException 69 */ 70 static public void writeIntArray(Activity activity, int[] data) throws IOException { 71 if (null == activity || null == data) 72 return ; 73 74 String filePath = activity.getApplicationContext().getFilesDir().getAbsoluteFile() + "/zengjfData.txt"; 75 writeIntArray(filePath, data); 76 } 77 78 /** 79 * 从文件中读出长度为length的整形数组 80 * 81 * @param filePath 文件路径 82 * @param length 数组长度 83 * @return 返回数组,如果出错,返回null 84 * @throws IOException 85 */ 86 static public int[] readIntArray(String filePath, int length) throws IOException { 87 88 if (null == filePath || length <= 0) 89 return null; 90 91 if (filePath.trim().equals("")) 92 return null; 93 94 File file = new File(filePath); 95 if (!file.canRead()) 96 throw new RuntimeException("Zengjf Utils writeIntArray(): no permission for file -- " + filePath + "."); 97 98 int[] data = new int[length]; // for return data 99 100 // if file not exist in first time and file length less than data size, 101 // just create file and make data for it102 if (!file.exists() || (file.length() < (4 * length))) {103 for (int i = 0; i < data.length; i++) 104 data[i] = 0;105 106 writeIntArray(filePath, data);107 return data;108 }109 110 //get data111 RandomAccessFile raf = new RandomAccessFile(file, "r");112 for (int i = 0; i < length; i++) 113 data[i] = raf.readInt();114 115 raf.close();116 117 return data;118 }119 120 /**121 * 从文件中读取整形数组,文件位置、名已经被指定,作为使用者可以不关心122 * 123 * @param activity 调用这个函数的Activity124 * @param length 数组的长度125 * @return 返回数组,如果出错,返回null126 * @throws IOException127 */128 static public int[] readIntArray(Activity activity, int length) throws IOException {129 if (null == activity || 0 == length) 130 return null;131 132 String filePath = activity.getApplicationContext().getFilesDir().getAbsoluteFile() + "/zengjfData.txt";133 return readIntArray(filePath, length);134 }135 136 /**137 * 往文件中写入原始整形数组,其实就是填充整形0138 * 139 * @param filePath 文件路径140 * @param length 数组大小141 * @throws IOException142 */143 static public void writeRawIntArray(String filePath, int length) throws IOException {144 145 if (null == filePath || length <= 0) 146 return ;147 148 if (filePath.trim().equals("")) 149 return ;150 151 File file = new File(filePath); 152 int[] data = new int[length]; // for return data153 154 // if file not exist in first time, just create file and make data for it155 if (file.exists()) {156 for (int i = 0; i < data.length; i++) 157 data[i] = 0;158 159 writeIntArray(filePath, data);160 }161 }162 163 /**164 * 165 * 往文件中写入值为0的整形数组,文件位置、名已经被指定,作为使用者可以不关心166 * 167 * @param activity 调用这个函数的Activity168 * @param length 写入数组的长度169 * @throws IOException170 */171 static public void writeRawIntArray(Activity activity, int length) throws IOException{172 if (null == activity || 0 == length) 173 return ;174 175 String filePath = activity.getApplicationContext().getFilesDir().getAbsoluteFile() + "/zengjfData.txt";176 writeRawIntArray(filePath, length);177 }178 179 /**180 * 测试用的的Demo181 * @param activity 调用这个函数的Activity182 */183 static public void testDemo(Activity activity) {184 int[] data = {1, 2, 3, 4, 5, 6};185 try {186 writeIntArray(activity, data);187 int[] redata = readIntArray(activity, 6);188 Log.e("zengjf utils", Arrays.toString(redata));189 } catch (IOException e) {190 // TODO Auto-generated catch block191 e.printStackTrace();192 }193 }194 }

 

转载于:https://www.cnblogs.com/zengjfgit/p/4612301.html

你可能感兴趣的文章
cmake检测g++编译器是否支持c++11
查看>>
有关Oracle数据库中同义词
查看>>
Ubuntu10.4下配置telnet服务
查看>>
IM 之 融云
查看>>
前言《iOS网络高级编程:iPhone和iPad的企业应用开发》(书籍学习)
查看>>
shell学习笔记(2)
查看>>
node_modules
查看>>
第一个爬虫测试
查看>>
Spring.net 间接调用被AOP拦截的方法失效(无法进入aop的拦截方法)
查看>>
Mysql 不同版本 说明
查看>>
常见BUG
查看>>
条件表达式
查看>>
SpringMvc多视图配置(jsp、velocity、freemarker) 在src目录views.properties配置
查看>>
项目使用EntityFramework需要做的几项工作
查看>>
notepad++
查看>>
爱上MVC~在Views的多级文件夹~续~分部页的支持
查看>>
常用的css命名规则:
查看>>
PHP检测数组中的每个值是否含有特殊字符
查看>>
JavaScript全局变量的避免(《JavaScript语言精粹》相关笔记)
查看>>
宝宝不开心
查看>>