1.缓冲流:
缓冲流概述:
缓冲流也称为高效流,或者高级流。(字节流,字符流可以称为原始流(基础流))。
作用:
可以提高原始字节流,字符流读写数据的性能。(底层自带了一个长度为8192的数组。)
字节缓冲流:
/** * 字节缓冲流是依赖于原始流的,读写数据的方法和原始流是一样的。 */ public class Demo1 { public static void main(String[] args) throws IOException { FileInputStream fis=new FileInputStream("daye10_io\\src\\com\\itheima\\buffered_io\\美女.jpg"); BufferedInputStream bis=new BufferedInputStream(fis); BufferedOutputStream bos=new BufferedOutputStream(new FileOutputStream("daye10_io\\src\\com\\itheima\\buffered_io\\美女1.jpg")); byte[] bytes=new byte[1024]; int len; while((len=bis.read(bytes))!=-1){ bos.write(bytes,0,len); } //自动关闭字节流。 bis.close(); bos.close(); } }
字符缓冲流:
/** * 字符缓冲流特有的方法: * readLine():读取一行并返回,如果没有读取到则返回null。 * newLine():写入一个换行符。 */ public class Demo2 { public static void main(String[] args) throws IOException { method01(); method02(); } private static void method01() throws IOException { FileReader fr=new FileReader("daye10_io\\src\\com\\itheima\\buffered_io\\古诗.txt"); BufferedReader br=new BufferedReader(fr); FileWriter fw=new FileWriter("daye10_io\\src\\com\\itheima\\buffered_io\\古诗1.txt"); BufferedWriter bw=new BufferedWriter(fw); char[] chars=new char[1024]; int len; while ((len=br.read(chars))!=-1){ bw.write(chars,0,len); } br.close(); bw.close(); } private static void method02() throws IOException { BufferedReader br=new BufferedReader(new FileReader("daye10_io\\src\\com\\itheima\\buffered_io\\古诗.txt")); BufferedWriter bw=new BufferedWriter(new FileWriter("daye10_io\\src\\com\\itheima\\buffered_io\\古诗2.txt")); String line; while ((line=br.readLine())!=null){ bw.write(line); /** * 这个方法具备跨平台性,在Windows系统中换行符是\r\n 在Linux系统中换行符\n 在Mac系统中换行符是\r */ bw.newLine(); } br.close(); bw.close(); } }
案例:
/**
* 读取出师表文件的每一行,排序后,写到另一个文件中去。
*/
public class Demo3 {
public static void main(String[] args) throws IOException {
// 1.使用BufferedReader字符输入流对象,读取,每一行。
BufferedReader br=new BufferedReader(new FileReader("daye10_io\\src\\com\\itheima\\buffered_io\\出师表.txt"));
// 2.创建一个list集合,用于存储读取到的每一行。
ArrayList<String> list=new ArrayList<>();
String line;
while ((line=br.readLine())!=null){
list.add(line);
}
br.close();
// 3.对List集合按照每一行的首字符编号排序。
ArrayList<String> orders=new ArrayList<>();
Collections.addAll(orders,"一","二","三","四","五","陆","柒","八","九","十","十一");
Collections.sort(list, new Comparator<String>() {
@Override
public int compare(String o1, String o2) {
/**
* .表示任意字符。
* \\.表示.字面的含义。(特殊字符前面加\\表示它原来的含义)
*/
String[] order1 = o1.split("\\.");
String str1 = order1[0];
String[] order2 = o2.split("\\.");
String str2 = order2[0];
return orders.indexOf(str1)-orders.indexOf(str2);
}
});
// 4.把排序好的每一行,用BufferedWriter写到目标文件中去。
BufferedWriter bw=new BufferedWriter(new FileWriter("daye10_io\\src\\com\\itheima\\buffered_io\\出师表1.txt"));
for (String s : list) {
bw.write(s);
bw.newLine();
}
bw.close();
}
}
2.转换流:
字符集:
- 字符:字母,汉字等。
- 字符编码:把字符转换为0,1的过程。
- 字符集: 多个(字符—编码)的对应关系放在一起,就是字符集(编码表)
- ASCII字符集:包含字母,数字,标点符号,控制字符,每一个字符占1个字节。
- GBK字符集:兼容ASCII,还包含中文,1个字母占1个字节;一个汉字占2个字节。
- Unicode字符集的UTF-8编码方案:兼容ASCII字符集,还包含世界上所有国家的文字。1个字母占1个字节,1个汉字占3个字节。
乱码问题层出不穷。各个国家各自为政,都在搞自己的字符集,这也是乱码问题最重要的原因之一,非常不利于国际之间的交流。
字符输入转换流:
InputSteamReader,可以把原始的字节流按照指定编码转换成字符输入流
/** * 字符流在读取文件时,可能会存在乱码问题。 * 原因: * Java默认使用的编码方案是UTF-8,它默认只能读取UTF-8编码格式的文件 * 如果使用FileReader读取GBK格式的文件,就会乱码。 */ public class Demo1 { public static void main(String[] args) throws IOException { InputStreamReader isr=new InputStreamReader(new FileInputStream("daye10_io\\src\\com\\itheima\\charset\\出师表.txt"),"GBK"); BufferedReader br=new BufferedReader(isr); String line; while ((line=br.readLine())!=null){ System.out.println(line); } br.close(); } }
字符输出转换流:
OuputSteamWriter,可以把原始的字节流按照指定编码转换成字符输出流
public class Demo2 { public static void main(String[] args) throws IOException { OutputStreamWriter osw=new OutputStreamWriter(new FileOutputStream("daye10_io\\src\\com\\itheima\\charset\\GBK.txt"),"GBK"); BufferedWriter bw=new BufferedWriter(osw); bw.write("你好世界"); bw.newLine(); bw.write("HelloWorld"); bw.close(); } }
/** * 将GBK编码的文本内容保存到UTF-8编码的文本内容。 */ public class Demo3 { public static void main(String[] args) throws IOException { BufferedReader br=new BufferedReader(new InputStreamReader(new FileInputStream("daye10_io\\src\\com\\itheima\\charset\\UTF-8.txt"),"GBK")); BufferedWriter bw=new BufferedWriter(new OutputStreamWriter(new FileOutputStream("daye10_io\\src\\com\\itheima\\charset\\GBK.txt"),"UTF-8")); String line; while ((line=br.readLine())!=null){ bw.write(line); bw.newLine(); } br.close(); bw.close(); } }
3.对象流:
/** * 注意点: * 1.被序列化的类,必须实现一个Serializable * 2.序列化和反序列化,序列号必须保持一致,建议写一个固定的序列号。 * private static final long serialVersionUID=123456789L; */ public class Demo1 { public static void main(String[] args) throws IOException, ClassNotFoundException { /** * ObjectOutputSteam序列化Student对象(写对象) */ //1.创建ObjectOutputSteam流对象,用于往文件中写对象。 ObjectOutputStream oos=new ObjectOutputStream(new FileOutputStream("daye10_io\\src\\com\\itheima\\object_io\\Demo1.txt")); //2.writeObject写对象到文件中 Student student=new Student("小红",20); oos.writeObject(student); //3.关闭流 oos.close(); System.out.println("--------------------------------------"); /** * ObjectInputStream反序列化Student对象(读对象) */ ObjectInputStream ois=new ObjectInputStream(new FileInputStream("daye10_io\\src\\com\\itheima\\object_io\\Demo1.txt")); //2.readObject读对象的方法 Student student1= (Student) ois.readObject(); System.out.println(student1); } }
/** * 注意:只有实现序列化接口后,才可以序列化和反序列化。(Serializable) */ public class Student implements Serializable { private String name; private int age; public Student(String name, int age) { this.name = name; this.age = age; } public Student() { } @Override public String toString() { return "Student{" + "name='" + name + '\'' + ", age=" + age + '}'; } //写一个固定的序列号。 private static final long serialVersionUID=123456789L; }
public class Demo1 { public static void main(String[] args) throws IOException, ClassNotFoundException { //1.创建ObjectOutputSteam流对象,用于往文件中写对象。 ObjectOutputStream oos=new ObjectOutputStream(new FileOutputStream("daye10_io\\src\\com\\itheima\\object_io\\Demo1.txt")); //2.writeObject写对象到文件中 Student student1=new Student("小红",20); Student student2=new Student("小白",21); Student student3=new Student("小黑",23); Student student4=new Student("小放",26); ArrayList<Student> students=new ArrayList<>(); students.add(student1); students.add(student2); students.add(student3); students.add(student4); oos.writeObject(students); //3.关闭流 oos.close(); System.out.println("--------------------------------------"); ObjectInputStream ois=new ObjectInputStream(new FileInputStream("daye10_io\\src\\com\\itheima\\object_io\\Demo1.txt")); //2.readObject读对象的方法 ArrayList<Student> studentArrayList = (ArrayList<Student>) ois.readObject(); studentArrayList.forEach(student -> System.out.println(student)); } }
4.打印流:
/** * 扩展:我们之前用到的打印输出语句,其实底层就是PrintStream * System.out.println("hello"); * *标准输入流和标准输出流 * System.in :标准的输入流,用来读取控制台的数据。 * System.out:标准的输出流,用来往控制台打印数据。 */ public class Demo { public static void main(String[] args) throws IOException { //1.创建PrintStream或者PrintWriter流对象 PrintStream ps=new PrintStream("daye10_io\\src\\com\\itheima\\print_io\\Demo.txt"); //2.写数据 // ps.write("你好世界".getBytes()); ps.print('a'); ps.print("hello"); ps.print(97); ps.print(3.14); ps.print(true); ps.print(new Student("小白",100)); // 3.关闭流 ps.close(); System.out.println("-------------------------------------"); InputStream in = System.in; //把字节流转换为字符流 InputStreamReader isr = new InputStreamReader(in); BufferedReader br = new BufferedReader(isr); String str = br.readLine(); System.out.println(str); str = br.readLine(); System.out.println(str); str = br.readLine(); System.out.println(str); System.out.println("---------------"); //默认往控制台打印 System.out.println("helloworld"); //修改打印语句的目的地 System.setOut(new PrintStream("daye10_io\\a.txt")); System.out.println("你好世界"); } }
Properties:
- 其实就是一个Map集合,但是我们一般不会当集合使用,因为HashMap更好用。
- Properties一般用于读,写 配置文件(通过修改配置文件,控制程序的运行状态)
public class Demo1 { public static void main(String[] args) throws IOException { //Properties是一个双列集合,属于Map下面的子类。 Properties pro=new Properties(); //添加键值对 //注意:Properties集合键和值一般都是String类型,所以添加键和值时,建议使用setProperty方法。 pro.setProperty("zhangsan","20"); pro.setProperty("lisi","30"); //把Properties集合中的键和值,写到配置文件中。 pro.store(new FileWriter("daye10_io\\src\\com\\itheima\\properties_list\\Demo1.txt"),null); System.out.println("---------------------------------------------------------------------------"); Properties pro1=new Properties(); //读取配置文件 pro1.load(new FileReader("daye10_io\\src\\com\\itheima\\properties_list\\Demo1.txt")); System.out.println(pro1); } }
IO框架:
commons-io概述
commons-io是apache开源基金组织提供的一组有关IO操作的类库,可以提高开发的效率。
commons-io工具包提供了许多有关io操作的类。主要的类FileUtils
public class Demo1 {
public static void main(String[] args) throws IOException {
//1.FileUtils.readFileToString():读取文件中的数据,返回字符串。
String gbk = FileUtils.readFileToString(new File("daye10_io\\出师表.txt"), "GBK");
System.out.println(gbk);
//2.FileUtils.copyFile():复制文件。
FileUtils.copyFile(new File("daye10_io\\美女.jpg"),new File("daye10_io\\美女1.jpg"));
//3.FileUtils.copyDirectoryToDirectory():复制文件夹。
FileUtils.copyDirectoryToDirectory(new File("daye10_io"),new File("D:\\a"));
}
}