Learn Typing Programming Basics SQL HTML CSS JavaScript Python C++ Java JavaFX Swing Computer Fundamentals English English Conversations Problem Solving

كيفية تطبيق المزامنة في جافا و مشكلة استرجاع البيانات

مرحباً، هل يمكن مساعدتي في فهم المزامنة في جافا أكثر.

أنا درست المزامنة من الموقع وحاولت تطبيقها بمثال صغير بحيث أنني حين أضغط على المستطيل و يزيد الطول و العرض الخاص به، و بعدها أضغط save و أفتح الملف الذي حفظته و يعطيني البيانات الصحيحة و لكن عندما أغلق البرنامج و أرجع للمزامنة لا يتم استراجع التغييرات و إنما يرجع من نقطة الصفر!

ممكن شرح كيف يمكنني حفظ التغييرات التي أفعلها مباشرة بالـscene بالملف و بعد إعادة تشغيله ترجع الإعدادت التي فعلتها قبل الحفظ و شكراً لكم.

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.stage.FileChooser;
import javafx.stage.Stage;
public class javaSer extends Application implements Serializable {
public static int inc = 0;
@Override
public void start(Stage primaryStage) {
Button btn = new Button("open setting");
Button btn2 = new Button("save setting");
btn.setOnMouseClicked((event) -> {
try {
FileChooser fileChooser = new FileChooser();
fileChooser.setTitle("open");
File selectedFile = fileChooser.showOpenDialog(primaryStage);
_import(selectedFile.getPath());
} catch (Exception e) {
}
});
btn2.setOnMouseClicked((event) -> {
try {
FileChooser fileChooser = new FileChooser();
fileChooser.setTitle("save");
File name = fileChooser.showSaveDialog(primaryStage);
_export(name.getPath());
} catch (Exception e) {
}
});
VBox root = new VBox();
root.setSpacing(20);
Rectangle rect = new Rectangle();
rect.setHeight(Editor.height);
rect.setWidth(Editor.width);
rect.setFill(Color.RED);
rect.setTranslateX(0);
rect.setTranslateX(0);
rect.setOnMouseClicked((event) -> {
Editor.height += 150;
Editor.width += 150;
});
root.getChildren().addAll(btn, btn2, rect);
Scene scene = new Scene(root, 300, 250);
primaryStage.setTitle("Hello World!");
primaryStage.setScene(scene);
primaryStage.show();
}
public static class Editor implements Serializable {
public static int height = 100 + inc;
public static int width = 100 + inc;
public String language;
public String encoding;
public String fontSize;
public String fontFamily;
public boolean autoSave;
public boolean autoComplete;
public transient String direction;
}
public static void main(String[] args) {
launch(args);
}
public void _import(String Path) {
Editor e = new Editor();
if (new File(Path).exists()) {
try {
FileInputStream fis = new FileInputStream(Path);
ObjectInputStream ois = new ObjectInputStream(fis);
e = (Editor) ois.readObject();
fis.close();
ois.close();
System.out.println("Deserialized data has been created in the memory");
System.out.println("Language: " + e.language);
System.out.println("Encoding: " + e.encoding);
System.out.println("Font size: " + e.fontSize);
System.out.println("Font family: " + e.fontFamily);
System.out.println("Auto save: " + e.autoSave);
System.out.println("Direction: " + e.direction);
System.out.println("Auto Complete: " + e.autoComplete);
System.out.println("Height: " + e.height);
System.out.println("Width : " + e.width);
System.out.println();
} catch (IOException | ClassNotFoundException ex) {
System.out.println(ex.getMessage());
}
}
// _export( Path);
}
public void _export(String Path) {
Editor e = new Editor();
try {
e.language = "woks";
e.encoding = "utf-8";
e.fontSize = "12pt";
e.fontFamily = "tahoma";
e.autoSave = true;
e.direction = "right to left";
e.height = e.height + inc;
e.width = e.width + inc;
System.out.println("" + e.height + " und " + e.width);
FileOutputStream fos = new FileOutputStream(Path);
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(e);
oos.close();
fos.flush();
fos.close();
System.out.println("path is " + Path + "");
} catch (IOException ex) {
System.out.println(ex.getMessage());
}
}
}
import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.io.Serializable; import javafx.application.Application; import javafx.scene.Scene; import javafx.scene.control.Button; import javafx.scene.layout.VBox; import javafx.scene.paint.Color; import javafx.scene.shape.Rectangle; import javafx.stage.FileChooser; import javafx.stage.Stage; public class javaSer extends Application implements Serializable { public static int inc = 0; @Override public void start(Stage primaryStage) { Button btn = new Button("open setting"); Button btn2 = new Button("save setting"); btn.setOnMouseClicked((event) -> { try { FileChooser fileChooser = new FileChooser(); fileChooser.setTitle("open"); File selectedFile = fileChooser.showOpenDialog(primaryStage); _import(selectedFile.getPath()); } catch (Exception e) { } }); btn2.setOnMouseClicked((event) -> { try { FileChooser fileChooser = new FileChooser(); fileChooser.setTitle("save"); File name = fileChooser.showSaveDialog(primaryStage); _export(name.getPath()); } catch (Exception e) { } }); VBox root = new VBox(); root.setSpacing(20); Rectangle rect = new Rectangle(); rect.setHeight(Editor.height); rect.setWidth(Editor.width); rect.setFill(Color.RED); rect.setTranslateX(0); rect.setTranslateX(0); rect.setOnMouseClicked((event) -> { Editor.height += 150; Editor.width += 150; }); root.getChildren().addAll(btn, btn2, rect); Scene scene = new Scene(root, 300, 250); primaryStage.setTitle("Hello World!"); primaryStage.setScene(scene); primaryStage.show(); } public static class Editor implements Serializable { public static int height = 100 + inc; public static int width = 100 + inc; public String language; public String encoding; public String fontSize; public String fontFamily; public boolean autoSave; public boolean autoComplete; public transient String direction; } public static void main(String[] args) { launch(args); } public void _import(String Path) { Editor e = new Editor(); if (new File(Path).exists()) { try { FileInputStream fis = new FileInputStream(Path); ObjectInputStream ois = new ObjectInputStream(fis); e = (Editor) ois.readObject(); fis.close(); ois.close(); System.out.println("Deserialized data has been created in the memory"); System.out.println("Language: " + e.language); System.out.println("Encoding: " + e.encoding); System.out.println("Font size: " + e.fontSize); System.out.println("Font family: " + e.fontFamily); System.out.println("Auto save: " + e.autoSave); System.out.println("Direction: " + e.direction); System.out.println("Auto Complete: " + e.autoComplete); System.out.println("Height: " + e.height); System.out.println("Width : " + e.width); System.out.println(); } catch (IOException | ClassNotFoundException ex) { System.out.println(ex.getMessage()); } } // _export( Path); } public void _export(String Path) { Editor e = new Editor(); try { e.language = "woks"; e.encoding = "utf-8"; e.fontSize = "12pt"; e.fontFamily = "tahoma"; e.autoSave = true; e.direction = "right to left"; e.height = e.height + inc; e.width = e.width + inc; System.out.println("" + e.height + " und " + e.width); FileOutputStream fos = new FileOutputStream(Path); ObjectOutputStream oos = new ObjectOutputStream(fos); oos.writeObject(e); oos.close(); fos.flush(); fos.close(); System.out.println("path is " + Path + ""); } catch (IOException ex) { System.out.println(ex.getMessage()); } } }

تعليقات 2

أضف تعليق

يجب تسجيل الدخول حتى تتمكن من إضافة تعليق أو رد.

الدورات

أدوات مساعدة

أقسام الموقع

دورات
مقالات كتب مشاريع أسئلة