标签:err table 一个 java基础 rgs rop 文件操作 关闭 print
This class implements a hash table[该类实现hashtable]
which maps keys to values [元素是键值对]
Any non-null object can be used as a key or as a value [hashtable的键和值都不能为null]
所以是从上面看,hashTable 基本上和hashMap一样的.
hashTable 是线程安全的,hashMap 是线程不安全的.
package class_Map;
import java.util.Hashtable;
public class ClassTest03_HashTable {
?
@SuppressWarnings({ "rawtypes", "unchecked" })
public static void main(String[] args) {
Hashtable table = new Hashtable();// ok
table.put("john", 100); // ok
// table.put(null, 100); //错误 , 键不能为 null, 抛出异常
// table.put("john", null);//错误 , 值不能为 null, 抛出异常
table.put("lucy", 100);// ok
table.put("lic", 100);// ok
System.out.println(table);
}
}

Properties是继承了HashTable的子类,主要用于文件io流当中。
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.Iterator;
import java.util.Properties;
?
public class Properties01 {
?
public static void main(String[] args) {
?
// 创建一个 Properties 对象, 按照 key-value 来进行管理的
Properties prop = new Properties();
// 对文件操作
try {
// 功能,将 a.properites 文件读取
// 读取属性文件a.properties
// 文件编程内容,你们 1 day 讲解
// 创建一个 InputStream, 就可以读取 a.properties
// in 的编译类型 InputStream, 运行类型 BufferedInputStream
InputStream in = new BufferedInputStream(new FileInputStream("src\\my.properties"));
prop.load(in); /// 加载属性列表
?
// 泛型<String>
Iterator<String> it = prop.stringPropertyNames().iterator();
// 迭代器读取
while (it.hasNext()) {
String key = it.next();
/*
*
* user=hsp pwd=123456 ip=192.168.11.11
*/
System.out.println(key + ":" + prop.getProperty(key));
}
// 关闭流
in.close();
?
} catch (Exception e) {
// TODO: handle exception
System.out.println(e.getMessage());
}
?
}
}
import java.io.FileOutputStream;
import java.util.Properties;
?
public class Properties02 {
?
public static void main(String[] args) {
// TODO Auto-generated method stub
// 创建一个 Properties 对象, 按照 key-value 来进行管理的
Properties prop = new Properties();
try {
?
/// 保存属性到a.properties文件
// 创建了一个 FileOutputStream 对象,用于向文件中 a.properties 写入内容
FileOutputStream oFile = new FileOutputStream("src\\my.properties", true);// true表示追加打开
prop.setProperty("db", "order"); //增加
//
//prop.store(oFile, "The New properties file");
prop.store(oFile, ""); //如果是空串就没有多余信息了..
oFile.close();
?
} catch (Exception e) {
// TODO: handle exception
System.err.println(e.getMessage());
}
System.out.println("操作成功~~");
}
}
标签:err table 一个 java基础 rgs rop 文件操作 关闭 print
原文地址:https://www.cnblogs.com/SongHai/p/14195399.html