- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我正在开发 Java 文件 I/O 接口(interface),我需要我的文件采用二进制格式,而不是字符串格式。我发现 ObjectOutputStream 和 ObjectInputStream 对我的需求很有用,但我需要接口(interface)能够在文件末尾写入,因为我需要它在同一文件中一遍又一遍地记录 future 的数据。
我尝试使用 FileOutputStream(String file, boolean append) 构造函数,但由于我的类实现了 Serialized,因此似乎存在问题。当它尝试读取第二条记录时,它会抛出 StreamCorruptedException。我知道这一定是因为有这个标题描述了对象的字段。
我的问题是,如何才能成功存储对象?我喜欢使用对象,因为它可以更轻松地构造和处理数据。这是我到目前为止的代码:
try
{
FileOutputStream outFile;
ObjectOutputStream outStream;
FileInputStream inFile;
ObjectInputStream inStream;
outFile = new FileOutputStream("people.mcf", true);
outStream = new ObjectOutputStream(outFile);
//Writing the objects
System.out.println("Writing file...");
for(int i = 0; i < 3; i++)
{
outStream.writeObject(people[i]);
}
System.out.println("Finished writing file...");
outFile.close();
outStream.close();
//Reading the files
System.out.println("Attempting to read file...");
inFile = new FileInputStream("people.mcf");
inStream = new ObjectInputStream(inFile);
Person buffer;
while(true)
{
buffer = (Person)inStream.readObject();
System.out.println(buffer.getData());
}
}
catch(EOFException e)
{
System.out.println("Reached end of file...");
}
catch(IOException e)
{
System.err.println(e.toString());
}
这是 Person 类:
static class Person implements Serializable
{
private final String name;
private final int age;
private final String address;
public Person(String name, int age, String address)
{
this.name = name;
this.age = age;
this.address = address;
}
public String getData()
{
return name + " " + age + " " + address;
}
}
这是我得到的输出:
Writing file...
Finished writing file...
Attempting to read file...
Andres 26 Palo Gordo
Pilar 22 Palo Gordo
Kelvin 27 Palo Gordo
java.io.StreamCorruptedException: invalid type code: AC
BUILD SUCCESSFUL (total time: 0 seconds)
编辑:我不是在问为什么会出现 StreamCorruptedException,我知道是 ObjectOutputStream 导致了这种情况,我要求的是另一种以结构化方式存储对象数据的方法。
最佳答案
如果您的记录具有固定长度或至少具有最大长度,则以下代码可以完成这项工作。
使用RandomAccessFile您将能够查找文件指针来写入新记录或更新现有记录。请参阅Indexed File in Wikipedia .
为了更加便携,您可以使用 ByteBuffer 序列化记录,已被 FileChannel 使用这是 RandomAccessFile 的接口(interface),请参阅 RandomAccessFile.getChannel() .
代码示例:
重新审视人员类别:
import java.nio.ByteBuffer;
import java.util.UUID;
public class Person implements ISerializable<UUID> {
private final UUID _id;
private final String _name;
private final int _age;
private final String _address;
public Person( ByteBuffer source ) {
_id = SerializeUtil.unserializeUUID( source );
_name = SerializeUtil.unserializeString( source );
_age = source.get();
_address = SerializeUtil.unserializeString( source );
}
public Person( String name, int age, String address) {
_id = UUID.randomUUID();
_name = name;
_age = age;
_address = address;
}
@Override
public UUID getKey() {
return _id;
}
@Override
public void serialize( ByteBuffer target ) {
target.clear();
SerializeUtil.serializeUUID ( _id , target );
SerializeUtil.serializeString( _name , target );
target.put((byte)_age );
SerializeUtil.serializeString( _address, target );
}
}
索引文件类:
import java.io.Closeable;
import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.util.Map;
import java.util.TreeMap;
import java.util.function.Consumer;
import java.util.function.Function;
public class IndexedFile<K extends Comparable<K>, R extends ISerializable<K>>
implements
Closeable
{
private final Map<K, Long> _index = new TreeMap<>();
private final RandomAccessFile _file;
private final FileChannel _channel;
private final ByteBuffer _record;
private final Function<ByteBuffer, R> _factory;
public IndexedFile( File tracksFile, int recordSize, Function<ByteBuffer, R> factory ) throws IOException {
_file = new RandomAccessFile( tracksFile, "rw" );
_channel = _file.getChannel();
_record = ByteBuffer.allocate( recordSize );
_factory = factory;
_channel.truncate( 0 );
}
@Override
public void close() throws IOException {
_file.close();
_channel.close();
}
public int size() {
return _index.size();
}
public int recordSize() {
return _record.capacity();
}
public void put( R record ) throws IOException {
final K key = record.getKey();
final Long offset = _index.get( key );
if( offset != null ) {
_channel.position( offset );
}
else {
final long pos = _channel.size();
_channel.position( pos );
_index.put( key, pos );
}
record.serialize( _record );
_record.position( _record.limit());
_record.flip();
_channel.write( _record );
}
public R read( int recNo ) throws IOException {
if( recNo >= _index.size()) {
return null;
}
final long offset = recNo*_record.capacity();
_channel.position( offset );
_record.clear();
_channel.read( _record );
_record.flip();
return _factory.apply( _record );
}
public R get( K key ) throws IOException {
final Long offset = _index.get( key );
if( offset == null ) {
return null;
}
_channel.position( offset );
_record.clear();
_channel.read( _record );
_record.flip();
return _factory.apply( _record );
}
}
为了完整性,实用程序类:
import java.nio.ByteBuffer;
import java.util.UUID;
public final class SerializeUtil {
public static void serializeString( String s, ByteBuffer target ) {
final byte[] bytes = s.getBytes();
target.putInt( bytes.length );
target.put( bytes );
}
public static String unserializeString( ByteBuffer target ) {
final int length = target.getInt();
final byte[] bytes = new byte[length];
target.get( bytes );
return new String( bytes );
}
public static void serializeUUID( UUID id, ByteBuffer target ) {
target.putLong( id.getMostSignificantBits());
target.putLong( id.getLeastSignificantBits());
}
public static UUID unserializeUUID( ByteBuffer source ) {
final long msb = source.getLong();
final long lsb = source.getLong();
return new UUID( msb, lsb );
}
}
以及接口(interface)ISerialized:
import java.nio.ByteBuffer;
public interface ISerializable<K extends Comparable<K>> {
public K getKey();
public void serialize( ByteBuffer target );
}
关于JAVA:如何在二进制文件中正确存储对象?不覆盖,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43698661/
我正在尝试将谷歌地图集成到 Xamarin Android。但是,如标题中所写,收到错误。此错误出现在我的 SetContentView (Resource.Layout.Main); 上,如下所示:
在 Delphi 中如何以非文本模式打开二进制文件?类似于 C 函数 fopen(filename,"rb") 最佳答案 有几个选项。 1。使用文件流 var Stream: TFileStrea
我现在正在处理一个问题,如下所示: 有两个数字 x1 和 x2 并且 x2 > x1。 例如 x1 = 5; x2 = 10; 而且我必须在二进制表示中找到 x1 和 x2 之间的总和。 5 = 10
我有这个“程序集”文件(仅包含 directives ) // declare protected region as somewhere within the stack .equiv prot_s
有没有办法在powershell中确定指定的文件是否包含指定的字节数组(在任何位置)? 就像是: fgrep --binary-files=binary "$data" "$filepath" 当然,
我是一名工程师,而不是软件程序员,所以请原谅我的无知。 我编写了一个 Delphi(7SE) 程序,用于从连接到两个数字温度计的 USB 端口读取“真实”数据类型。 我已经完成了该计划的大部分内容。
我有一些代码,例如: u=(float *)calloc(n, sizeof(float)); for(i=1; i
typedef struct pixel_type { unsigned char r; unsigned char g; unsigned char b;
如何判断二进制数是否为负数? 目前我有下面的代码。它可以很好地转换为二进制文件。转换为十进制时,我需要知道最左边的位是否为 1 以判断它是否为负数,但我似乎无法弄清楚该怎么做。 此外,我如何才能让它返
我有一个带有适当重载的 Vect*float 运算符的 vector 类,我正在尝试创建全局/非成员 float*Vect 运算符,如下所示:(注意这是一个经过大量编辑的示例) class Vect
对于使用 C 编程的项目,我们正在尝试将图像转换为二进制数据,反之亦然。我们在网上找到的所有其他解决方案都是用 C++ 或 Java 编写的。这是我们尝试过的方法: 将图像转换为包含二进制数据的文本文
我需要对列表的元素求和,其中包含所有零或一,如果列表中有 1,则结果为 1,否则为 0。 def binary_search(l, low=0,high=-1): if not l: retu
我到处搜索以找到将 float 转换为八进制或二进制的方法。我知道 float.hex 和 float.fromhex。是否有模块可以对八进制/二进制值执行相同的工作? 例如:我有一个 float 1
当我阅读有关 list.h 文件中的 hlist 的 FreeBSD 源代码时,我对这个宏感到困惑: #define hlist_for_each_entry_safe(tp, p, n, head,
我不知道出了什么问题,也不知道为什么会出现此错误。我四处搜索,但我终究无法弄明白。 void print_arb_base(unsigned int n, unsigned int b) {
在任何语言中都可以轻松地将十进制转换为二进制,反之亦然,但我需要一个稍微复杂一点的函数。 给定一个十进制数和一个二进制位,我需要知道二进制位是开还是关(真或假)。 示例: IsBitTrue(30,1
在下面的代码中,我创建了两个文件,一个是文本格式,另一个是二进制格式。文件的图标显示相同。但是这两个文件的特征完全相同,包括大小、字符集(==二进制)和流(八位字节)。为什么没有文本文件?因为如果我明
我想通读一个二进制文件。谷歌搜索“python binary eof”引导我here . 现在,问题: 为什么容器(SO 答案中的 x)不包含单个(当前)字节而是包含一大堆字节?我做错了什么? 如果应
为什么只允许以 10 为基数使用小数点?为什么以下会引发语法错误? 0b1011101.1101 我输入的数字是否有歧义?除了 93.8125 之外,字符串似乎没有其他可能的数字 同样的问题也适用于其
boost 库中有二进制之类的东西吗?例如我想写: binary a; 我很惭愧地承认我曾尝试找到它(Google、Boost)但没有结果。他们提到了一些关于 binary_int<> 的内容,但我既
我是一名优秀的程序员,十分优秀!