java 读取文件第n行,Java阅读第n行

I am trying to read a specific line from a text file, however I don't want to load the file into memory (it can get really big).

I have been looking but every example i have found requires either to read every line (this would slow my code down as there are over 100,000 lines) or load the whole thing into an array and get the correct element (file will have alot of lines to input).

An example of what I want to do:

String line = File.getLine(5);

"code is not actual code, it is made up to show the principle of what i want"

Is there a way to do this?

-----Edit-----

I have just realized this file will be written too in between reading lines (adding to the end of the file).

解决方案

Is there a way to do this?

Not unless the lines are of a fixed number of bytes each, no.

You don't have to actually keep each line in memory - but you've got to read through the whole file to get to the line you want, as otherwise you won't know where to start reading.

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值