Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/375.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java如何将非常大的长数组写入/读取到文件中_Java - Fatal编程技术网

Java如何将非常大的长数组写入/读取到文件中

Java如何将非常大的长数组写入/读取到文件中,java,Java,我有一个15亿长的入口数组。现在,我想把数据写入磁盘,然后重新读取。有人能帮我吗?有没有java库(或自定义过程)可以有效地完成这项工作 通常,我使用FileChannel和MappedByteBuffer来完成。但是,对于15亿美元的长期进入,它只是超出了限制 编辑: 我从未尝试过使用这种大小的对象,但我认为您可以尝试将数组封装在类implementig java.io.Serializable接口中: class MyWrapper implements java.io.Serial

我有一个15亿长的入口数组。现在,我想把数据写入磁盘,然后重新读取。有人能帮我吗?有没有java库(或自定义过程)可以有效地完成这项工作

通常,我使用FileChannel和MappedByteBuffer来完成。但是,对于15亿美元的长期进入,它只是超出了限制

编辑:


我从未尝试过使用这种大小的对象,但我认为您可以尝试将数组封装在类implementig java.io.Serializable接口中:

    class MyWrapper implements java.io.Serializable 
{ 
Object[] myArray; 
}
然后,当您需要在磁盘上存储阵列时,只需使用接口方法即可:

FileOutputStream fouts = new 
        FileOutputStream("pathtofile");

    // Write object with ObjectOutputStream
    ObjectOutputStream outobj= new
        ObjectOutputStream (fouts);

    // Write object out to disk
    outobj.writeObject ( myWrapperInstance );
为了取回

FileInputStream infile = new 
    FileInputStream("pathtofile");

ObjectInputStream inobj = 
    new ObjectInputStream (infile);

Object obj = inobj.readObject();

MyWrapper myWrapperInstance = (MyWrapper) obj;

您是否安装了64位JVM?为什么你需要把它全部读入内存?你打算如何处理这个问题,因为这个问题可能还有其他解决方案。你们认为“简单地超过了限制”是什么?你是想用一句话把它写出来吗?我一直在读/写千兆字节的文件,没有问题。我一定错过了什么。你有简单的代码吗?@JamesBlack,我有64位JVM。事实上,我有一个非常大的数据集,其中包含很长的数字(15亿),我必须将其加载到内存中。必须执行某种排序工作,然后我必须写回结果。你可以使用包含临时文件的合并排序,而不是将所有数据都放在RAMI中。我会将这些行插入数据库(mysel、oracle等),然后让数据库进行排序。这就是我要做的。请参阅此处如何逐行读取文件,
FileInputStream infile = new 
    FileInputStream("pathtofile");

ObjectInputStream inobj = 
    new ObjectInputStream (infile);

Object obj = inobj.readObject();

MyWrapper myWrapperInstance = (MyWrapper) obj;