标准输入、输出流
转换流-InputStreamReader 和 OutputStreamWriter
- 1,InputStreamReader:Reader的子类,可以将InputStream(字节流)包装成(转换)为Reader(字符流)
- 2,OutputStreamWriter:Writer的子类,实现将OutputStream(字节流)包装成(转换)为Writer(字符流)
- 3,当处理纯文本数据时,如果使用字符流效率更高,并且可以有效解决中文问题,所以建议将字节流转换成字符流
- 4,可以在使用指定编码格式(比如:utf-8,gdk,gb2312,ISO8859-1)
//演示使用 InputStreamReader 转换流解决中文乱码问题
//将字节流 FileInputStream 转成字符流 InputStreamReader, 指定编码 gbk/utf-8
public class InputStreamReader_ {
public static void main(String[] args) throws IOException {
String filePath = "d:\\a.txt";
//1. 把 FileInputStream 转成 InputStreamReader
//2. 指定编码 gbk
// InputStreamReader isr = new InputStreamReader(new FileInputStream(filePath),"gbk");
//3. 把 InputStreamReader 传入 BufferedReader
// BufferedReader br = new BufferedReader(isr);
//将 2 和 3 合在一起
BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(filePath)));
//4. 读取
System.out.println(br.readLine());
//5. 关闭外层流
br.close();
}
}
案例:演示OutputStreamWriter 使用
//演示:OutputStreamWriter 使用
//将FileOutStream 字节流,转成字符流 OutputStreamWriter
//指定处理的编码 gbk/utf-8/utf8
public class OutputStreamWriter_ {
public static void main(String[] args) throws IOException {
String filePath = "d:\\zl.txt";
String charSet = "gbk";
OutputStreamWriter outputStreamWriter = new OutputStreamWriter(new FileOutputStream(filePath),charSet);
BufferedWriter bufferedWriter = new BufferedWriter(outputStreamWriter);
bufferedWriter.write("hi,帅哥");
bufferedWriter.close();
System.out.println("按照"+charSet+"完成");
}
}
打印流-PrintStream 和 PrintWriter
打印流只有输出流,没有输入流
- 演示 PrintStream (字节打印流/输出流)
import java.io.IOException;
import java.io.PrintStream;
//演示 PrintStream (字节打印流/输出流)
public class PrintStream_ {
public static void main(String[] args) throws IOException {
////标准输出,System的out方法是PrintStream类型
PrintStream out = new PrintStream(System.out);
// PrintStream out = System.out;
out.print("hello");
//因为 print 底层使用的是 write , 所以我们可以直接调用 write 进行打印/输出
out.write("你好".getBytes());
out.close();
//我们可以去修改打印流输出的位置/设备
PrintStream out1 = new PrintStream("d:\\hello.txt");
//public static void setOut(PrintStream out) {
// checkIO();
// setOut0(out); // native 方法,修改了 out
// }
System.setOut(out1);
System.out.println("hello");
}
}
- //演示 PrintWriter 使用方式 输入(字符打印流)
//演示 PrintWriter 使用方式 输入(字符打印流)
public class PrintWriter_ {
public static void main(String[] args) throws FileNotFoundException {
//标准输出
PrintWriter printWriter1 = new PrintWriter(System.out);
//设置输出位置
PrintWriter printWriter = new PrintWriter("d:\\帅哥.txt");
printWriter.print("帅哥");
printWriter.close();
}
}
Properties 类
基本介绍
- 1,专门用于读写配置文件的集合类
配置文件的格式:
键=值
键=值 - 2,注意:键值对不需要有空格,值不需要引号引起来,默认类型为String
- 3,Properties的常见方法
load:加载配置文件的键值对到Properties
list:将数据显示到指定设备
getProperty(key):根据键获取值
setProperty(key,value):设置键值对到Properties对象
store:将Properties中的键值对存储到配置文件,在idea中,保存信息到配置文件,如果有中文,会存储为unicode码
http://tool.chinaz.com/tools/unicode.aspx unicode码查询工具
public class Properties1 {
public static void main(String[] args) throws IOException {
//使用 Properties 类来读取 mysql.properties 文件
//1,创建 Properties 对象
Properties properties = new Properties();
//2,加载指定配置文件
properties.load(new FileReader("src\\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);
//使用 Properties 类来创建 配置文件, 修改配置文件内容
Properties properties1 = new Properties();
//创建
//1.如果该文件没有 key 就是创建
//2.如果该文件有 key ,就是修改
properties1.setProperty("charset", "utf8");
properties1.setProperty("user", "汤姆");//注意保存时,是中文的 unicode 码值
properties1.setProperty("pwd", "888888");
//将 k-v 存储文件中即可
properties1.store(new FileOutputStream("src\\mysql2.properties"), null);
System.out.println("保存配置文件成功~");
}
}