Skip to content

Instantly share code, notes, and snippets.

@AleksandrPikalo
Created May 26, 2019 10:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AleksandrPikalo/bae1c6067b3a5c9079844244320d63f7 to your computer and use it in GitHub Desktop.
Save AleksandrPikalo/bae1c6067b3a5c9079844244320d63f7 to your computer and use it in GitHub Desktop.
Serialize
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_8" default="true" project-jdk-name="1.8" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" />
</component>
</project>
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/saveVar.iml" filepath="$PROJECT_DIR$/saveVar.iml" />
</modules>
</component>
</project>
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>
import java.io.IOException;
public class Run {
public static void main (String[] args) throws IllegalAccessException, IOException, NoSuchFieldException, InstantiationException {
Saver saver = new Saver();
Serialize ser = new Serialize();
ToFileFromFile toFileFromFile = new ToFileFromFile();
saver.setS("Hello");
saver.setI(10);
saver.setD(50.3);
toFileFromFile.save(ser.serialize(saver));
String fromFile = toFileFromFile.read();
ser.deserialize(fromFile, saver.getClass());
}
}
import java.lang.annotation.*;
@Target(value = ElementType.FIELD)
@Retention(value = RetentionPolicy.RUNTIME)
public @interface Save {
}
public class Saver {
@Save
public String s;
@Save
private int i;
public double d;
public void setS(String s) {
this.s = s;
}
public void setI(int i) {
this.i = i;
}
public void setD(double d) {
this.d = d;
}
public String getS() {
return s;
}
public int getI() {
return i;
}
public double getD() {
return d;
}
}
import java.io.FileReader;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.security.InvalidParameterException;
public class Serialize {
public static String serialize (Object o) throws IllegalAccessException {
Class<?> cls = o.getClass();
StringBuilder sb = new StringBuilder();
Field[] fields = cls.getDeclaredFields();
for (Field field : fields) {
if (!field.isAnnotationPresent(Save.class)) {
continue;
}
if (Modifier.isPrivate(field.getModifiers())) {
field.setAccessible(true);
}
sb.append(field.getName() + "=");
if (field.getType()==int.class) {
sb.append(field.getInt(o));
} else {
if (field.getType() == String.class) {
sb.append((String) field.get(o));
} else {
if (field.getType() == long.class) {
sb.append(field.getDouble(o));
}
}
}
sb.append(";");
}
return sb.toString();
}
public static <T> T deserialize (String s, Class<T> cls) throws IllegalAccessException, InstantiationException, NoSuchFieldException {
T res = (T)cls.newInstance();
String[] pairs = s.split(";");
for (String pair : pairs) {
String[] nv = pair.split("=");
if (nv.length!=2) {
throw new InvalidParameterException();
}
String name = nv[0];
String value = nv[1];
Field field = cls.getDeclaredField(name);
if (Modifier.isPrivate(field.getModifiers())) {
field.setAccessible(true);
if (field.isAnnotationPresent(Save.class)) {
if (field.getType() == int.class) {
field.setInt(res, Integer.parseInt(value));
} else {
if (field.getType() == String.class) {
field.set(res, value);
} else {
if (field.getType() == Double.class) {
field.setDouble(res, Double.parseDouble(value));
}
}
}
}
}
}
return res;
}
}
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class ToFileFromFile {
String path = "C:\\Users\\admin\\IdeaProjects\\saveVar\\src\\File";
public void save (String s) throws IOException {
FileWriter fileWriter = new FileWriter(path);
try {
fileWriter.write(s);
} finally {
fileWriter.close();
}
}
public String read () throws FileNotFoundException {
FileReader fileReader = new FileReader(path);
String res = fileReader.toString();
return res;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment