hooyantsing's Blog

第36次课程

字数统计: 689阅读时长: 3 min
2019/07/13

源辰74班

第36次课程

07.13.2019

**内容    **

Java_API_项目案例[蒋斌]

**1.**将字节流(InputStream)包装成字符流(InputStreamReader)

Reader/InputStreamReader/FileReader关系

InputStreamReader:是字节流通向字符流的桥梁。

b8bc4efbdce28721f5e446e7723a477e.png

BufferedReader 内包装 InputStreamReader

1
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));

案例分析:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
package course.pm;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

public class Demo1 {

    public static void main(String[] args) throws IOException {
        //字节流
        InputStream in = new FileInputStream("网络端口.txt");
        //将字节流(InputStream)包装成字符流(InputStreamReader)
        InputStreamReader inputStreamReader = new InputStreamReader(in);
        BufferedReader br = new BufferedReader(inputStreamReader);
        char[] ch = new char[1024];
        br.readLine();
        int len = -1;
        StringBuffer sb = new StringBuffer();
        while((len=br.read(ch))!=-1) {
            sb.append(new String(ch));
        }
        System.out.println(sb.toString());
    }
}

**2.**DataInputStream和DataOutputStream(数据流)

案例分析:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
package course.pm;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

public class Demo2 {

    public static void main(String[] args) throws IOException {
        File f = new File("2.txt");
        OutputStream out = new FileOutputStream(f);
        //将字节流包装成数据流
        DataOutputStream dataout = new DataOutputStream(out);
        dataout.writeInt(100);
        dataout.writeBoolean(true);
        //和FileWriter一样需要刷新
        dataout.flush();
        InputStream in = new FileInputStream(f);
        DataInputStream dataIn = new DataInputStream(in);
        //获取文件中int值
        int readInt = dataIn.readInt();
        System.out.println(readInt);
        //获取文件中的Boolean值
        boolean bool = dataIn.readBoolean();
        System.out.println(bool);
    }
}

3.ObjectInputStream和****ObjectOutputStream(对象流)

案例分析:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
package course.pm;
import java.io.*;
public class Demo3 {
    public static void main(String[] args)  throws IOException, ClassNotFoundException {
       Person p = new Person("张三",18);
       ObjectOutputStream out = new  ObjectOutputStream(new  FileOutputStream("3.txt"));
       //将对象p写入
       out.writeObject(p);
       out.flush();
       
       ObjectInputStream in = new  ObjectInputStream(new  FileInputStream("3.txt"));
       Person inp =  (Person)in.readObject();
       System.out.println(inp.toString());
    }
}
//对象
class Person implements Serializable{ //必须实现Serializable(序列化)接口,才能使用对象流
    private String name;
    private transient int age;  //transient  反序列化  在写入文件时不可写入
    public Person(String name, int age) {
       super();
       this.name = name;
       this.age = age;
    }
    @Override
    public String toString() {
       return "Person [name=" + name + "]";
    }
}

**4.**Properties类(配置文件)

Properties 继承于 Hashtable。

案例分析:本案例将在控制台输出

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
package course.pm;

import java.util.*;
import java.util.Map.Entry;

public class Demo4 {

    public static void main(String[] args) {
        //获取系统的Properties信息
        Properties properties = System.getProperties();
        //由于Properties继承于Hashtable,因此也是这种类型。
        Set<Entry<Object,Object>> entrySet = properties.entrySet();
        Iterator<Entry<Object,Object>> iterator = entrySet.iterator();
        while(iterator.hasNext()) {
            Entry<Object,Object> entry = iterator.next();
            System.out.println(entry.getKey()+"  "+entry.getValue());
        }
    }
}

案例分析:本案例将生成文件

<本案例未进行整理>

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
package course.pm;

import java.io.*;
import java.util.Properties;

public class Demo5 {

    public static void main(String[] args) throws IOException {
        OutputStream out = new FileOutputStream("config.properties");
        Properties properties = System.getProperties();
        properties.store(out, "配置文件");
        InputStream in = new FileInputStream("config.properties");
        Properties p2 = new Properties();
        p2.load(in);
        System.out.println(p2.toString());
    }
}
CATALOG