1、打印流-PrintStream 和 PrintWriter
1.1、一览图
打印流只有输出流,没有输入流
1.2、代码实现
package transformation;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
/**
* PrintWriter 使用方式
*/
public class PrintWriter_ {
public static void main(String[] args) throws IOException {
PrintWriter printWriter = new PrintWriter(System.out);
System.out.println(printWriter); // java.io.PrintWriter@5cad8086
PrintWriter printWriter1 = new PrintWriter(new FileWriter("e:\\f2.txt"));
System.out.println(printWriter1); // java.io.PrintWriter@6e0be858
}
}
package printstream;
import java.io.IOException;
import java.io.PrintStream;
/**
* PrintStream (字节打印流/输出流)
*/
public class PrintStream_ {
public static void main(String[] args) throws IOException {
PrintStream out = System.out;
// 在默认情况下, PrintStream 输出数据的位置是 标准输出, 即显示
out.print("john, hello\n");
/*
print 源码
public void print(String s) {
if (s == null) {
s = "null";
}
write(s);
}
*/
// 因为 print 底层使用的是 write, 所以我们可以直接调用 write 进行打印/输出
out.write("jack, 你好".getBytes());
out.close();
/*
我们可以去修改打印流输出的位置/设备
1. 输出修改成到 "e:\\f1.txt"
2. "hello, lily" 就会输出到 e:\f1.txt
3. public static void setOut(PrintStream out) {
checkIO();
setOut0(out); // native 方法, 修改了 out
}
*/
System.setOut(new PrintStream("e:\\f1.txt"));
System.out.println("hello, lily"); // 将信息写入文件
}
}
2、Properties 类
2.1、需求引出
2.2、基本介绍
2.3、应用案例
Properties01.java
package properties_;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
public class Properties01 {
public static void main(String[] args) throws IOException {
// 读取 mysql.properties 文件, 并得到 ip, user 和 pwd
BufferedReader br = new BufferedReader(new FileReader("e:\\mysql.properties"));
String line = "";
while ((line = br.readLine()) != null) { // 循环读取
String[] split = line.split("=");
// 如果我们要求指定的 ip 值
if ("ip".equals(split[0])) {
System.out.println(split[0] + "值是: " + split[1]);
}
}
br.close();
}
}
mysql.properties.java
ip=192.168.100.100
user=root
pwd=12345
package properties_;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Properties;
public class Properties02 {
public static void main(String[] args) throws IOException {
// 使用 Properties 类来读取 mysql.properties 文件
// 1. 创建 Properties 对象
Properties properties = new Properties();
// 2. 加载指定配置文件
properties.load(new FileReader("e:\\mysql.properties"));
// 3. 把 k-v 显示控制台
properties.list(System.out);
// 4. 根据 key 获取对应的值
String user = properties.getProperty("user");
String pwd = properties.getProperty("pwd");
System.out.println("用户名=" + user);
System.out.println("密码是=" + pwd);
}
}
控制台输出
-- listing properties --
user=root
pwd=12345
ip=192.168.100.100
用户名=root
密码是=12345
package properties_;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;
public class Properties03 {
public static void main(String[] args) throws IOException {
// 使用 Properties 类来创建 配置文件, 修改配置文件内容
Properties properties = new Properties();
// 创建
// 1.如果该文件没有 key 就是创建
// 2.如果该文件有 key 就是修改
/*
// Properties 父类是 Hashtable, 底层就是 Hashtable 核心方法
public synchronized V put(K key, V value) {
// Make sure the value is not null
if (value == null) {
throw new NullPointerException();
}
// Makes sure the key is not already in the hashtable.
Entry<?,?> tab[] = table;
int hash = key.hashCode();
int index = (hash & 0x7FFFFFFF) % tab.length;
@SuppressWarnings("unchecked")
Entry<K,V> entry = (Entry<K,V>)tab[index];
for(; entry != null ; entry = entry.next) {
if ((entry.hash == hash) && entry.key.equals(key)) {
V old = entry.value;
entry.value = value; // 如果 key 存在, 就替换
return old;
}
}
addEntry(hash, key, value, index); // 如果是新 k, 就 addEntry
return null;
}
*/
properties.setProperty("charset", "utf8");
properties.setProperty("user", "汤姆"); // 注意保存时, 是中文的 unicode 码值
properties.setProperty("pwd", "888888");
// 将 k-v 存储文件中即可
properties.store(new FileOutputStream("e:\\mysql2.properties"), null);
System.out.println("保存配置文件成功~");
}
}
控制台输出
#Sat Jan 08 21:33:44 CST 2022
user=\u6C64\u59C6
pwd=888888
charset=utf8