Java 使用随机流(RandomAccessFile)读取文件最后一行数据

 使用随机流(RandomAccessFile)读取文件中的汉字

读取一行的方法:

String filePath="F:\\code\\java\\text\\demo\\db\\test.txt";
File file = new File(filePath);
RandomAccessFile randomAccessFile=new RandomAccessFile(file,"r");
String string=new String(randomAccessFile.readLine().getBytes("ISO-8859-1"), "gbk")
randomAccessFile.close();

读取全部的方法:

String filePath="F:\\code\\java\\text\\demo\\db\\test.txt";
File file = new File(filePath);
RandomAccessFile randomAccessFile=new RandomAccessFile(file, "r");
byte bytes[]=new byte[999];
randomAccessFile.read(bytes);
System.out.println( new String(bytes,"gbk"));
randomAccessFile.close();

 使用随机流(RandomAccessFile)读取文件最后一行数据

 public static void main(String[] args) throws IOException {
       String filePath="F:\\code\\java\\text\\demo\\db\\test.txt";
       System.out.println(getFileEndLine(filePath));
   }
    /**
     * 获取文件最后一行数据,文件中没有数据就返回""
     * @param filePath 文件路径
     * @return 文件最后一行数据,文件中没有数据就返回""
     * @throws IOException
     */
    public static String getFileEndLine(String filePath) throws IOException {

        File file = new File(filePath);
        RandomAccessFile randomAccessFile=new RandomAccessFile(file, "r");
        //获得文件中数据的长度
        long pos=randomAccessFile.length();
        //判断文件是否为空
        if(pos==0){
            return  "";
        }
        String endLine=getEndLine(randomAccessFile,pos)
        randomAccessFile.close();
        return endLine;
    }

    /**
     * 循环获取文件最后一行数据
     */
    private static String getEndLine(RandomAccessFile randomAccessFile,long pos) throws IOException {
        String endLine="";
        while (pos>0){
            //使光标向前移动一位
            pos--;
            //是读取文件数据的光标移动到pos位置
            randomAccessFile.seek(pos);

            //下面两个if的顺序不能颠倒

            //预防文件只有第一行有不为空的数据 和 文件中只有空格、换行的情况出现
            if (pos==0){
                //读取一行数据,从光标位置到改行尾部的数据
                endLine = randomAccessFile.readLine();
                //CharSequenceUtil.isAllBlank()检查字符串是否为空
                //endLine=" \t \n"也判定为空
                if (CharSequenceUtil.isAllBlank(endLine)) {
                    endLine="";
                }
            }

            if(randomAccessFile.read()=='\n') {
                //读取一行数据,从光标位置到改行尾部的数据
                endLine = randomAccessFile.readLine();
                //CharSequenceUtil.isAllBlank()检查字符串是否为空
                //endLine=" \t \n"也判定为空
                if (CharSequenceUtil.isAllBlank(endLine)) {
                    endLine =  "";
                } else {
                    break;
                }
            }
        }
        return endLine;
    }

自己记录的一些笔记,内容如有不对请指正

  • 2
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

wangjingyang2020

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值