⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 testfiles.java

📁 Java经典例程 从外国一大学计算机教授出版物下载的代码 经典
💻 JAVA
字号:
import java.io.*;
import javagently.*;

class TestFiles implements Serializable  {

  /* Serializing files                J M Bishop  April 2000
   * =================
   *
   * illustrates how easy it is to save and retrieve a data structure
   * to and from a file.
   * The program and the class of objects must declar that they
   * implement Serializable.
   */

  TestFiles ()  throws IOException {
    doItWithAnArray();
    doItWithAList();
  }

  Serializable outAndInAgain (Serializable structure) {
    Serializable copy = null;

     try{
     // Serialize the object
       ObjectOutputStream out =
             new ObjectOutputStream (new FileOutputStream("mydata"));
       out.writeObject(structure);
       out.close();

     // Read the object back in
       ObjectInputStream in =
             new ObjectInputStream (new FileInputStream("mydata"));
       copy = (Serializable) in.readObject();
       in.close();
     } catch (IOException e)
       {System.out.println("IO problem "+e.getMessage());}
       catch (ClassNotFoundException e)
       {System.out.println("Class problem "+e.getMessage());}
       return copy;
    }

   void doItWithAnArray () {

    Students [] table = new Students [10];
    Students [] copy = new Students [10];
     // Set up test data
    table[0] = new Students("Peter",84);
    table[1] = new Students("John",51);
    table[2] = new Students("Kathy",65);
    int n = 2;

    // Print the original data
    System.out.println("\nThe original table");
     for (int i=0; i<=n; i++)
       System.out.println(table[i]);

    // Make a few changes
    table[1].marks = 58;
    table[2].name = "Cathy";
    table[3] = new Students("Sipho",70);
    n = 3;

    copy = (Students []) outAndInAgain (table);

     //Print the table and the copy
     System.out.println("\nThe copied table");
     for (int i=0; i<=n; i++) {
       System.out.println(copy[i]);
     }
   }

  void doItWithAList () throws IOException {
    Coffee coffees = null;
    Coffee copy = null;
    String name;
    Stream in = new Stream ("names.dat", Stream.READ);

    // Read the coffee data from a text file
    for (int i=0; i<=4; i++) {
      name = in.readString();
      coffees = new Coffee (name, coffees);
    }

    // Print out the list
    Coffee list = coffees;
    System.out.println("\nCoffees on the list");
    for (int i=0; i<=4; i++) {
      System.out.println(list.name);
      list = list.link;
    }

    copy = (Coffee) outAndInAgain (coffees);

    //Print the list as read back
     list = copy;
     System.out.println("\nThe read back list");
     for (int i=0; i<=4; i++) {
       System.out.println(list.name);
       list = list.link;
     }
   }

   public static void main (String args []) throws IOException {
     new TestFiles ();
   }

class Students implements Serializable {

  String name;
  int marks;

  Students (String n, int m) {
    name = n;
    marks = m;
  }

  public String toString () {
    return name + " " + marks;
  }
}

  class Coffee implements Serializable {
    /* Small test class with a link */

    Coffee (String name, Coffee c) {
      this.name = name;
      link = c;
    }

    String name;
    Coffee link;
  }

}

⌨️ 快捷键说明

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