android逐行读取文件内容以及保存为文件
用于长时间使用的apk,并且有规律性的数据
1,逐行读取文件内容
//首先定义一个数据类型,用于保存读取文件的内容
class WeightRecord {
String timestamp;
float weight;
public WeightRecord(String timestamp, float weight) {
this.timestamp = timestamp;
this.weight = weight; }
} //开始读取
private WeightRecord[] readLog() throws Exception {
ArrayList<WeightRecord> result = new ArrayList<WeightRecord>();
File root = Environment.getExternalStorageDirectory();
if (root == null)
throw new Exception("external storage dir not found");
//首先找到文件
File weightLogFile = new File(root,WeightService.LOGFILEPATH);
if (!weightLogFile.exists())
throw new Exception("logfile '"+weightLogFile+"' not found");
if (!weightLogFile.canRead())
throw new Exception("logfile '"+weightLogFile+"' not readable");
long modtime = weightLogFile.lastModified();
if (modtime == lastRecordFileModtime)
return lastLog;
// file exists, is readable, and is recently modified -- reread it.
lastRecordFileModtime = modtime;
// 然后将文件转化成字节流读取
FileReader reader = new FileReader(weightLogFile);
BufferedReader in = new BufferedReader(reader);
long currentTime = -1;
//逐行读取
String line = in.readLine();
while (line != null) {
WeightRecord rec = parseLine(line);
if (rec == null)
Log.e(TAG, "could not parse line: '"+line+"'");
else if (Long.parseLong(rec.timestamp) < currentTime)
Log.e(TAG, "ignoring '"+line+"' since it's older than prev log line");
else {
Log.i(TAG,"line="+rec);
result.add(rec);
currentTime = Long.parseLong(rec.timestamp);
}
line = in.readLine();
}
in.close();
lastLog = (WeightRecord[]) result.toArray(new WeightRecord[result.size()]);
return lastLog;
}
//解析每一行
private WeightRecord parseLine(String line) {
if (line == null)
return null;
String[] split = line.split("[;]");
if (split.length < 2)
return null;
if (split[0].equals("Date"))
return null;
try {
String timestamp =(split[0]);
float weight = Float.parseFloat(split[1]) ;
return new WeightRecord(timestamp,weight);
}
catch (Exception e) {
Log.e(TAG,"Invalid format in line '"+line+"'");
return null;
}
}
2,保存为文件
public boolean logWeight(Intent batteryChangeIntent) {
Log.i(TAG, "logBattery");
if (batteryChangeIntent == null)
return false;
try {
FileWriter out = null;
if (mWeightLogFile != null) {
try {
out = new FileWriter(mWeightLogFile, true);
}
catch (Exception e) {}
}
if (out == null) {
File root = Environment.getExternalStorageDirectory();
if (root == null)
throw new Exception("external storage dir not found");
mWeightLogFile = new File(root,WeightService.LOGFILEPATH);
boolean fileExists = mWeightLogFile.exists();
if (!fileExists) {
if(!mWeightLogFile.getParentFile().mkdirs()){
Toast.makeText(this, "create file failed", Toast.LENGTH_SHORT).show();
}
mWeightLogFile.createNewFile();
}
if (!mWeightLogFile.exists()) {
Log.i(TAG, "out = null");
throw new Exception("creation of file '"+mWeightLogFile.toString()+"' failed");
}
if (!mWeightLogFile.canWrite())
throw new Exception("file '"+mWeightLogFile.toString()+"' is not writable");
out = new FileWriter(mWeightLogFile, true);
if (!fileExists) {
String header = createHeadLine();
out.write(header);
out.write('\n');
}
}
Log.i(TAG, "out != null");
String extras = createBatteryInfoLine(batteryChangeIntent);
out.write(extras);
out.write('\n');
out.flush();
out.close();
return true;
} catch (Exception e) {
Log.e(TAG,e.getMessage(),e);
return false;
}
}
android逐行读取文件内容以及保存为文件的更多相关文章
- Android音频处理——通过AudioRecord去保存PCM文件进行录制,播放,停止,删除功能
Android音频处理--通过AudioRecord去保存PCM文件进行录制,播放,停止,删除功能 音频这方面很博大精深,我这里肯定讲不了什么高级的东西,最多也只是一些基础类知识,首先,我们要介绍一下 ...
- 解决gvim 8.1中zip插件打开zip文件内容时,而文件路径带有空格的问题。
解决gvim 8.1中zip插件打开zip文件内容时,而文件路径带有空格的问题. 现象是只能打开一次,第二次打开就显示为空了. 通过 lcd切换工作目录.使得命令行操作中不再有带空格的路径 vim81 ...
- MATLAB实例:新建文件夹,保存.mat文件并保存数据到.txt文件中
MATLAB实例:新建文件夹,保存.mat文件并保存数据到.txt文件中 作者:凯鲁嘎吉 - 博客园 http://www.cnblogs.com/kailugaji/ 用MATLAB实现:指定路径下 ...
- Shell脚本逐行读取文本内容并拆分,根据条件筛选文件
时间:2018-11-13 整理:byzqy 需求: 最近帮朋友写了一段脚本,他的需求是根据一份产品清单,去服务器上捞取对应产品编号的测试Log,数量大概有9000~10000条左右.文本内容大致如下 ...
- NPOI 2.0 读取、编辑、保存Excel文件
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.I ...
- Java 读取Excel内容并保存进数据库
读取Excel中内容,并保存进数据库 步骤 建立数据库连接 读取文件内容 (fileInputStream 放进POI的对应Excel读取接口,实现Excel文件读取) 获取文件各种内容(总列数,总行 ...
- Java多种方式读文件,追加文件内容,等对文件的各种操作
一.多种方式读文件内容. 1.按字节读取文件内容 2.按字符读取文件内容 3.按行读取文件内容 4.随机读取文件内容 import java.io.BufferedReader; import jav ...
- Linux 父子进程实现复制文件内容到另一个文件内
1. 子进程先拷贝前一半 ,父进程一直阻塞到子进程拷贝完再拷贝后一半 /* 子进程先拷贝前一半文件,父进程先阻塞等待子进程拷贝完前一半内容, * 然后父进程在拷贝,后一半内容 * */ #includ ...
- Beyond Compare 忽略两个文件内容的顺序比较文件内容(xjl456852原创)
有时两个文件内容的顺序是不固定的,对比时需要忽略文件顺序进行对比. 可以这样设置: 点击菜单下面工具栏按钮: 点击Format旁的三角,选择Sorted,就会按文件的顺序排序比较.忽略了文件内容顺序的 ...
随机推荐
- 第三篇:web之前端之JavaScript基础
前端之JavaScript基础 前端之JavaScript基础 本节内容 JS概述 JS基础语法 JS循环控制 ECMA对象 BOM对象 DOM对象 1. JS概述 1.1. javascript ...
- css3中允许单词内断句word-wrap和怎么处理断句word-break
首先说一下:浏览器的默认行为,在一行中几个单词 排列着,如果最后一个长单词 太长时 首先是移到下一行,如果该单词的长度大于父元素的宽度,会溢出. <!doctype html> <h ...
- 使用html5兼容低版本浏览器
因为html5 新出的一些语义化的标签,在低版本浏览器下不能识别,举个例子,比如你写了一个 header 标签中,写了一段文本,在低版本浏览器下,肯定是能看到的,但是,那是他是不认识 header标签 ...
- Socket服务器整体架构概述
转载:http://www.cnblogs.com/tianzhiliang/archive/2010/10/28/1863684.html Socket服务器主要用于提供高效.稳定的数据处理.消息转 ...
- Android Studio 2.2 HTTP proxy setting 提示异常
操作系统 :MacOS 10.11.6 IDE :Android Studio 2.2 Java Version :1.8 异常现象描述: 在给Android Studio 2.2设置代理时,出现警告 ...
- java 反射 - 获取成员变量的值.
通过反射,可以获取所有声明的成员变量(包括所有的),代码如下: package spt.test.src; public class Person { private String name = &q ...
- FTPClient文件下载
一.FTPClient下载文件所需要的jar包: org.apache.commons.net [commons-net-3.4.jar] 二.FTPClient的连接和关闭 //FTPClient ...
- 给分类(Category)添加属性
遇到一个问题,写了一个分类,但原先类的属性不够用.添加一个属性,调用的时候崩溃了,说是找不到getter.setter方法.查了下文档发现,OC的分类允许给分类添加属性,但不会自动生成getter.s ...
- .NET 设计模式之简单工厂模式(二)
1:建立接口 namespace Factory { public interface IPerson { } } 2:建立Worker.Student来继承IPerson接口 namespace F ...
- windows 远程连接linux服务器
百度搜索“SSH Secure Shell Client” 并下载 2 点击Profiles选择add profiles 并添加profils名称 3 点击Profiles 填写远程linux的ip ...