1. import java.util.HashMap;
  2. import java.util.Map;
  3. public class fibMemo {
  4. public static void main(String[] args) {
  5. Map<Integer, Integer> map = new HashMap<Integer, Integer>();
  6. System.out.println(fibMemo(10, map));
  7. map = new HashMap<Integer, Integer>();
  8. System.out.println(fibMemo(20, map));
  9. }
  10. public static int fibMemo(int n, Map<Integer, Integer> map) {
  11. return n;
  12. }
  13. }
  14. /*prints
  15. 10
  16. 20
  17. */

编译多个Java文件

  1. //方法一
  2. javac TransportBike.java
  3. javac TransportCar.java
  4. javac TransportationDriver.java
  5. //方法二
  6. javac TransportBike.java TransportCar.java TransportationDriver.java
  7. //方法三
  8. javac Transport*.java
  9. //方法四 只编译有main方法的文件
  10. javac TransportationDriver.java
  11. //运行
  12. java TransportationDriver

外部文件传递参数

  1. //方法一
  2. //TransportationDriver文件:
  3. import java.io.*;
  4. public class TransportationDriver {
  5. public static void main(String[] args) {
  6. Bike harley = new Bike();
  7. Car toyota = new Car();
  8. harley.wheels();
  9. toyota.wheels();
  10. System.out.println(args[0] + " and " + args[1] + " want to purchase a bike.");
  11. System.out.println(args[1] + " wants to purchase a car.");
  12. }
  13. }
  14. java TransportationDriver Customer1 Customer2
  15. //方法二
  16. import java.io.*;
  17. import java.util.Scanner;
  18. public class TransportationDriver {
  19. public static void main(String[] args) {
  20. Bike harley = new Bike();
  21. Car toyota = new Car();
  22. String fileName = args[0];
  23. Scanner input = new Scanner(fileName);
  24. String person1 = input.next();
  25. String person2 = input.next();
  26. harley.wheels();
  27. toyota.wheels();
  28. System.out.println(person1 + " and " + person2 + " want to purchase a bike.");
  29. System.out.println(person2 + " wants to purchase a car.");
  30. }
  31. }
  32. //input.txt 文件里放Customer1 Customer2
  33. java TranportationDriver input.txt
  34. //打印结果
  35. /*A bike has two wheels.
  36. A car has four wheels.
  37. Customer1 and Customer2 want to purchase a bike.
  38. Customer2 wants to purchase a car.
  39. */

序列化

序列化接口用于储存文件中,内存中,数据库中对象的状态 (成员字段)

  1. import java.io.Serializable;
  2. public class Car implements Serializable{
  3. private String make;
  4. private int year;
  5. //provide the JVM with a unique identifier when converting a serialized stream of bytes back into an object with a serialVersionUID
  6. private static final long serialVersionUID=1L;
  7. }

FileOutputStream,它将帮助我们写入文件
ObjectOutputStream,它将帮助我们将可序列化对象写入输出流
在main()中初始化FileOutputStream对象,该对象将创建一个字节流,并将其写入名为person-file.txt的文件。
在main()中初始化ObjectOutputStream对象,这将帮助将对象序列化到指定的输出流。
在main()中使用objectOutputStream.writeObject()将michael和peter对象序列化到一个文件。

  1. public class Person implements Serializable {
  2. private String name;
  3. private int age;
  4. private static final long serialVersionUID = 1L;
  5. public Person(String name, int age) {
  6. this.name = name;
  7. this.age = age;
  8. }
  9. public static void main(String[] args) throws FileNotFoundException, IOException{
  10. Person michael = new Person("Michael", 26);
  11. Person peter = new Person("Peter", 37);
  12. //将创建一个字节流,并将其写入名为person-file.txt的文件
  13. FileOutputStream fileOutputStream = new FileOutputStream("persons.txt");
  14. //将对象序列化到指定的输出流
  15. ObjectOutputStream objectOutputStream = new ObjectOutputStream(fileOutputStream);
  16. //将michael和peter对象序列化到一个文件。
  17. objectOutputStream.writeObject(michael);
  18. objectOutputStream.writeObject(peter);
  19. }
  20. }
  21. --------------------------------------------------------------------
  22. import java.io.Serializable;
  23. import java.io.FileOutputStream;
  24. import java.io.ObjectOutputStream;
  25. import java.io.FileNotFoundException;
  26. import java.io.IOException;
  27. public class Car implements Serializable {
  28. private String make;
  29. private int year;
  30. private static final long serialVersionUID = 1L;
  31. public Car(String make, int year){
  32. this.make=make;
  33. this.year=year;
  34. }
  35. public static void main(String[] args) throws FileNotFoundException, IOException {
  36. Car toyota=new Car("Toyota",2021);
  37. Car honda=new Car("Honda",2020);
  38. FileOutputStream fileOutputStream = new FileOutputStream("cars.txt");
  39. ObjectOutputStream objectOutputStream = new ObjectOutputStream(fileOutputStream);
  40. objectOutputStream.writeObject(toyota);
  41. objectOutputStream.writeObject(honda);
  42. }
  43. }