IO流3(转换流,对象操作流,Properties)

一、标准输入输出流
System类;
public static final InputStream in; 
标准输入流,静态的,最终的,类型是InputStream(字节输入流)
System.in
public static final PrintStream out;
标准输出流,静态的,最终的,类型是PrintStream
System.out
PrintStream 为其他输出流添加了功能,使它们能够方便地打印各种数据值表示形式
。它还提供其他两项功能。与其他输出流不同,PrintStream 永远不会抛出
IOException;
二、转换流
字符流 = 字节流 + 编码
转换流是将字节流转换成字符流
1.字符流通向字节流的桥梁(将字节输出流转换成字符输出流) OutputStreamWriter
     Writer w = new OutputStreamWriter(System.out);
传入的是字节流对象,返回的是字符流对象
2.字节流通向字符流的桥梁(将字节输入流转换成字符输入流) InputStreamReader
传入的是字节流对象,返回的是字符流对象
     InputStream is = System.in;
             Reader r = new InputStreamReader(is);
三、打印流
打印流添加输出数据的功能,使它们能够方便地打印各种数据值表示形式.
字符打印流   PrintWriter    只能输出不能输入
void print(String str): 输出任意类型的数据,
void println(String str): 输出任意类型的数据,自动写入换行操作
注意:
     可以自动换行,println()
           不能输出字节,但是可以输出其他任意类型
           通过某些配置,可以实现自动刷新(但是只有在调用 println、printf 或 format时才
可用)
           也是包装流,不具备写出功能
           可以把字节输出流转换成字符输出流
1.使用打印流复制文本文件
public class PrintWriterDemo3 {
public static void main(String[] args) throws IOException  {
//创建输入流对象
BufferedReader br = new BufferedReader(new
FileReader("SystemInOutDemo.java"));
//创建打印流对象
PrintWriter pw = new PrintWriter(new
FileWriter("d:\\SystemInOutDemo.java"), true );
//自动刷新
String line;//用于存储读取到的每行数据
while((line = br.readLine()) != null) {
pw.println(line);
}
//释放资源
pw.close();
br.close();
}
}

四、对象流
ObjectInputStream 称为 反序列化流,利用输入流从文件中读取对象
继承字节输入流
Object readObject()  :从 ObjectInputStream 读取对象。
ObjectOutputStream 称为 序列化流,利用输出流向文件中写入对象
继承字节输出流
void writeObject(Object obj) :将指定的对象写入 ObjectOutputStream。
特点:用于操作对象。可以将对象写入到文件中,也可以从文件中读取对象
注意:
            使用对象输出流写出对象,只能使用对象输入流来读取对象
            只能将支持 java.io.Serializable 接口的对象写入流中
 Serializable :序列号,是一个标识接口,只起标识作用,没有方法
               当一个类的对象需要用IO流进行读写的时候,这个类必须实现该接口
1.注意事项
没有实现序列化接口: NotSerializableException
读到了序列化文件的末尾: EOFException
2.示例代码:
//学生类
public class Student implements Serializable {
private static final long serialVersionUID = 888L;
String name;
int age;
String gender;
public Student(String name,int age) {
this.name = name;
this.age = age;
}

@Override
public String toString() {
return "Student [name=" + name + ", age=" + age + ", gender=" +
gender + "]";
}
}

//测试类
public class ObjectOutputStreamDemo3 {
public static void main(String[] args) throws Exception   {
//创建对象输入流的对象
ObjectInputStream ois = new ObjectInputStream(new
FileInputStream("b.txt"));
//读取数据
Object obj = ois.readObject();
//向下转型,获取具体的子类对象
ArrayList<Student> list = (ArrayList<Student>) obj;
for (Student student : list) {
System.out.println(student);
}
//释放资源
ois.close();
}

private static void method() throws Exception {
//创建对象输出流的对象
ObjectOutputStream oos = new ObjectOutputStream(new
FileOutputStream("b.txt"));
//创建集合对象
ArrayList<Student> list = new ArrayList<Student>();
//添加学生对象
list.add(new Student("wangwu",30));
list.add(new Student("zhaoliu",28));
//写出集合对象
oos.writeObject(list);
//释放资源
oos.close();
}
}

五、HashMap和Hashtable的区别
1.相同点
都属于双列集合
键不能重复
2.不同点
HashMap可以存储null键和null值
Hashtable不可以存储null键和null值
Hashtable是JDK1.0版本就出现了
HashMap是JDK1.2版本出现的
HashMap是线程不安全的,效率较高。但是不安全
Hashtable是线程安全的,效率较低。但是安全

Hashtable目前已经被淘汰了,但是它有一个特别争气的子类,Properties集合目前还在被广泛的使用

六、Properties集合
Properties 类表示了一个持久的属性集。Properties 可保存在流中或从流中加载。属
性列表中每个键及其对应值都是一个字符串。
特点:
1、Hashtable的子类,map集合中的方法都可以用(实现了Map接口)。
2、该集合没有泛型。键值都是字符串。
3、它是一个可以持久化的属性集。键值可以存储到集合中,也可以存储到持久
化的设备(硬盘、U盘、光盘)上。键值的来源也可以是持久化的设备。
4、有和流技术相结合的方法。
1.加载配置文件中的信息
database.properties
#url=jdbc:mysql://localhost:3306/qiyue
url=jdbc:mysql://localhost:3306/qiyue
username=root
password=root
//测试类
public class Test01 {
public static void main(String[] args) throws IOException{
Properties prop = new Properties();
FileInputStream fis = new FileInputStream("database.properties");
//将输入流关联的文件内容  自动加载到集合中
prop.load(fis);
//获取内容
String url = prop.getProperty("url");
String username = prop.getProperty("username");
String password = prop.getProperty("password");
System.out.println(url);
System.out.println(username);
System.out.println(password);
}
}
2.写出集合内容到配置文件中
public class Test02 {
public static void main(String[] args) throws IOException{
Properties prop = new Properties();
prop.setProperty("url", "xxxxxxx");
prop.setProperty("username", "root");
prop.setProperty("password", "root");
//将集合中的内容写出到配置文件中
FileWriter fw = new FileWriter("config.properties");
prop.store(fw, "this is my config");
}
}


  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值