我有两个问题。
1)如果你的程序有一系列在某些条件下发生变化的原语,但是你需要保存原语的值,即使在程序关闭之后,你会使用序列化和反序列化吗?
2)如果是这样,你怎么做/代码是什么样的 包Lib;
public class App {
static vars obj = new vars();
public static void main(String args[]) {
runLogic(obj);
}
private static void runLogic(final vars obj){
int book;
String person;
int date;
String dateToday;
int i = (Integer)0;
String a;
String one = null;
int i2 = (Integer)0;
String b;
String two = null;
int i3 = (Integer)0;
String c;
String three = null;
int i4 = (Integer)0;
String d;
String four = null;
int i5 = (Integer)0;
for (;;) {
System.out.println("\n\nMAIN MENU\n"+
"Press:\n" +
"1 and then 'enter' for checkout,\n"+
"2 and then 'enter' for check in,\n" +
"3 and then 'enter' to look at books checked out,\n"+
"4 and then 'enter' to look at books overdue");
Scanner scanner = new Scanner(System.in);
int main = scanner.nextInt();
switch (main) {
//checkout
case 1:
System.out.println("Enter the book number");
Scanner booknum = new Scanner(System.in);
book = booknum.nextInt();
System.out.println("Enter the person's name");
Scanner name = new Scanner(System.in);
person = name.nextLine();
System.out.println("Enter the number of weeks the book can be borrowed for");
Scanner weeks = new Scanner(System.in);
date = weeks.nextInt();
int millis = date * 604800000;
System.out.println("Enter today's date");
Scanner theDate = new Scanner(System.in);
dateToday = theDate.nextLine();
Timer time = new Timer();
if (book == 1) {
i = 1;
a = "1";
one = a +" is checked out by " + person + "(due by "+ date + " weeks)" + "-checked out on " + dateToday;
time.schedule(new TimerTask(){
public void run(){
obj.set(0.5);
}
},millis);
}
if (book == 2) {
i2 = 2;
b = "2";
two = b +" is checked out by " + person + "(due by "+ date + " weeks)" + "-checked out on " + dateToday;
time.schedule(new TimerTask(){
public void run(){
obj.set2(0.5);
}
},millis);
if (book == 3) {
i3 = 3;
c = "3";
three = c +" is checked out by " + person + "(due by "+ date + " weeks)" + "-checked out on " + dateToday;
time.schedule(new TimerTask(){
public void run(){
obj.set3(0.5);
}
},millis);
}
if (book == 4) {
i4 = 4;
d = "4";
four = d +" is checked out by " + person + "(due by "+ date + " weeks)" + "-checked out on " + dateToday;
time.schedule(new TimerTask(){
public void run(){
obj.set4(0.5);
}
},millis);
}
if (book == 5) {
i5 = 5;
e = "5";
five = e +" is checked out by " + person + "(due by "+ date + " weeks)" + "-checked out on " + dateToday;
time.schedule(new TimerTask(){
public void run(){
obj.set5(0.5);
}
},millis);
}
break;
//check in
case 2:
System.out.println("Enter the book number");
Scanner s6 = new Scanner(System.in);
int byeBook = s6.nextInt();
if (byeBook == 1) {
i = (Integer) 0;
obj.set(0);
}
if (byeBook == 2) {
i2 = (Integer) 0;
obj.set2(0);
}
if (byeBook == 3) {
i3 = (Integer) 0;
obj.set3(0);
}
if (byeBook == 4) {
i4 = (Integer) 0;
obj.set4(0);
}
if (byeBook == 5) {
i5 = (Integer) 0;
obj.set5(0);
}
if (byeBook == 6) {
i6 = (Integer) 0;
obj.set6(0);
}
if (byeBook == 7) {
i7 = (Integer) 0;
obj.set7(0);
}
if (byeBook == 8) {
i8 = (Integer) 0;
obj.set8(0);
}
if (byeBook == 9) {
i9 = (Integer) 0;
obj.set9(0);
}
if (byeBook == 10) {
i10 = (Integer) 0;
obj.set10(0);
}
break;
//checked out books
case 3:
System.out.println("here are the books checked out");
if (i == 1) {
System.out.println(one);
} else {
}
if (i2 == 2) {
System.out.println(two);
} else {
}
if (i3 == 3) {
System.out.println(three);
} else {
}
if (i4 == 4) {
System.out.println(four);
} else {
}
if (i5 == 5) {
System.out.println(five);
} else {
}
break;
//overdue books
case 4:
double done = obj.get();
double dtwo = obj.get2();
double dthree = obj.get3();
double d4 = obj.get4();
double d5 = obj.get5();
if (done == 0.5){
System.out.println("Book 1 is overdue.To find out who has the book checked out,look at the list of checked out books.");
}
if (dtwo == 0.5){
System.out.println("Book 2 is overdue.To find out who has the book checked out,look at the list of checked out books.");
}
if (dthree == 0.5){
System.out.println("Book 3 is overdue.To find out who has the book checked out,look at the list of checked out books.");
}
if (d4 == 0.5){
System.out.println("Book 4 is overdue.To find out who has the book checked out,look at the list of checked out books.");
}
if (d5 == 0.5){
System.out.println("Book 5 is overdue.To find out who has the book checked out,look at the list of checked out books.");
}
}
}
}
//manage overdue functionality
static class vars{
double od;
double od2;
double od3;
double od4;
double od5;
void set(double val){
od = val;
}
double get(){
return od;
}
void set2(double val2){
od2 = val2;
}
double get2(){
return od2;
}
void set3(double val3){
od3 = val3;
}
double get3(){
return od3;
}
void set4(double val4){
od4 = val4;
}
double get4(){
return od4;
}
void set5(double val5){
od5 = val5;
}
double get5(){
return od5;
}
}
}
答案 0 :(得分:0)
序列化用于Java中的短期持久性。使用序列化来保留一些对象是可以的,但是对于大量对象和基元,使用数据库是首选,因为它确保了它的ACID兼容性。就像您选择序列化一样 - 一旦您的代码库增长,类更改会使您的序列化数据不可读,并且让非基于Java的代码通过I / O使用该信息是不可能的。
答案 1 :(得分:0)
基本上有two degrees序列化:
浅拷贝复制所有"原语"变量,但也没有序列化其他对象。深层拷贝序列化所有。
序列化是非常通用的术语。你可以任何你想要的方式做到这一点。这意味着将您的对象以任何形式保存在任何永久存储中。
序列化的常用方法:
不要害怕看看Google Gson或JPA(虽然JPA可能过于复杂,目前无法理解)。这些只是少数,有很多方法可以进行序列化。
答案 2 :(得分:0)
我认为这里有一些术语混淆。每Wikipedia:
序列化是将数据结构或对象状态转换为可以存储的格式(例如,在文件或内存缓冲区中,或通过网络连接链接传输)并稍后在相同或另一个计算机环境中重建的过程
除了显示供用户查看的内容之外,计算机所做的几乎所有操作都是某种形式的序列化。读取和写入文件或数据库,创建和解析JSON,XML,CSV和其他结构化数据格式都算作序列化。实际上,调用某些序列化的关键要求是它是可逆的 - 只要(相关的)数据在写入过程中没有丢失,你的程序就会对它正在使用的任何内容进行序列化。
现在所有这一切都不要与Java Serialization混淆,{{3}}是一种特殊的数据序列化方式,专门为Java对象和数据结构而设计。使用Java序列化有利有弊(个人而言,我会说更多的缺点,但这既不在这里也不在那里)但它只是你可以“保存原语价值的众多方法之一,即使在程序已关闭“。根据您的具体操作,您可能只想将数据写入文本文件,然后再次读取。很多时候,除此之外的任何事都是矫枉过正的。
答案 3 :(得分:0)
这取决于。
答案 4 :(得分:0)
如果我理解您的问题,并根据我认为的最新编辑,请更改vars
-
static class vars implements Serializable {
private static final long serialVersionUID = 1l;
然后使用ByteArrayInputStream和OutputStream进行演示,
public static void main(String[] args) {
vars v = new vars();
v.set(1.0);
v.set2(2.0);
v.set3(3.0);
v.set4(4.0);
v.set5(5.0);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
try {
ObjectOutputStream oos = new ObjectOutputStream(
baos);
oos.writeObject(v);
oos.close();
ByteArrayInputStream bais = new ByteArrayInputStream(
baos.toByteArray());
ObjectInputStream ois = new ObjectInputStream(
bais);
vars dev = (vars) ois.readObject();
System.out.println(dev.get5());
} catch (Exception e) {
e.printStackTrace();
}
}
vars
应该是Java命名标准的Vars
,但上面的输出是预期的
5.0