import java.util.HashMap;
import java.util.Map;
public class fibMemo {
public static void main(String[] args) {
Map<Integer, Integer> map = new HashMap<Integer, Integer>();
System.out.println(fibMemo(10, map));
map = new HashMap<Integer, Integer>();
System.out.println(fibMemo(20, map));
}
public static int fibMemo(int n, Map<Integer, Integer> map) {
return n;
}
}
/*prints
10
20
*/
编译多个Java文件
//方法一
javac TransportBike.java
javac TransportCar.java
javac TransportationDriver.java
//方法二
javac TransportBike.java TransportCar.java TransportationDriver.java
//方法三
javac Transport*.java
//方法四 只编译有main方法的文件
javac TransportationDriver.java
//运行
java TransportationDriver
外部文件传递参数
//方法一
//TransportationDriver文件:
import java.io.*;
public class TransportationDriver {
public static void main(String[] args) {
Bike harley = new Bike();
Car toyota = new Car();
harley.wheels();
toyota.wheels();
System.out.println(args[0] + " and " + args[1] + " want to purchase a bike.");
System.out.println(args[1] + " wants to purchase a car.");
}
}
java TransportationDriver Customer1 Customer2
//方法二
import java.io.*;
import java.util.Scanner;
public class TransportationDriver {
public static void main(String[] args) {
Bike harley = new Bike();
Car toyota = new Car();
String fileName = args[0];
Scanner input = new Scanner(fileName);
String person1 = input.next();
String person2 = input.next();
harley.wheels();
toyota.wheels();
System.out.println(person1 + " and " + person2 + " want to purchase a bike.");
System.out.println(person2 + " wants to purchase a car.");
}
}
//input.txt 文件里放Customer1 Customer2
java TranportationDriver input.txt
//打印结果
/*A bike has two wheels.
A car has four wheels.
Customer1 and Customer2 want to purchase a bike.
Customer2 wants to purchase a car.
*/
序列化
序列化接口用于储存文件中,内存中,数据库中对象的状态 (成员字段)
import java.io.Serializable;
public class Car implements Serializable{
private String make;
private int year;
//provide the JVM with a unique identifier when converting a serialized stream of bytes back into an object with a serialVersionUID
private static final long serialVersionUID=1L;
}
FileOutputStream,它将帮助我们写入文件
ObjectOutputStream,它将帮助我们将可序列化对象写入输出流
在main()中初始化FileOutputStream对象,该对象将创建一个字节流,并将其写入名为person-file.txt的文件。
在main()中初始化ObjectOutputStream对象,这将帮助将对象序列化到指定的输出流。
在main()中使用objectOutputStream.writeObject()将michael和peter对象序列化到一个文件。
public class Person implements Serializable {
private String name;
private int age;
private static final long serialVersionUID = 1L;
public Person(String name, int age) {
this.name = name;
this.age = age;
}
public static void main(String[] args) throws FileNotFoundException, IOException{
Person michael = new Person("Michael", 26);
Person peter = new Person("Peter", 37);
//将创建一个字节流,并将其写入名为person-file.txt的文件
FileOutputStream fileOutputStream = new FileOutputStream("persons.txt");
//将对象序列化到指定的输出流
ObjectOutputStream objectOutputStream = new ObjectOutputStream(fileOutputStream);
//将michael和peter对象序列化到一个文件。
objectOutputStream.writeObject(michael);
objectOutputStream.writeObject(peter);
}
}
--------------------------------------------------------------------
import java.io.Serializable;
import java.io.FileOutputStream;
import java.io.ObjectOutputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
public class Car implements Serializable {
private String make;
private int year;
private static final long serialVersionUID = 1L;
public Car(String make, int year){
this.make=make;
this.year=year;
}
public static void main(String[] args) throws FileNotFoundException, IOException {
Car toyota=new Car("Toyota",2021);
Car honda=new Car("Honda",2020);
FileOutputStream fileOutputStream = new FileOutputStream("cars.txt");
ObjectOutputStream objectOutputStream = new ObjectOutputStream(fileOutputStream);
objectOutputStream.writeObject(toyota);
objectOutputStream.writeObject(honda);
}
}