File读写是我们经常对于File处理经常需要动作,接下来我会用4中方式进行操作,具体在项目中采用哪一种方式,由自己进行判断。
最近在Thomas Nagel《What Does It All Mean》,摘录开头一段话:
要评估每一种主张,每一条论证和每一套理论,并且尝试着判断它们是否可以被接受,这最终都依赖于每一个人的独立思考,而非听命于权威
2B青年方式:
private static class StandardSerialization implements SerializationTest { public void testWriteBuffered(TestObject test, String fileName) throws IOException { ObjectOutputStream objectOutputStream = null; try { FileOutputStream fileOutputStream = new FileOutputStream(fileName); BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(fileOutputStream); objectOutputStream = new ObjectOutputStream(bufferedOutputStream); objectOutputStream.writeObject(test); } finally { if (objectOutputStream != null) { objectOutputStream.close(); } } } public TestObject testReadBuffered(String fileName) throws IOException, ClassNotFoundException { ObjectInputStream objectInputStream = null; try { FileInputStream fileInputStream = new FileInputStream(fileName); BufferedInputStream bufferedInputStream = new BufferedInputStream(fileInputStream); objectInputStream = new ObjectInputStream(bufferedInputStream); return (TestObject) objectInputStream.readObject(); } finally { if (objectInputStream != null) { objectInputStream.close(); } } } }
普通青年:
private static class StandardSerializationRaf implements SerializationTest { public void testWriteBuffered(TestObject test, String fileName) throws IOException { ObjectOutputStream objectOutputStream = null; try { RandomAccessFile randomAccessFile = new RandomAccessFile(fileName, FILE_MODE_RW); FileOutputStream fileOutputStream = new FileOutputStream(randomAccessFile.getFD()); objectOutputStream = new ObjectOutputStream(fileOutputStream); objectOutputStream.writeObject(test); } finally { if (objectOutputStream != null) { objectOutputStream.close(); } } } public TestObject testReadBuffered(String fileName) throws IOException, ClassNotFoundException { ObjectInputStream objectInputStream = null; try { RandomAccessFile randomAccessFile = new RandomAccessFile(fileName, FILE_MODE_R); FileInputStream fileInputStream = new FileInputStream(randomAccessFile.getFD()); objectInputStream = new ObjectInputStream(fileInputStream); return (TestObject) objectInputStream.readObject(); } finally { if (objectInputStream != null) { objectInputStream.close(); } } } }
文艺青年:
采用Kryo Framework
private static class Kryo2Serialization implements SerializationTest { private static Kryo kryo = new Kryo(); public void testWriteBuffered(TestObject test, String fileName) throws IOException { Output output = null; try { RandomAccessFile raf = new RandomAccessFile(fileName, "rw"); output = new Output(new FileOutputStream(raf.getFD()), MAX_BUFFER_SIZE); kryo.writeObject(output, test); } finally { if (output != null) { output.close(); } } } public TestObject testReadBuffered(String fileName) throws IOException { Input input = null; try { RandomAccessFile raf = new RandomAccessFile(fileName, "r"); input = new Input(new FileInputStream(raf.getFD()), MAX_BUFFER_SIZE); return kryo.readObject(input, TestObject.class); } finally { if (input != null) { input.close(); } } } }
Geeker:
private static class DirectSerialization implements SerializationTest { public void testWriteBuffered(TestObject test, String fileName) throws IOException { RandomAccessFile raf = null; try { MemoryBuffer memoryBuffer = new MemoryBuffer(MAX_BUFFER_SIZE); raf = new RandomAccessFile(fileName, FILE_MODE_RW); test.write(memoryBuffer); raf.write(memoryBuffer.getBuffer()); } finally { if (raf != null) { raf.close(); } } } public TestObject testReadBuffered(String fileName) throws IOException { RandomAccessFile raf = null; try { raf = new RandomAccessFile(fileName, FILE_MODE_R); MemoryBuffer unsafeBuffer = new MemoryBuffer((int) raf.length()); raf.read(unsafeBuffer.getBuffer()); return TestObject.read(unsafeBuffer); } finally { if (raf != null) { raf.close(); } } } }
最终输出的结果是:
结论:
RandomAccessFile确实提高很明显
Kryo-dynamic serialization在write的时候优势明显,读取相差无几
UNSAFE_MEMORY确实高效,不过容易Memory Leak
相关推荐
《深入理解System.Runtime.Serialization.DLL及其在.NET框架中的作用》 在.NET框架中,`System.Runtime.Serialization`命名空间是处理序列化和反序列化的核心组件,而`System.Runtime.Serialization.dll`则是这个...
sirenix.serialization.dll
放在bin文件夹 解决 System.Runtime.Serialization.Json无法引用的问题
hystrix-serialization-1.5.18.jar
kotlinx-serialization-compiler-plugin.jar
**PyPI 官网下载 | oslo.serialization-2.2.0.tar.gz** PyPI(Python Package Index)是Python开发者获取和分享开源软件包的主要平台。`oslo.serialization` 是一个在PyPI上发布的Python库,它专注于数据序列化和反...
文本存档便于人类阅读,而二进制存档则更紧凑且速度更快。存档对象负责实际的序列化和反序列化操作。 4. **序列化策略**:可以使用`BOOST_CLASS_EXPORT`宏来标记类,以便在运行时动态注册,也可以在编译时静态注册...
【Kotlinx.Serialization详解】 Kotlinx.Serialization是一个强大的开源库,专门为Kotlin编程语言提供了跨平台的序列化解决方案。这个库允许开发者将数据对象转换成字节流或JSON等不同格式,反之亦然,这对于数据...
Boost.Serialization库是C++社区广泛使用的序列化工具,它提供了丰富的功能来处理各种类型的对象,包括智能指针。在C++11及更高版本中,智能指针(如`std::unique_ptr`,`std::shared_ptr`和`std::scoped_ptr`)被...
.net2.0版本的json操作类 ...System.Web.Script.Serialization的json操作类,位于3.0以上的System.Web.Extensions类库中. 本项目是从System.Web.Extensions中剥离出System.Web.Script.Serialization的完整项目
数据的序列化和反序列化 Serialization DeSerialization
akka-kryo-serialization, 基于Kryo的Akka序列化 akka-kryo-serialization-- Scala 和Akka基于kryo的序列化程序这个库为 Scala 和Akka提供定制的基于kryo的序列化程序。 它可以用于更高效的akka远程处理。它还可以...
- 整个代码由一个头文件组成 json.hpp,没有子项目,没有依赖关系,没有复杂的构建系统,使用起来非常方便 - 语法直观,就像写普通的c++代码 - 不止用起来似c++习惯和风格,更是使用 C++ 11 标准编写 ...
Kotlin cross-platform / multi-format reflectionless serialization Kotlin serialization consists of a compiler plugin, which automatically produces visitor code for classes, and runtime library, ...
根据测试结果,fast-serialization的序列化速度可以提高2到10倍。此外,fast-serialization与JDK原生的序列化是兼容的,因此可以作为JDK原生序列化的drop-in replacement使用。这就意味着,开发者可以在不需要修改已...
资源分类:Python库 所属语言:Python 资源全名:oslo.serialization-2.21.0-py2.py3-none-any.whl 资源来源:官方 安装方法:https://lanzao.blog.csdn.net/article/details/101784059
cereal is a header-only C++11 serialization library. cereal takes arbitrary data types and reversibly turns them into different representations, such as compact binary encodings, XML, or JSON. cereal ...
在.NET框架中,`System.Runtime.Serialization.dll`, `ServiceModel.dll` 和 `ServiceModel.Web.dll` 这三个动态链接库(DLL)是实现数据序列化和网络服务操作的关键组件。它们在开发过程中扮演着重要角色,特别是在...