java按列读取数据再存储_Java如何实现按指定行读取文件

最近在开发实战中,遇到了一个这样的技术情景:

把log4j生成的日志文件定时刷进MySQL数据库,比如三个小时刷一次,那么每次刷数据的时候,如何控制文件读取是从上一次文件读取结束的地方开始继续读取的?并且本次要读取到文件结尾处。在网上各种搜索提问后,找到了一个叫RandomAccessFile Java类解决了问题。

先上代码:

static int size=1;//主要是为了控制循环的次数,因为是定时刷,每次刷的文件行数可能不一样

static long chars=0;//chars指的是字符数

/**

* 读取文件内容

* @param fileName

*/

public Map readANDwrite(String fileName) {

//大集合,以sessionid为键,以一次session的所有访问记录list为值进行存储

Map bigMap = new HashMap();

//一次session的访问记录集合

File file = new File(fileName);

//java提供的一个可以分页读取文件的类,此类的实例支持对随机访问文件的读取和写入

RandomAccessFile rf = null;

String tempString = null;

try {

//初始化RandomAccessFile,参数一个为文件路径,一个为权限设置,这点与Linux类似,r为读,w为写

rf = new RandomAccessFile(fileName, "rw");

//设置到此文件开头测量到的文件指针偏移量,在该位置发生下一个读取或写入操作

rf.seek(chars);

//获取文件的行数

int fileSize = getTotalLines(file);

for (int i = size-1; i < fileSize; i++) {//从上一次读取结束时的文件行数到本次读取文件时的总行数中间的这个差数就是循环次数

//一行一行读取

tempString = rf.readLine();

//文件中文乱码处理

tempString = tempString.replaceAll("%(?![0-9a-fA-F]{2})", "%25");

tempString = tempString.replaceAll("\\+", "%2B");

tempString = java.net.URLDecoder.decode(tempString, "GB2312");

//将字符串JSON转换为实体JSON,以便能通过key取value

JSONObject json = JSONObject.fromObject(tempString);

String refPage = json.get("refPage").toString();

System.out.println(refPage);

Map tmap = new HashMap();

if (bigMap.containsKey(refPage))

tmap = (Map) bigMap.get(refPage);

else {

tmap = new HashMap();

}

// 计数

String tCount = "count";

int pvCount = 1;

if (tmap.containsKey(tCount)) {

pvCount = (Integer) tmap.get(tCount);

}

pvCount++;

tmap.put(tCount, pvCount);

bigMap.put(refPage, tmap);

}

//返回此文件中的当前偏移量。

chars = rf.getFilePointer();

size=fileSize;

} catch (IOException e) {

e.printStackTrace();

}catch(JSONException j){

} finally {

if (rf != null) {

try {

rf.close();

} catch (IOException e1) {

}

}

}

return bigMap;

}//获取文件的行数

static int getTotalLines(File file) throws IOException {

FileReader in = new FileReader(file);

LineNumberReader reader = new LineNumberReader(in);

String s = reader.readLine();

int lines = 0;

while (s != null) {

lines++;

s = reader.readLine();

}

reader.close();

in.close();

return lines;

}



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值