字符流
1. 字符流概述
字节流操作中文不方便,所有用字符流
- 字符流 = 字节流 + 编码表
字节流复制文本中文的时候为什么没有问题?因为底层最终进行字符拼接
如何识别中文? 无论哪种编码,第一个字节是负数
2. 编码解码
编码解码规则:采用何种方式编码,就要采用对应方式解码,否则乱码
默认UTF-8
public class Demo {
public static void main(String[] args) throws IOException {
//默认的都是UTF-8
String s = "中国";//[-28, -72, -83, -27, -101, -67](utf-8)
// byte[] bys = s.getBytes();
// System.out.println(Arrays.toString(bys));
byte[] bys = s.getBytes("GBK");
System.out.println(Arrays.toString(bys));//[-42, -48, -71, -6]
String ss = new String(bys, "GBK");
System.out.println(ss);//中国
}
}
3. 字符流中的编码解码问题
字符流中两个抽象基类
- Reader:字符输入流抽象基类
- Writer:…
字符流编码解码相关类
- InputStreamReader
- OutputStreamWriter
public class Demo {
public static void main(String[] args) throws IOException {
OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("java.txt"), "GBK");
osw.write("中国");
osw.close();
InputStreamReader isr = new InputStreamReader(new FileInputStream("java.txt"), "GBK");
//一次读出一个字节
int by;
while ((by = isr.read()) != -1) {
System.out.println((char) by);
}
isr.close();//
//中
//国
}
}
java.txt 文本中因为默认是utf-8,所以是乱码;而用GBK解码所以正常读取
4. 字符流写数据5种方式
- 写一个字符
-
一次写入一个字符数组
-
写入字符数组一部分
-
写一个字符串
-
写入字符串一部分
5. 字符流读数据2种方式
用法和字节流是一样的
案例:java文件的复制(简介版)
如果需要编码或者解码,老实写长的那种
简版:FileReader和FileWriter
6. 字符缓冲流
字符缓冲流:
- BufferedWriter:将文本写入字符输出流,缓冲字符,以提供单个字符,数组和字符串的高效写入,可以指定缓冲区大小,或者可以接受默认大小。默认值足够大,可用于大多数用途
- BufferedReader:从字符输入流读取文本,缓冲字符,以提供字符,数组和行的高效读取,可以指定缓冲区大小,或者可以使用默认大小。默认值足够大,可用于大多数用途
构造方法:
- BufferedWriter(Writer out)
- BufferedReader(Reader in)
public class Demo {
public static void main(String[] args) throws IOException {
// BufferedWriter bw = new BufferedWriter(new FileWriter("java.txt"));
// bw.write("hello\r\n");
// bw.write("world\r\n");
// bw.close();
BufferedReader br = new BufferedReader(new FileReader("java.txt"));
// int by;
// while ((by = br.read())!=-1){
// System.out.print((char) by);
// //hello
// //world
// }
char[] chs = new char[1024];
int len;
while ((len = br.read(chs)) != -1) {
System.out.print(new String(chs, 0, len));//不熟练
//hello
//world
}
}
}
字符数组输出不熟练
案例:java文件复制(字符缓冲流)
package file;
import java.io.*;
public class Demo {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new FileReader("CopyDemo.java"));
BufferedWriter bw = new BufferedWriter(new FileWriter("CopyDemo1.java"));
//一次读写一个字符
/*int ch;
while (((ch = br.read())!=-1)){
bw.write(ch);
}*/
//一次读写一个字符数组
char[] chs = new char[1024];
int len;
while ((len = br.read(chs)) != -1) {
bw.write(chs, 0, len);
}
br.close();
bw.close();
}
}
7. 字符缓冲流的特有功能
BufferedWriter:
void newLine()
:写一行行分隔符,行分隔符字符串由系统属性定义(可以使用不同的系统)
BufferedReader:
public String readLine()
:读一行文字。结果包含行的内容的字符串,不包括任何行终止字符,如果流的结尾已经到达,则为null
package file;
import java.io.*;
public class Demo {
public static void main(String[] args) throws IOException {
BufferedWriter bw = new BufferedWriter(new FileWriter("java.txt"));
for (int i = 0; i < 10; i++) {
bw.write("hello" + i);
bw.newLine();
bw.flush();
//hello0
//hello1
//hello2
//hello3
//hello4
//hello5
//hello6
//hello7
//hello8
//hello9
}
bw.close();
}
}
public class Demo {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new FileReader("java.txt"));
String line;
while ((line = br.readLine()) != null) {
System.out.println(line);
}
}
}
案例:java文件复制(字符流缓冲特有功能)
最常用的方式!
有手就行
8. IO流小结!!!!
![在这里插入图片描述](https://img-blog.csdnimg.cn/35a94599eb344907aaaa521833e74e40.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA5aSn5qaC5piv54qs6Z2S,size_20,color_FFFFFF,t_70,g_se,x_16
案例:集合到文件
package file;
import java.io.*;
import java.util.ArrayList;
public class Demo {
public static void main(String[] args) throws IOException {
//创建集合
ArrayList<String> arrayList = new ArrayList<>();
arrayList.add("wen");
arrayList.add("tian");
arrayList.add("sheng");
//创建字符缓冲输出对象
BufferedWriter bw = new BufferedWriter(new FileWriter("arrays.txt"));
for (String s : arrayList) {
bw.write(s);
bw.newLine();
bw.flush();
}
bw.close();
}
}
案例:文件到集合
有手就行
package file;
import java.io.*;
import java.util.ArrayList;
public class Demo {
public static void main(String[] args) throws IOException {
//创建对象
BufferedReader br = new BufferedReader(new FileReader("arrays.txt"));
ArrayList<String> arrayList = new ArrayList<>();
//读取
String line;
while ((line = br.readLine()) != null) {
arrayList.add(line);
}
//遍历
for (String s : arrayList) {
System.out.println(s);
}
//wen
//tian
//sheng
br.close();
}
}
案例:集合到文件(Student类)
package file;
import java.io.*;
import java.util.ArrayList;
public class Demo {
public static void main(String[] args) throws IOException {
//创建对象
ArrayList<Student> arrayList = new ArrayList<Student>();
Student s1 = new Student("cczu001", "wts", 20, "江苏");
Student s2 = new Student("cczu002", "wen", 21, "南京");
Student s3 = new Student("cczu003", "tian", 22, "上海");
Student s4 = new Student("cczu004", "sheng", 23, "广州");
arrayList.add(s1);
arrayList.add(s2);
arrayList.add(s3);
arrayList.add(s4);
BufferedWriter bw = new BufferedWriter(new FileWriter("java.txt"));
//存入
for (Student s : arrayList) {
StringBuilder sb = new StringBuilder();
sb.append(s.getSid()).append(',').append(s.getName()).append(',').append(s.getAge()).append(',').append(s.getAddress());
bw.write(sb.toString());
bw.newLine();
bw.flush();
}
bw.close();
}
}
案例:文件到集合(Student类)
package file;
import java.io.*;
import java.util.ArrayList;
public class Demo {
public static void main(String[] args) throws IOException {
//创建对象
BufferedReader br = new BufferedReader(new FileReader("java.txt"));
ArrayList<Student> arrayList = new ArrayList<Student>();
String line;
while ((line = br.readLine()) != null) {
String[] strArr = line.split(",");//利用了这个 ctrl+alt+v自动生成前面
Student s = new Student();
s.setSid(strArr[0]);
s.setName(strArr[1]);
s.setAge(Integer.parseInt(strArr[2]));//字符串转换成证书类型
s.setAddress(strArr[3]);
arrayList.add(s);
}
//遍历集合
for (Student ss : arrayList) {
System.out.println(ss.getSid() + ',' + ss.getName() + ',' + ss.getAge() + ',' + ss.getAddress());
}
//cczu001,wts,20,江苏
//cczu002,wen,21,南京
//cczu003,tian,22,上海
//cczu004,sheng,23,广州
}
}
案例:输入学生成绩传入到文件中(集合到文件)
package file;
import java.io.*;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.Scanner;
import java.util.TreeSet;
public class Demo {
public static void main(String[] args) throws IOException {
TreeSet<Student> treeSet = new TreeSet<>(new Comparator<Student>() {
@Override
public int compare(Student s1, Student s2) {
//总分数从高到低
int num = s2.getsum() - s1.getsum();
//次要条件
int num2 = num == 0 ? s2.getChinese() - s1.getChinese() : num;
int num3 = num2 == 0 ? s2.getMath() - s1.getMath() : num2;
int num4 = num3 == 0 ? s2.getEnglish() - s1.getEnglish() : num3;
int num5 = num4 == 0 ? s2.getName().compareTo(s1.getName()) : num4;
return num5;
}
});
//键盘录入5个学生成绩
for (int i = 0; i < 5; i++) {
Scanner sc = new Scanner(System.in);
System.out.println("正在录入第" + (i + 1) + "个学生成绩:");
System.out.print("姓名:");
String name = sc.nextLine();
System.out.print("语文成绩:");
int chinese = sc.nextInt();
System.out.print("数学成绩:");
int math = sc.nextInt();
System.out.print("英语成绩:");
int english = sc.nextInt();
//创建学生对象,录入类的成员变量种
Student s = new Student();
s.setName(name);
s.setChinese(chinese);
s.setMath(math);
s.setEnglish(english);
treeSet.add(s);
}
//创建数据缓冲流
BufferedWriter bw = new BufferedWriter(new FileWriter("java.txt"));
//遍历结合,写入
for (Student ss : treeSet) {
StringBuilder sb = new StringBuilder();
sb.append(ss.getName()).append(',').append(ss.getChinese()).append(',').append(ss.getMath()).append(',').append(ss.getEnglish()).append(',').append(ss.getsum());
//写入
bw.write(sb.toString());
bw.newLine();
;
bw.flush();
}
bw.close();
}
}
案例:复制单级文件夹
package file;
import java.io.*;
public class Demo {
public static void main(String[] args) throws IOException {
//创建数据源对象
File srcfile = new File("G:\\Program\\java_basic_learning\\itcast");
//获取数据源文件夹名字
String strname = srcfile.getName();
//创建目的地对象
File destFolderName = new File("G:\\Program\\java_basic_learning\\src", strname);
//判断目的地是否存在
if (!destFolderName.exists()) {
destFolderName.mkdir();
}
//获取数据源所有文件
File[] listFiles = srcfile.listFiles();
for (File f : listFiles) {
//获取名称
String destname = f.getName();
//创建目的地文件对象,注意路径
File destfile = new File(destFolderName, destname);
copyFile(f, destfile);
}
}
private static void copyFile(File file, File destfile) throws IOException {
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(destfile));
byte[] bys = new byte[1024];
int len;
while ((len = bis.read(bys)) != -1) {
bos.write(bys, 0, len);
}
bis.close();
bos.close();
}
}