这是在其他群里看到的一个面试题,

假如有一个10g的txt文档上面存的全是数字,内存只有8g,硬盘无限大,如何用代码找到第1000万个数字是什么?

public static void writer() throws  Exception{
        String num = "123456789";
        FileOutputStream out = new FileOutputStream(new File("D:\\test.txt"));
        OutputStreamWriter writer = new OutputStreamWriter(out);
        for (int i = 0; i < 100000000; i++) {
            StringBuilder sb = new StringBuilder();
            for (int j = 0; j < 13; j++) {
                sb.append(num);
            }
            writer.write(sb.toString());
        }
        writer.flush();
        writer.close();
        out.close();
    }

先运行上面的代码,生成了一个10g的测试文档,然后运行下面的代码,可以得到结果,话费时间为1ms

如何获取一个10g大小的文档的第1000万个字符

public static void main(String[] args) throws Exception{
//        writer();
        long start = System.currentTimeMillis();
        RandomAccessFile raf = new RandomAccessFile("D:\\test.txt","r");
        long pos = 100000000L -1L;
        raf.seek(pos);

        long end = System.currentTimeMillis();
        System.out.println("第1000万个数是:" + (char)raf.read() + ",花费的时间为" + (end-start));
    }

如何获取一个10g大小的文档的第1000万个字符

 

 

 

相关文章:

  • 2022-12-23
  • 2021-05-17
  • 2022-12-23
  • 2021-11-02
  • 2021-05-19
  • 2021-12-05
  • 2021-10-16
  • 2021-11-17
猜你喜欢
  • 2021-10-02
  • 2021-10-12
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-09-12
  • 2022-12-23
相关资源
相似解决方案