打印流以及Properties

打印流以及Properties

字节打印流

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
import java.io.*;
import java.nio.charset.StandardCharsets;

/**
* @author zss
*/
public class Test {
public static void main(String[] args) throws IOException {
PrintStream out=System.out;
//默认打印在显示器上
out.println("join,hello");
//out底层调用的是write,所以可以直接调用write进行输出
out.write("你好啊".getBytes(StandardCharsets.UTF_8));
//我们也可以更改打印的位置
System.setOut(new PrintStream("D:\\文件下载\\hello.txt"));
//在文件中显示
System.out.println("hello");
out.close();
}
}

字符打印流

1
2
3
4
5
6
7
8
9
10
11
12
13
import java.io.*;
import java.nio.charset.StandardCharsets;

/**
* @author zss
*/
public class Test {
public static void main(String[] args) throws IOException {
PrintWriter printWriter=new PrintWriter(new FileWriter("D:\\文件下载\\hello.txt"));
printWriter.println("你好啊");
printWriter.close();
}
}

Properties

传统方法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
import java.io.*;

/**
* @author zss
*/
public class Test {
public static void main(String[] args) throws IOException {
BufferedReader bufferedReader=new BufferedReader( new FileReader("src\\Information.properties"));
String line;
while ((line=bufferedReader.readLine())!=null){
String []split=line.split("=");
System.out.println(split[0]+"的值"+split[1]);
}
bufferedReader.close();
}
}

image-20220703171201137

使用Properties类

专门用于读写配置文件的集合类,配置文件的格式:

键=值,键值对不需要有空格,值不需要用引号,默认为String类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
import java.io.*;
import java.util.Properties;

/**
* @author zss
*/
public class Test {
public static void main(String[] args) throws IOException {
//创建Properties类来读取文件
Properties properties=new Properties();
//加载指定配置的文件
properties.load(new FileReader("src\\Information.properties"));
//KV显示在控制台
properties.list(System.out);
//根据key获取相应的值
String user= properties.getProperty("user");
System.out.println(user);
//修改相应的文件
properties.setProperty("password","55555");
properties.list(System.out);

}
}

image-20220703171313929

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
import java.io.*;
import java.util.Properties;

/**
* @author zss
*/
public class Test {
public static void main(String[] args) throws IOException {
//创建Properties类来读取文件
Properties properties=new Properties();
properties.setProperty("password","55555");
properties.setProperty("user","zss");
properties.setProperty("id","11");
//注释效果
properties.store(new FileOutputStream("src\\test.properties"),"hello");

}

}

image-20220703171913787

练习

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
33
34
35
36
37
38
39
40
41
42
43
44
import java.io.*;
import java.util.Properties;

/**
* @author zss
*/
public class Test {
public static void main(String[] args) throws IOException, ClassNotFoundException {
Properties properties=new Properties();
properties.load(new FileReader("src\\test.properties"));
Employee employee=new Employee(properties.getProperty("user"),Integer.parseInt(properties.getProperty("id")), properties.getProperty("password") );
System.out.println(employee);
ObjectOutputStream objectOutputStream=new ObjectOutputStream(new FileOutputStream("D:\\文件下载\\test.dat"));
objectOutputStream.writeObject(employee);
objectOutputStream.close();
ObjectInputStream objectInputStream=new ObjectInputStream(new FileInputStream("D:\\文件下载\\test.dat"));
Employee employee1=(Employee) objectInputStream.readObject();
System.out.println(employee1);
objectInputStream.close();

}
}
class Employee implements Serializable{
String user;
int id;
String password;

public Employee(String user, int id, String password) {
this.user = user;
this.id = id;
this.password = password;
}

@Override
public String toString() {
return "Employee{" +
"user='" + user + '\'' +
", id=" + id +
", password='" + password + '\'' +
'}';
}
}


image-20220703171940795