【问题标题】:ID Generator Java [duplicate]ID生成器Java [重复]
【发布时间】:2015-01-05 07:57:22
【问题描述】:

我正在尝试制作一个程序,您可以根据自己的身份在其中输入信息,然后程序将该信息存储到以您自己的 ID 号为标题的保存文件中,但我不知道如何制作该程序自动为此生成一个 ID。

import java.io.*;
import java.util.ArrayList;

public class SaveObjects{

public static void main(String[] arg){


    String name="Mike Johnson", ethnicity="White", dob="26101998";
    int grade=10;

    try{
        FileOutputStream saveFile=new FileOutputStream("10001.sav");
        ObjectOutputStream save = new ObjectOutputStream(saveFile);

        // Now we do the save.
        save.writeObject(name);
        save.writeObject(ethnicity);
        save.writeObject(dob);
        save.writeObject(grade);


        save.close();
    }
    catch(Exception exc){
        exc.printStackTrace();
    }

    name="Joe Fok"; ethnicity="Chinese"; dob="02071957";
    grade=0;

    try{
        FileOutputStream saveFile=new FileOutputStream("10002.sav");


        ObjectOutputStream save = new ObjectOutputStream(saveFile);


        save.writeObject(name);
        save.writeObject(ethnicity);
        save.writeObject(dob);
        save.writeObject(grade);


        save.close();
    }
    catch(Exception exc){
        exc.printStackTrace();
    }
}
}

这是另一个文件:

import java.io.*;
import java.util.ArrayList;

public class RestoreObjects{
public static void main(String[] arg){
    String name="", ethnicity="", dob="";
    int grade=0;
    try{
        FileInputStream saveFile = new FileInputStream("10001.sav");
        ObjectInputStream save = new ObjectInputStream(saveFile);
        name = (String) save.readObject();
        ethnicity = (String) save.readObject();
        dob = (String) save.readObject();
        grade = (Integer) save.readObject();
        save.close(); // This also closes saveFile.
    }
    catch(Exception exc){
        exc.printStackTrace(); // If there was an error, print the info.
    }
    System.out.println("\nID: 10001\n");
    System.out.println("\tName: "+name);
    System.out.println("\tEthnicity: " + ethnicity);
    System.out.println("\tDate of Birth: "+dob);
    System.out.println("\tGrade: " + grade);
    System.out.println();
    try{
        FileInputStream saveFile = new FileInputStream("10002.sav");
        ObjectInputStream save = new ObjectInputStream(saveFile);
        name = (String) save.readObject();
        ethnicity = (String) save.readObject();
        dob = (String) save.readObject();
        grade = (Integer) save.readObject();
        save.close();
    }
    catch(Exception exc){
        exc.printStackTrace();
    }
    System.out.println("\nID: 10002\n");
    System.out.println("\tName: "+name);
    System.out.println("\tEthnicity: " + ethnicity);
    System.out.println("\tDate of Birth: "+dob);
    System.out.println("\tGrade: " + grade);
    System.out.println();
}
}

【问题讨论】:

    标签: java variables


    【解决方案1】:

    一种方法是使用数据库序列,或者如果您想在 java 中执行此操作,请使用 UUID.randomUUID()

    【讨论】:

      猜你喜欢
      • 2017-02-16
      • 2023-03-09
      • 2018-07-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-09-23
      • 1970-01-01
      • 2011-11-28
      相关资源
      最近更新 更多