转换流
当你需要读取一个文本文件的时候,你的编译器定义的编码格式,如果与文本文件保存的数据格式不相同的话,那么读取出来的数据就是乱码。
这时,你就可以使用转换流来解决这个问题。只有转换流才能指定读取和写入的字符集
相当于给流带了一个面具,让字节流变成了字节流
InputStreamReader类
InputStreamReader:字节字符转换输入流,将字节输入流转换为字符输入流
代码实现
public class InputStreamReaderDemo {
public static void main(String[] args) throws IOException {
//1.实例化File的对象
//File file = new File("file/input1.txt");
//2.实例化转换输入流的对象
//注意:当一个流的存在的意义是为了实例化另外一个流,则这个流不需要手动进行关闭
//InputStream input = new FileInputStream(file);
//InputStreamReader reader = new InputStreamReader(input);
//使用默认的字符集【GBK】进行实例化转换流
//InputStreamReader reader = new InputStreamReader(new FileInputStream(new File("file/input1.txt")));
//使用指定字符集进行实例化转换流
//字符集一般使用字符串直接传参,不区分大小写,但是,如果字符集书写有误的话,则会跑出java.io.UnsupportedEncodingException
InputStreamReader reader = new InputStreamReader(new FileInputStream(new File("file/input1.txt")),"UTF-8");
//3.读取
char[] arr = new char[16];
int len = 0;
while((len = reader.read(arr)) != -1) {
String string = new String(arr, 0, len);
System.out.println(string);
}
reader.close();
}
}
OutputStreamWriter类
OutputStreamWriter:字符转换输出流,将内存中的字符转成字节保存到硬盘中。
代码实现
public class OutputStreamWriterDemo {
public static void main(String[] args) throws IOException {
//需求:将一段文本以utf-8的格式写入到文件中【注,文件格式为默认格式】
//1.实例化FIle对象
//注意:对于所有的输出流而言,文件可以不存在,在进行写入的过程中可以自动进行创建
//但是,对于所有的输入流而言,文件必须先存在,然后才能操作,否则,会抛出FileNotFounedException
File file = new File("file/output1.txt");
//2.实例化转换输出流
//如果不想覆盖源文件中的内容时,则在传参的时候,设置一个参数为true
OutputStreamWriter writer = new OutputStreamWriter(new FileOutputStream(file,true), "utf-8");
//3.写入
writer.write("太难了,真的是太难了");
//4.刷新
writer.flush();
//5.关闭
writer.close();
}
}
缓冲流
作用:为了增强基础流的功而存在,提高了流的工作效率【读写效率】
注意:如果使用记事本创建的文件,文件是utf-8或者unicode编码,文件的前面有一个BOM(Byte Order Mark)头,BOM作用指定文件使用的编码类型。GBK编码没有添加bom头。
utf-8:EF BB BF
unicode 小端: FF FE 66 00
unicode 大端 :FE FF 00 66
相当于给给流穿了一件衣服,使数据传输的更快
BufferedInputStream类
字节缓冲流(读取)
public class BufferedInputStreamDemo {
public static void main(String[] args) throws IOException {
//实例化一个File对象
File file = new File("file/test.txt");
//实例化一个缓冲字节输入流的对象
BufferedInputStream input = new BufferedInputStream(new FileInputStream(file));
//读取
byte[] arr = new byte[1024];
int len = 0;
while((len = input.read(arr)) != -1) {
String string = new String(arr, 0, len);
}
//打印
System.out.println(string);
//关闭资源
input.close();
}
}
BufferOutputStream类
字节缓冲流(写入)
public class BufferedOutputStreamDemo {
public static void main(String[] args) throws IOException {
//实例化FIle对象
File file = new File("test33.txt");
//实例化换种字节输出流
BufferedOutputStream output = new BufferedOutputStream(new FileOutputStream(file));
//写
output.write("helloworld".getBytes());
//刷新
output.flush();
//关闭
output.close();
}
}
BufferedReader类
字符缓冲流
public class BufferedReaderDemo {
public static void main(String[] args) throws IOException {
//实例化FIle对象
File file = new File("test33.txt");
//实例化缓冲字符流的对象
BufferedReader reader = new BufferedReader(new FileReader(file));
//方式一:read循环读取
/*
//读取
char[] arr = new char[8];
int len = 0;
while((len = reader.read(arr)) != -1) {
String string = new String(arr, 0, len);
}
*/
//方式二:readLine循环读取
/*
String result1 = reader.readLine();
System.out.println(result1);
String result2 = reader.readLine();
System.out.println(result2);
*/
//一次读取一行,循环读取所有的数据
//(result = reader.readLine()) != null
//注意条件是 不等于 null
String result = "";
while((result = reader.readLine()) != null) {
System.out.println("第一行:" + result);
}
reader.close();
}
}
BufferedWriter类
字节缓冲流
public class BufferedWriterDemo {
public static void main(String[] args) throws IOException {
// 实例化FIle对象
File file = new File("test33.txt");
//实例化缓冲字符输出流
BufferedWriter writer = new BufferedWriter(new FileWriter(file,true));
// 写
writer.write("今天天气还可以");
// 作用:主要就是为了换行
writer.newLine();
// 刷新
writer.flush();
//关闭
writer.close();
}
}