📄 save.java
字号:
/**
* Email: taorundong@126.com
*
* @author taorundong
* @version 1.00 07/02/05
*/
//to test the restoration and onload of the names
import java.io.*;
import java.util.*;
public class Save{
People [] person = null;
FileInputStream input = null;
FileOutputStream output = null;
File file = null;
Hashtable hashTable = null;
String s = null;
Save(){
hashTable = new Hashtable();
try{
file = new File("file.txt");
input = new FileInputStream(file);
output = new FileOutputStream(file);
}
catch(Exception e){
e.printStackTrace();
}
person = new People[3];
person[0] = new People("one");
person[1] = new People("two");
person[2] = new People("three");
save();
load();
setHashTable();
}
public void save(){
for(int i=0;i<3;i++){
try{
output.write((person[i].getName()+'\n').getBytes());
}
catch(Exception e){
e.printStackTrace();
}
}
try{
output.close();
}
catch(Exception e){
e.printStackTrace();
}
}
public void load(){
byte [] buf = new byte[1024];
int b = 0;
try{
while((b=input.read(buf,0,1024)) != -1){
s =new String(buf,0,b);
String temp = s.substring(0,s.lastIndexOf('\n'));
}
}
catch(Exception e){
e.printStackTrace();
}
try{
input.close();
}
catch(Exception e){
e.printStackTrace();
}
}
//The key of the Hashtable must be not the same
//So here will show you some warning information when you built the program
public void setHashTable(){
People [] person = new People[3];
for(int i=0;i<s.length();i++){
int index = s.indexOf('\n');
String temp = s.substring(0,index);
System.out.print(temp);
s = s.substring(index+1);
person[i] = new People(temp);
hashTable.put(temp,person[i]);
}
}
public static void main(String args[]){
new Save();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -