加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

如何从java扫描程序获取文件中的位置(字节位置)?

发布时间:2020-12-15 08:32:32 所属栏目:Java 来源:网络整理
导读:如何从 java扫描程序获取文件中的位置(字节位置)? Scanner scanner = new Scanner(new File("file"));scanner.useDelimiter("abc");scanner.hasNext();String result = scanner.next(); 而现在:如何获取结果在文件中的位置(以字节为单位)? 使用scanner.ma
如何从 java扫描程序获取文件中的位置(字节位置)?

Scanner scanner = new Scanner(new File("file"));
scanner.useDelimiter("abc");
scanner.hasNext();
String result = scanner.next();

而现在:如何获取结果在文件中的位置(以字节为单位)?

使用scanner.match().start()不是答案,因为它给出了内部缓冲区内的位置.

解决方法

它的possibe使用RandomAccessFile ..试试这个..

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;

public class RandomFileAccessExample 
{
    RandomFileAccessExample() throws IOException
    {
        RandomAccessFile file = new RandomAccessFile("someTxtFile.txt","r");
        System.out.println(file.getFilePointer());
        file.readLine();
        System.out.println(file.getFilePointer());
    }
    public static void main(String[] args) throws IOException {
        new RandomFileAccessExample();
    }

}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读