• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Devaka Cooray
  • Ron McLeod
  • paul wheaton
Saloon Keepers:
  • Tim Moores
  • Piet Souris
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Frits Walraven
  • Scott Selikoff

Saving structures in Java

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all!

Please give me some advice regarding data saving in Java.

I’m new in Java. Previously I wrote programs on C++.

It was easy to save data in C++: just declare structure, fill its fields and then save it like byte array. As far as Java doesn’t have structures I suppose that Java programming means another save concept.

I will appreciate you for ideas and ideas what to read regarding this.
 
Marshal
Posts: 76798
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Yura Vyrovoy wrote:. . . Previously I wrote programs on C++. . . .

Forget all about C++. Java® has things which look like things in C++ and are actually different. You can create a class which encapsulates your data. You can serialise instnces of that class to a byte[] with a byte array output stream, but why do you need to serialise it?
 
Yura Vyrovoy
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote: You can serialise instnces of that class to a byte[] with a byte array output stream, but why do you need to serialise it?



Perhaps your solution to forget C++ is great I've serialized my class and then I opened it and I found too much data that had caused by reflection. So I hoped that I can just use C++ practice.
 
Saloon Keeper
Posts: 14488
325
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you need strict control on how your data is serialized, you need to make your type Externalizable.

This however, is usually a BAD IDEA. If you care about how your types are represented in binary, then Java probably isn't the right tool for the job.
 
Yura Vyrovoy
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:If you need strict control on how your data is serialized, you need to make your type Externalizable.

This however, is usually a BAD IDEA. If you care about how your types are represented in binary, then Java probably isn't the right tool for the job.



Thank you for answer. It seems now that I've got what you wanted to say

Thanks again.
 
author
Posts: 23928
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:If you need strict control on how your data is serialized, you need to make your type Externalizable.



I don't think Externalizable gives full control of the data (going to file) -- as the serialization header is still written to the stream. It does however, give full control of the payload though... which may or may not be what the OP wants.

Yura Vyrovoy wrote:
It was easy to save data in C++: just declare structure, fill its fields and then save it like byte array.



This is somewhat debatable, this "works" because C provides access to the low level memory of a struct. However, this assumes an implementation detail of the compiler -- if you write the file from a program compiled on a machine, and read the same file compiled on a different machine (with a different byte alignment of fields, and different compiler), it is not guaranteed to work.

Henry
 
Stephan van Hulst
Saloon Keeper
Posts: 14488
325
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Henry Wong wrote:I don't think Externalizable gives full control of the data (going to file) -- as the serialization header is still written to the stream. It does however, give full control of the payload though... which may or may not be what the OP wants.



True, I forgot about this.

I have written a Java application in the past that wrote data to a little-endian file (I had no control over the format), and I just created my own OutputStream implementation analogous to DataOutputStream that allowed me to set the endian-ness and included methods such as readUnsignedInteger().

Yura, if you need precise control over the output, consider designing your class like this:

 
Yura Vyrovoy
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:

Henry Wong wrote:I don't think Externalizable gives full control of the data (going to file) -- as the serialization header is still written to the stream. It does however, give full control of the payload though... which may or may not be what the OP wants.



True, I forgot about this.

I have written a Java application in the past that wrote data to a little-endian file (I had no control over the format), and I just created my own OutputStream implementation analogous to DataOutputStream that allowed me to set the endian-ness and included methods such as readUnsignedInteger().

Yura, if you need precise control over the output, consider designing your class like this:



Got it! Am I right? :
- If I need precise output control I can write directly to the file. But it's my responsibility to check byte order when I read it and to check variable order while reading
- Serialization write additional info into the file but java machine is responsible for the correctness of reading.

Now it seems to be clear. Thank you very much!
 
Stephan van Hulst
Saloon Keeper
Posts: 14488
325
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, the thing is, as long as you only communicate your data between Java programs, the format of the binary data really is not important. However, if you need to communicate with applications that are platform-dependent, you need to think about the underlying binary data carefully, and Serializable is not magically going to solve this for you. Worse yet, Serializable is probably going to make things worse.

It's really difficult to give you a hard rule of thumb to help you on your way. It really depends on why you want to serialize your objects.
 
Yura Vyrovoy
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:It's really difficult to give you a hard rule of thumb to help you on your way. It really depends on why you want to serialize your objects.



In the project that I'm writing I need to save some settings. It includes list of data structures - strings, numbers etc. The easiest way (as I thought) is to write some header with the number of items in list and than to write structures one by one. When I found that I can not just write structure as a byte[] I implemented Serialization. But it's a little bit cumbersome - code and file both. So I decided to ask how more experienced people decide this problem.
 
Campbell Ritchie
Marshal
Posts: 76798
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What is wrong with a configuration or properties file? Or have I completely misunderstood the problem?
 
Yura Vyrovoy
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:What is wrong with a configuration or properties file? Or have I completely misunderstood the problem?



no problem. I just described why I asked my question.

I have settings I need to save. And while I read how to save data I've got questions about direct data save, serialization and all I've asked before.
 
Stephan van Hulst
Saloon Keeper
Posts: 14488
325
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Alright, here's an example of why the binary data doesn't matter at all. You don't care about what gets stored to disk. All you care about is that you can write and read settings. You don't need to use byte[] arrays at all, you can use java.util.Properties to set, get, load and store properties. As a matter of fact, doing it this way is preferable, because users will often want to view and change settings using a separate text editor.

It's also best to encapsulate this logic in a class that provides strong types with semantic names. Here's an example:
 
Police line, do not cross. Well, this tiny ad can go through:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic