🍪 Privacy & Transparency

We and our partners use cookies to Store and/or access information on a device. We and our partners use data for Personalised ads and content, ad and content measurement, audience insights and product development. An example of data being processed may be a unique identifier stored in a cookie. Some of our partners may process your data as a part of their legitimate business interest without asking for consent. To view the purposes they believe they have legitimate interest for, or to object to this data processing use the vendor list link below. The consent submitted will only be used for data processing originating from this website. If you would like to change your settings or withdraw consent at any time, the link to do so is in our privacy policy accessible from our home page..

Vendor List | Privacy Policy
Skip to content

Technik Blog

Programmieren | Arduino | ESP32 | MicroPython | Python | Raspberry PI

Menu
  • Projekte
    • LED’s
    • Servo & Schrittmotoren
    • Sound
    • LCD’s
    • Kommunikation
    • Sicherheit
    • Weekend Project
  • Arduino
    • Tutorials
    • ProMini
      • Anschließen & Programmieren
    • Nano
      • Arduino Nano – Übersicht
    • UNO
      • Übersicht
    • MEGA 2560
      • Übersicht
    • Leonardo
      • Übersicht
    • NodeMCU
      • NodeMCU – “Einer für (fast) Alles!”
    • Lilypad
      • Arduino: Lilypad “Jetzt Geht’s Rund!”
    • WEMOS
      • WEMOS D1 – Arduino UNO kompatibles Board mit ESP8266 Chip
      • WEMOS D1 Mini – Übersicht
      • Wemos D1 mini Shields
    • STM32x
      • STM32F103C8T6 – Übersicht
    • Maker UNO
      • Maker UNO – Überblick und Test
    • ATTiny85
      • Mini Arduino mit ATTiny85 Chip
      • ATtiny85 mit dem Arduino UNO beschreiben
  • Android
  • Über mich
  • DeutschDeutsch
  • EnglishEnglish
Menu

Serialisieren von Objekten

Posted on 14. Juli 20142. Mai 2023 by Stefan Draeger

Eine Java Klasse kann durch das Serialisieren quasi im Ganzen gespeichert werden. Dazu muss jedoch das Interface Serializable implementiert werden.

import java.io.*;
import java.util.*;
import java.lang.Exception;

public class EinstellungHandler {

	private static final String FILENAME = "./einstellung.ser";

	private static Einstellung einstellung;

	public static void main(String... args) {
		if (new File(FILENAME).exists()){
			einstellung = (Einstellung) readObject();
		} else {
		    einstellung = new Einstellung();
		}
		einstellung.setSprache("deutsch");
		writeObject(einstellung);
	}

	private static Object readObject() {
		Object o = null;
		FileInputStream fileInStream = null;
		ObjectInputStream objInStream = null;
		try {
			fileInStream = new FileInputStream(FILENAME);
			objInStream = new ObjectInputStream(fileInStream);
			o = objInStream.readObject();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} finally {
			if (fileInStream != null) {
				try {
					fileInStream.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}

			if (objInStream != null) {
				try {
					objInStream.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		return o;
	}

	private static void writeObject(Object o) {
		FileOutputStream fileOutStream = null;
		ObjectOutputStream objOutStream = null;
		try {
			fileOutStream = new FileOutputStream(FILENAME);
			objOutStream = new ObjectOutputStream(fileOutStream);
			objOutStream.writeObject(o);
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (fileOutStream != null) {
				try {
					fileOutStream.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}

			if (objOutStream != null) {
				try {
					objOutStream.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}

	private static class Einstellung implements Serializable {

		private static final long serialVersionUID = 1l;

		private String sprache;

		public String getSprache() {
			return this.sprache;
		}

		public void setSprache(String inSprache) {
			this.sprache = inSprache;
		}
	}
}

Nachteile der Serialisierung von Objekten

Als großen Nachteil der Serialisierung von Objekten ist dass, wenn die Klasse welche Serialisiert wurde (quasi gespeichert) auch nur mit diesem Code geladen werden kann.

Änderungen, an der Java Klasse führen dazu das die Serialisierte Datei nicht mehr geladen werden kann, es wird eine java.io.InvalidClassException ausgegeben.

Schreibe einen Kommentar Antworten abbrechen

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert

Kategorien

Tools

  • 8×8 LED Matrix Tool
  • 8×16 LED Matrix Modul von Keyestudio
  • 16×16 LED Matrix – Generator
  • Widerstandsrechner
  • Rechner für Strom & Widerstände
  • ASCII Tabelle

Meta

  • Videothek
  • Impressum
  • Datenschutzerklärung
  • Disclaimer
  • Kontakt
  • Cookie-Richtlinie (EU)

Links

Blogverzeichnis Bloggerei.de Blogverzeichnis TopBlogs.de das Original - Blogverzeichnis | Blog Top Liste Blogverzeichnis trusted-blogs.com
©2023 Technik Blog | Built using WordPress and Responsive Blogily theme by Superb