Java 如何从特定部分开始读取二进制文件

Java 如何从特定部分开始读取二进制文件,java,android,string,file-io,binaryfiles,Java,Android,String,File Io,Binaryfiles,我在代码中所做的只是使用FileOutputStream的append标志将一个标记然后一些字符串数据附加到一个二进制文件中。现在,如何从指定的标记开始读取?我不知道从哪里开始 文件和字符串大小是可变的,所以我不能真正信任它。唯一不变的是标记 编辑:我忘了提到该文件将从另一个线程/活动/应用程序/设备访问 用于附加某些数据的代码: String TAG = "CLIPPYDATA-"; String content = "The quick brown fox jumps over the la

我在代码中所做的只是使用
FileOutputStream
的append标志将一个标记然后一些字符串数据附加到一个二进制文件中。现在,如何从指定的标记开始读取?我不知道从哪里开始

文件和字符串大小是可变的,所以我不能真正信任它。唯一不变的是标记

编辑:我忘了提到该文件将从另一个线程/活动/应用程序/设备访问

用于附加某些数据的代码:

String TAG = "CLIPPYDATA-";
String content = "The quick brown fox jumps over the lazy dog."; //size not fixed, sample purpose only.

FileOutputStream output = new FileOutputStream("/path/to/file", true);
try {
        output.write((TAG + content).getBytes());
} finally {
        //output.flush(); (should I?)
        output.close();
}
样本输出:

yyvAžîéÃI&8QÀ Ø +ZŠ( ¢Š( ¢Š(ÿÙCLIPPYDATA-The quick brown fox jumps over the lazy dog.

样本输入:

yyvAžîéÃI&8QÀ Ø +ZŠ( ¢Š( ¢Š(ÿÙCLIPPYDATA-The quick brown fox jumps over the lazy dog.
期望输出:

CLIPPYDATA-The quick brown fox jumps over the lazy dog.
只需将
seek()
写入标记的位置即可


编辑“您在何处写入标记”由写入标记之前的文件大小给出。

如注释中所述,以下是一个示例:

对于附加数据:

    String TAG = "CLIPPYDATA-";
    String content = "The quick brown fox jumps over the lazy dog."; //size not fixed, sample purpose only.
    File outputFile  = new File("/path/to/file");
    long fileLength = outputFile.length();
    FileOutputStream output = new FileOutputStream(outputFile, true);
    try {
        output.write((TAG + content).getBytes());
        byte[] bytes = ByteBuffer.allocate(Long.SIZE / Byte.SIZE).putLong(fileLength).array();
        output.write(bytes);
    } finally {
        //output.flush(); (should I?)
        output.close();
    }
    RandomAccessFile raf = new RandomAccessFile("/path/to/file", "rb");
    long endPositon = raf.length() - Long.SIZE / Byte.SIZE;
    // get last 8 bytes
    raf.seek(endPositon);
    long tagPosition = raf.readLong();
    raf.seek(tagPosition);
    byte[] bytes = new byte[endPositon - tagPosition];
    raf.read(bytes);
    String appendedData = new String(bytes);
    if (appendedData.startsWith(TAG)) {
        // appendedData is what you want
    }
对于读取数据:

    String TAG = "CLIPPYDATA-";
    String content = "The quick brown fox jumps over the lazy dog."; //size not fixed, sample purpose only.
    File outputFile  = new File("/path/to/file");
    long fileLength = outputFile.length();
    FileOutputStream output = new FileOutputStream(outputFile, true);
    try {
        output.write((TAG + content).getBytes());
        byte[] bytes = ByteBuffer.allocate(Long.SIZE / Byte.SIZE).putLong(fileLength).array();
        output.write(bytes);
    } finally {
        //output.flush(); (should I?)
        output.close();
    }
    RandomAccessFile raf = new RandomAccessFile("/path/to/file", "rb");
    long endPositon = raf.length() - Long.SIZE / Byte.SIZE;
    // get last 8 bytes
    raf.seek(endPositon);
    long tagPosition = raf.readLong();
    raf.seek(tagPosition);
    byte[] bytes = new byte[endPositon - tagPosition];
    raf.read(bytes);
    String appendedData = new String(bytes);
    if (appendedData.startsWith(TAG)) {
        // appendedData is what you want
    }

为什么不把标记位置放在文件的末尾呢?如果你把标记位置放在文件的末尾,比如最后8个字节。然后,您可以执行以下操作:读取最后8个字节->获取标记的位置->查找位置->读取标记并content@bladefury它已经在文件的末尾了。文件内容->标记->内容。我只是添加了新数据。无论如何,我想你是说我也应该把附加数据的起始位置放在哪里,对吗?是的,在附加标记和content@bladefury听起来合法。我来试一试。问题是偏移量是可变的。这并不能回答这个问题。若要评论或要求作者澄清,请在其帖子下方留下评论。-@PandaLion98没关系,
seek()
接受一个整数,可以是一个变量。@EJP变量,因为没有固定值:)@PandaLion9所以你真正的问题是知道值,而不是进行搜索?你的问题不是这么说的。当然,在进行追加之前,您需要的值是由文件大小给定的。@PandaLion98除非只有一个追加,否则很难看出这是如何解决问题的。