Java 17 Recipes


Download 3.2 Mb.
Pdf ko'rish
bet154/245
Sana02.06.2024
Hajmi3.2 Mb.
#1839910
1   ...   150   151   152   153   154   155   156   157   ...   245
Bog'liq
Java 17 Recipes

 How It Works
The Java Serialization framework allows you to specify the implementation for 
serializing an object. It requires implementing the Externalizable interface instead 
of the Serializable interface. The Externalizable interface contains two methods: 
writeExternal(ObjectOutput out) and readExternal(ObjectInput in). By 
implementing these methods, you tell the framework how to encode/decode your 
object. You may choose to implement the Externalizable interface instead of the 
Serializable interface because Java’s default serialization is very inefficient. Because the 
Java Serialization framework needs to ensure that every object (and dependent object) is 
serialized. It writes even objects with default values or that might be empty and/or null. 
Implementing the Externalizable interface also provides finer-grained control on how 
your class is being serialized. In our example, the Serializable version created a setting 
of 439 bytes, compared with the Externalizable version of only 103 bytes!
8-3. Serializing Java Objects as XML
 Problem
Although you love the Serialization framework, you want to create something that is at 
least cross-language-compatible (or human-readable). You would like to save and load 
your objects using XML.
ChApTer 8 InpuT And OuTpuT


304
 Solution
In this example, the XMLEncoder object encodes the Settings object, which contains 
program settings information, and writes it to the settings.xml file. The XMLDecoder 
object takes the settings.xml file and reads it as a stream, decoding the Settings 
object. FileSystem gains access to the machine’s file system. FileOutputStream writes a 
file to the system. FileInputStream obtains input bytes from a file within the file system. 
In this example, these three file objects create new XML files reads them for processing.
public static void main(String[] args) {
Ch_8_3_XMLExample example = new Ch_8_3_XMLExample();
example.start();
}
private void start() {
ProgramSettings settings = new ProgramSettings(new Point(10,10),new 
Dimension(300,200), Color.blue, "The title of the application" );
try {
//Encoding
FileSystem fileSystem = FileSystems.getDefault();
try (FileOutputStream fos = new FileOutputStream("settings.xml"); 
XMLEncoder encoder =
new XMLEncoder(fos)) {
encoder.setExceptionListener((Exception e) -> {
System.out.println("Exception! :"+e.toString());
});
encoder.writeObject(settings);
}
// Decoding
try (FileInputStream fis = new FileInputStream("settings.xml"); 
XMLDecoder decoder =
new XMLDecoder(fis)) {
ProgramSettings decodedSettings = (ProgramSettings) decoder.
readObject();
System.out.println("Is same? "+settings.
equals(decodedSettings));
}
ChApTer 8 InpuT And OuTpuT


305
Path file= fileSystem.getPath("settings.xml");
List xmlLines = Files.readAllLines(file, Charset.
defaultCharset());
xmlLines.stream().forEach((line) -> {
System.out.println(line);
});
} catch (IOException e) {
e.printStackTrace();
}
}

Download 3.2 Mb.

Do'stlaringiz bilan baham:
1   ...   150   151   152   153   154   155   156   157   ...   245




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling