java中的随机访问文件

发布于 2024-07-07 01:28:38 字数 634 浏览 9 评论 0原文

我有以下字段:

  • 库存控制(16 字节记录)
    • 产品 ID 代码(int – 4 字节)
    • 库存数量(int – 4 字节)
    • 价格(双精度 - 8 字节)

如何使用上述长度创建固定长度随机访问文件? 我在网上尝试了一些示例,但是当我尝试访问它们时,我要么收到 EOF 异常,要么收到随机地址值。

我尝试了更多示例,但无法很好地理解这个概念。 我正在尝试用它做一个项目,并将尝试探索更多。

这是一些示例数据。 数据中可能存在漏洞,其中否。 库存可能是23 == 023

          Quantity
ID. No.   In Stock   Price

-------   --------   ------
 1001       476      $28.35
 1002       240      $32.56
 1003       517      $51.27
 1004       284      $23.75
 1005       165      $32.25

谢谢您的帮助。

I have the following fields:

  • Inventory control (16 byte record)
    • Product ID code (int – 4 bytes)
    • Quantity in stock (int – 4 bytes)
    • Price (double – 8 bytes)

How do I create a fixed length random access file using the above lengths? I tried some examples online, but I either get an EOF exception or random address values when I try to access them.

I tried some more examples and couldn't understand the concept very well. I'm trying a project with it and will try to explore more on it.

Here is some example data. There might be holes in the data where No. in stock could be 23 == 023.

          Quantity
ID. No.   In Stock   Price

-------   --------   ------
 1001       476      $28.35
 1002       240      $32.56
 1003       517      $51.27
 1004       284      $23.75
 1005       165      $32.25

Thanks for the help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

芸娘子的小脾气 2024-07-14 01:28:38

java.io.RandomAccessFile 是您正在寻找的类。 这是一个示例实现(您可能想要编写一些单元测试,因为我没有:)

package test;

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

public class Raf {
    private static class Record{
        private final double price;
        private final int id;
        private final int stock;

        public Record(int id, int stock, double price){
            this.id = id;
            this.stock = stock;
            this.price = price;
        }

        public void pack(int n, int offset, byte[] array){
            array[offset + 0] = (byte)(n & 0xff);
            array[offset + 1] = (byte)((n >> 8) & 0xff);
            array[offset + 2] = (byte)((n >> 16) & 0xff);
            array[offset + 3] = (byte)((n >> 24) & 0xff);
        }

        public void pack(double n, int offset, byte[] array){
            long bytes = Double.doubleToRawLongBits(n);
            pack((int) (bytes & 0xffffffff), offset, array);
            pack((int) ((bytes >> 32) & 0xffffffff), offset + 4, array);
        }

        public byte[] getBytes() {
            byte[] record = new byte[16];
            pack(id, 0, record);
            pack(stock, 4, record);
            pack(price, 8, record);
            return record;
        }
    }

    private static final int RECORD_SIZE = 16;
    private static final int N_RECORDS = 1024;

    /**
     * @param args
     * @throws IOException 
     */
    public static void main(String[] args) throws IOException {
        RandomAccessFile raf = new RandomAccessFile(args[0], "rw");
        try{
            raf.seek(RECORD_SIZE * N_RECORDS);

            raf.seek(0);

            raf.write(new Record(1001, 476, 28.35).getBytes());
            raf.write(new Record(1002, 240, 32.56).getBytes());
        } finally {
            raf.close();
        }
    }
}

java.io.RandomAccessFile is the class you're looking for. Here's an example implementation (you'll probably want to write some unit tests, as I haven't :)

package test;

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

public class Raf {
    private static class Record{
        private final double price;
        private final int id;
        private final int stock;

        public Record(int id, int stock, double price){
            this.id = id;
            this.stock = stock;
            this.price = price;
        }

        public void pack(int n, int offset, byte[] array){
            array[offset + 0] = (byte)(n & 0xff);
            array[offset + 1] = (byte)((n >> 8) & 0xff);
            array[offset + 2] = (byte)((n >> 16) & 0xff);
            array[offset + 3] = (byte)((n >> 24) & 0xff);
        }

        public void pack(double n, int offset, byte[] array){
            long bytes = Double.doubleToRawLongBits(n);
            pack((int) (bytes & 0xffffffff), offset, array);
            pack((int) ((bytes >> 32) & 0xffffffff), offset + 4, array);
        }

        public byte[] getBytes() {
            byte[] record = new byte[16];
            pack(id, 0, record);
            pack(stock, 4, record);
            pack(price, 8, record);
            return record;
        }
    }

    private static final int RECORD_SIZE = 16;
    private static final int N_RECORDS = 1024;

    /**
     * @param args
     * @throws IOException 
     */
    public static void main(String[] args) throws IOException {
        RandomAccessFile raf = new RandomAccessFile(args[0], "rw");
        try{
            raf.seek(RECORD_SIZE * N_RECORDS);

            raf.seek(0);

            raf.write(new Record(1001, 476, 28.35).getBytes());
            raf.write(new Record(1002, 240, 32.56).getBytes());
        } finally {
            raf.close();
        }
    }
}
待"谢繁草 2024-07-14 01:28:38

在最新的 Java 版本中,您可以使用 FileChannel 管理随机访问文件。 SeekableByteChannel 接口定义的方法允许您更改通道连接到的目标实体(如文件)中的指针位置。 FileChannel 实现了 SeekableByteChannel,允许您使用通道管理随机访问文件。 大小、位置、截断方法允许您随机读写文件。

请参阅http://www.zoftino.com/java-random-access-files 获取详细信息和示例。

With recent Java versions, you can manage Random access files using FileChannel. SeekableByteChannel interface define methods which allow you to change the position of the pointer in the destination entity like file which the channel is connected to. FileChannel implements SeekableByteChannel allowing you to manage random access files using channels. Methods size, position, truncate allow you to read and write files randomly.

see http://www.zoftino.com/java-random-access-files for details and example.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文