idgenerator.java

来自「java版的数据结构的完全代码 免费提供了 学习数据结构的请下载」· Java 代码 · 共 37 行

JAVA
37
字号
// Introduced in Chapter 17import java.io.*;/** Generates unique id numbers, even across multiple sessions. */public class IdGenerator {  /** File in which the next available id is stored. */  public static final File FILE = new File(BTree.DIR + "id");  /** Return the next available id number. */  public static int nextId() {    try {      int result;      if (FILE.exists()) {        ObjectInputStream in          = new ObjectInputStream(new FileInputStream(FILE));        result = in.readInt();      } else {        result = 0;      }      ObjectOutputStream out        = new ObjectOutputStream(new FileOutputStream(FILE));      out.writeInt(result + 1);      out.close();      return result;    } catch (IOException e) {      e.printStackTrace();      System.exit(1);      return 0;    }  }  public static void main(String[] args) {    System.out.println(nextId());  }}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?