栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > .Net

java倒序读文件

.Net 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

java倒序读文件

package cn.com.songjy.test.io;

import java.io.FileNotFoundException;

import java.io.IOException;

import java.io.RandomAccessFile;

public class FromEndRF {

public static void read(String filename, String charset) {

RandomAccessFile rf = null;

try {

rf = new RandomAccessFile(filename, "r");

long len = rf.length();

long start = rf.getFilePointer();

long nextend = start + len - 1;

String line;

rf.seek(nextend);

int c = -1;

while (nextend > start) {

c = rf.read();

if (c == 'n' || c == 'r') {

line = rf.readLine();

if (line != null) {

System.out.println(new String(line

.getBytes("ISO-8859-1"), charset));

} else {

System.out.println(line);

}

nextend--;

}

nextend--;

rf.seek(nextend);

if (nextend == 0) {// 当文件指针退至文件开始处,输出第一行

// System.out.println(rf.readLine());

System.out.println(new String(rf.readLine().getBytes(

"ISO-8859-1"), charset));

}

}

} catch (FileNotFoundException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

} finally {

try {

if (rf != null)

rf.close();

} catch (IOException e) {

e.printStackTrace();

}

}

}

public static void main(String args[]) {

read("mynewfile.txt", "UTF-8");

}

}

引自:

http://bbs.csdn.net/topics/190181198

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/1023709.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号