I/O框架

I/O框架

1. 流的概念

2. 流的分类

  • /**
    * 1: java.io 包下。
    * 2:i:input o:output
    * 3:流(stream)
    * 4:通过流:可以对文件实现读写操作。网络传输。
    * 5:流的分类:
    * 1:通过数据的流向
    * 输入流:将外部的数据流向程序内存。InputStream、
    * 输出流:将内存中数据写出到目的地。OutputStream
    * 2:通过处理的数据的单元
    * 字节流,以字节为单位来处理数据。能处理所有的数据。 FileInputStream
    * 字符流,以字符为单位处理数据。只能处理字符数据。但是底层还是字节流。FileReader
    * 字节流的功能更加的强大。
    * 字符流 处理字符数据 更加的简单,专业。
    *
    * 3:根据数据源不同分类
    * 节点流:直接和数据源相连的流。FileInputStream
    * 处理流:以其他的流为数据源的流。效率功能一般比节点流更强。BufferedInputStream。
    *
    */
  •  

3. File/FileFilter

package com.qf.io;

import java.io.File;
import java.io.FileFilter;
import java.io.IOException;
import java.util.Arrays;

/**
* java.io.File
* 1: 一个File对象 可以代表你磁盘上的一个文件,或者是一个目录。
* 2:通过File 对象,可以实现创建文件,创建目录,修改文件的属性,重命名文件。
* 不能读取和修改文件的内容。
*
*/
public class TestFile {

public static void main(String[] args) {
// couterFiles(new File("D:/java/3.文档资料"));
// deleteDir(new File("d:/77"));

test4();
}


// 思考:如何删除一个目录:只能删除文件和空目录。
private static void deleteDir(File delFile){
if(delFile.isFile()){
// 删除文件。
delFile.delete();
}else{
File[] files = delFile.listFiles();
for (int i = 0; i < files.length; i++) {
deleteDir(files[i]);
}
// 删除空目录。
delFile.delete();
}
}

// 获取到一个目录下的所有的满足某些规则的文件的名字
private static void test4(){
File file = new File("C:\\Program Files\\Java\\jdk1.8.0_161\\bin");
// 获取目录中,满足过滤器要求的内容。
File[] files = file.listFiles(new FileFilter(){
// 使用 accept 对所有的子文件进行过滤,如果返回true,代表满足需求。
@Override
public boolean accept(File file) {
return !file.getName().endsWith(".exe");
}
});

System.out.println(Arrays.toString(files));
}


private static void test() {
//在 d 盘的根目录下创建一个文件。1.txt,如果文件已经存在,删除了。如果不存在,创建。
File file = new File("D:/1.txt");
if(file.exists()){
file.delete();
}else{
try {
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
}

// 在d 盘的根目录下创建77/77/77
private static void test1() {
File file = new File("D:\\77\\77\\77");
if(!file.exists()){
file.mkdirs();//make directory

//创建文件。
File file2 = new File(file,"1.txt");
try {
file2.createNewFile();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

// 修改文件的属性。
private static void test3() {
//在 d 盘的根目录下创建一个文件。1.txt,如果文件已经存在,删除了。如果不存在,创建。
File file = new File("D:/1.txt");
if(!file.exists()){
try {
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
file.setLastModified(1000);
}

static int count;
// 统计一个目录中的所有的文件的个数。
private static void couterFiles(File file){
if(file.isFile()){
count ++;
}else{
// 目录。
// 获取到所有的子内容。
File[] files = file.listFiles();
// 递归处理每个子内容。
for (int i = 0; i < files.length; i++) {
couterFiles(files[i]);
}
}
}

}
package com.qf.io;

import java.io.File;
import java.io.IOException;

/**
* 路径:字符串。
* 1:绝对路径
* 一盘符开始的路径,绝对路径。
* 2:相对路径
* 相对于的路径是当前项目的根目录。
* . 代表了项目的根目录。
* .. 代表了项目的根目录的上一级目录。
*
* File.pathSeparator
* 多个目录之间的分隔符。;
* File.separator
* 子目录,父目录之间的分隔符。\
*
* 这两个变量是动态获取的。后去当前系统的不同的分隔符。如果考虑移植性,那么建议使用上述的两个变量。
*/
public class TestFile1 {

public static void main(String[] args) throws IOException {
// 绝对路径
File file = new File("d:/1.txt");
// 获取绝对路径。
// file.getAbsolutePath();

// 相对目录
File file1 = new File("res/1.txt");
file1.createNewFile();

// 当前目录
File file2 = new File("./res/2.txt");
file2.createNewFile();

File file3 = new File("../1.txt");
file3.createNewFile();

System.out.println(File.pathSeparator);
System.out.println(File.separator);
}

}

4. 字节流

  • InputStream/OutputStream

5. 字节节点流

  • FileInputStream/FileOutputStream

  • package com.qf.io;

    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.IOException;

    /**
    * java.io.InputStream
    * 1:是所有的字节输入流的父类。 2:抽象类。
    *
    * java.io.OutputStream
    * 1:是所有的字节输出流的父类。 2:抽象类。
    *
    * java.io.FileInputStream
    * 1: 是一个文件字节输入节点流。
    * 2:从数据源中读取数据到内存中。
    *
    * java.io.FileOutputStream
    * 1:是一个文件字节输出节点流。
    * 2:将内存中的数据写出到指定的数据源。
    *
    *
    */
    public class TestInputStream {

    public static void main(String[] args) {
    // test4();
    copy("c:/xu.jpg", "c:/newxu.jpg");
    }

    private static void test1() {
    try {
    FileInputStream fis = new FileInputStream("./res/1.txt");
    // 从数据源中读取下一个字节,并返回该字节对应整数。如果读取到流的末尾,返回-1.
    int value = fis.read();
    while (value != -1) {
    System.out.print((char) value);
    value = fis.read();
    }
    } catch (Exception e) {
    e.printStackTrace();
    }
    }

    private static void test2() {
    try {
    FileInputStream fis = new FileInputStream("./res/1.txt");
    byte[] buf = new byte[10];
    // 每次从数据源中读取若干个字节数据,保存到 字节数组中。返回本次读取到的字节的个数。
    // 读取到流的末尾返回-1
    int count;
    while ((count = fis.read(buf)) != -1) {
    // 使用读取到的字节数,还原字符串。
    String s = new String(buf, 0, count);
    System.out.print(s);
    }

    } catch (Exception e) {
    e.printStackTrace();
    }
    }

    private static void test3() {

    FileInputStream fis = null;
    try {
    fis = new FileInputStream("./res/1.txt");
    // 获取数据源中可以被读取到的字节数。
    int num = fis.available();
    // 让输入流跳过指定的字节数。
    // fis.skip(n);
    byte[] buf = new byte[num];
    // 每次从数据源中读取若干个字节数据,保存到 字节数组中。返回本次读取到的字节的个数。
    // 读取到流的末尾返回-1
    fis.read(buf);
    System.out.println(new String(buf));

    } catch (Exception e) {
    e.printStackTrace();
    } finally {
    // 流必须要关闭
    if (fis != null) {
    try {
    fis.close();
    } catch (IOException e) {
    e.printStackTrace();
    }
    }
    }
    }

    //
    private static void test4(){
    FileOutputStream fos = null;
    try {
    fos = new FileOutputStream("./res/2.txt");

    String str = "鹅鹅鹅,曲项向天歌。";
    byte[] buf = str.getBytes();

    fos.write(buf);
    } catch (Exception e) {
    e.printStackTrace();
    }finally{
    if(fos != null){
    try {
    fos.close();
    } catch (IOException e) {
    e.printStackTrace();
    }
    }
    }
    }


    private static void copy(String srcPath,String destPath){
    FileInputStream fis = null;
    FileOutputStream fos = null;
    try {
    fis = new FileInputStream(srcPath);
    // 如果文件不存在,底层会创建。
    fos = new FileOutputStream(destPath);

    byte[] buf = new byte[100];
    // 将数据源的数据读取到缓冲区。
    int count = fis.read(buf);
    while(count != -1){
    // 将读取到的数据源的数据,写出去。
    fos.write(buf, 0, count);

    count = fis.read(buf);

    // Thread.sleep(50);
    }
    } catch (Exception e) {
    e.printStackTrace();
    }finally {
    if(fos != null){
    try {
    fos.close();
    } catch (IOException e) {
    e.printStackTrace();
    }
    }
    // 流必须要关闭
    if (fis != null) {
    try {
    fis.close();
    } catch (IOException e) {
    e.printStackTrace();
    }
    }
    }
    }

    }
  • package com.qf.test;

    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.IOException;

    /**
    * 容器:
    * java.util.Collection
    * 元素的特点:无序,不唯一。
    * ---:List
    * 元素特点:有序(添加的顺序),不唯一
    * --- ArrayList:元素特点:有序(添加的顺序),不唯一
    * ---LinkedList:双向链表,有序(添加的顺序),不唯一
    * ---Set:
    * 元素特点:无序,唯一
    * --HashSet
    * 底层使用 HashMap 。使用了HashMap的key 部分。元素无序,唯一,可以有一个 null。
    * -- TreeSet
    * 底层使用二叉树。红黑树。平衡二叉树。
    * 要么使用内部比较器接口,要么使用外部的。
    * 元素,有序(默认升序),唯一。
    * --- Map
    * 映射,键值对数据。key+value。
    * key:无序,唯一。
    * value:无序,不唯一。
    * --HashMap
    * --key 的类型,需要重写 equals 和 hashCode。
    * --TreeMap
    * -- key 类型,要么实现内部比较器,要么指定外部的。
    *
    * Iterable
    * iterator()
    * Iterator
    * hasNext
    * next
    * remove
    * ---ListIterator
    * ---
    *
    * IO Stream
    * I:input
    * O:output
    *
    * 流:就是内存中数据的【通道】。数据:字节序列。通过流,可以对数据源进行读写操作。
    * 数据源:文件|流|网络连接。
    *
    * 输入输出流。
    * 字节字符流
    * 节点(处理流|包装流)
    *
    * InputStream:
    * 所有的字节输入流的父类,抽象类。 int read()
    * 所有的 xxxxInputStream都是它直接的或者间接的子类。
    * OutputStream:
    * 所有的字节输出流的父类,抽象类。 void write(int)
    * 所有的 xxxxOutputStream都是它直接的或者间接的子类。
    *
    * FileInputStream
    * FileOutputStream
    *
    */
    public class Test {

    public static void main(String[] args) {
    test();
    }

    private static void test() {
    FileInputStream fis = null;
    FileOutputStream fos = null;
    try {
    fis = new FileInputStream(new File("./res/1.txt"));
    // 一旦通道和目的数据建立连接,那么原有数据就会被清除。
    fos = new FileOutputStream("./res/2.txt",true);

    byte[] buf = new byte[10];

    int count = fis.read(buf);
    while(count != -1){
    //写出去
    fos.write(buf, 0, count);

    count = fis.read(buf);
    }
    } catch (Exception e) {
    e.printStackTrace();
    }finally {
    // 关闭信息通道。
    if(fis != null){
    try {
    fis.close();
    } catch (IOException e) {
    e.printStackTrace();
    }
    }
    if(fos != null){
    try {
    fos.close();
    } catch (IOException e) {
    e.printStackTrace();
    }
    }

    }
    }
    }
  •  

6. 字节过滤流

  • BufferedInputStream/BufferedOutputStream

  • package com.qf.io;

    import java.io.BufferedInputStream;
    import java.io.BufferedOutputStream;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileOutputStream;

    import com.qf.util.MyUtil;

    /**
    * BufferedInputStream
    * 带缓冲区的字节输入处理流。数据源是其他的流。
    * BufferedOutputStream
    * 带缓冲区的字节输出处理流。目的数据源是其他的流。
    *
    * 作用:提高流的读写的效率。
    * 注意:处理大文件适合,小文件效率更低。
    *
    * 处理流的关闭:
    * 只关闭处理流即可。在关闭处理的时候,会关闭和他关联的其他的流。
    *
    */
    public class TestBufferedStream {

    public static void main(String[] args) {
    test2();
    }

    private static void test1() {
    FileInputStream fis = null;
    FileOutputStream fos = null;
    try {
    fis = new FileInputStream(new File("./res/1.txt"));
    // 一旦通道和目的数据建立连接,那么原有数据就会被清除。
    fos = new FileOutputStream("./res/2.txt",true);

    byte[] buf = new byte[100];

    int count = fis.read(buf);
    while(count != -1){
    //写出去
    fos.write(buf, 0, count);

    count = fis.read(buf);
    }
    fos.flush();
    } catch (Exception e) {
    e.printStackTrace();
    }finally {
    // 关闭信息通道。
    MyUtil.closeAll(fis,fos);
    }
    }

    // 使用处理流文件复制
    private static void test2() {
    FileInputStream fis = null;
    FileOutputStream fos = null;

    BufferedInputStream bis = null;
    BufferedOutputStream bos = null;

    try {
    fis = new FileInputStream(new File("./res/s1.mp4"));
    bis = new BufferedInputStream(fis);
    // 一旦通道和目的数据建立连接,那么原有数据就会被清除。
    fos = new FileOutputStream("./res/s2.mp4");
    bos = new BufferedOutputStream(fos);

    byte[] buf = new byte[100];
    int count = bis.read(buf);

    while(count != -1){
    bos.write(buf, 0, count);
    count = bis.read(buf);
    }

    } catch (Exception e) {
    e.printStackTrace();
    }finally {
    // 关闭处理流即可。
    MyUtil.closeAll(bis,bos);
    }
    }
    }
  • ObjectInputStream/ObjectOutputStream

7. 对象序列化

package com.qf.io;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;

/**
* ObjectInputStream
* 对象输入流,读取字节数据还原为基本数据类型。
* ObjectOutputStream
* 对象输出流。还支持对所有的基本数据类型转换为字节数据。
* 作用:实现对象的序列化和反序列化。
*
* 对象的序列化:
* 将内存中的对象转换为字节数组的过程。
* 对象的反序列化:
* 将字节数组数据还原为内存中的对象的过程。
*
* 对象的序列化的注意问题
* 1:要序列的对象的类型必须要实现接口 java.io.Serializable
* 2: 该接口是一个标记型接口,没有字段和方法,仅仅代表了,序列的语义。
* 3:类通过实现 java.io.Serializable 接口以启用其序列化功能。
* 未实现此接口的类将无法使其任何状态序列化或反序列化。可序列化类的所有子类型本身都是可序列化的。
* 序列化接口没有方法或字段,仅用于标识可序列化的语义。
* 4: 对象反序列化的过程中,生成了内存中的对象,该对象的生成全部依赖于序列化的数据,并没有调用构造方法。
* 5:反序列化成功的要求:
* 1:必须有一个和序列化相同的类,包名+类名 必须一致。
* 2:jdk的版本需要一致。
* 3:序列化和反序列化的时候,两个类的 serialVersionUID 值必须一致。
* 需要在 序列化的类中显式的添加 serialVersionUID 并赋值。
* 4:一个对象可以序列化,那么该对象的所有的属性都是要求可以被序列化的。
*  
* 6:类中的哪些成员不能被序列化。
* 1:静态的。static 变量是不依赖对象的,对象的序列化和静态的属性没有关系。
* 2:transient 修饰的实例成员变量是不能被序列化的。
*
*
*/
public class ObjectStream {

public static void main(String[] args) throws Exception {
write();
read();
}

private static void read() throws Exception{
FileInputStream fis = new FileInputStream("./res/stu");
// 提高效率。可以不加
BufferedInputStream bis = new BufferedInputStream(fis);
ObjectInputStream ois = new ObjectInputStream(bis);
// 按照数据写入的顺序读取
int num = ois.readInt();
boolean bool = ois.readBoolean();
String utf = ois.readUTF();
Student stu = (Student)ois.readObject();

System.out.println(num);
System.out.println(bool);
System.out.println(utf);
System.out.println(stu);

ois.close();

}

// 序列化
private static void write() throws Exception {
FileOutputStream fos = new FileOutputStream("./res/stu");
BufferedOutputStream bos = new BufferedOutputStream(fos);
ObjectOutputStream oos = new ObjectOutputStream(bos);
oos.writeInt(10);
oos.writeBoolean(true);
oos.writeUTF("千万里我追寻着你");
oos.writeObject(new Student(20,"男","季程"));
oos.close();
}


}

class Student implements Serializable{
/**
*
*/
private static final long serialVersionUID = 1L;
private transient int age;
private String gender;
private String name;
private Book book = new Book();
public Student(int age, String gender, String name) {
super();
System.out.println(1);
this.age = age;
this.gender = gender;
this.name = name;
}
@Override
public String toString() {
return "Student [age=" + age + ", gender=" + gender + ", name=" + name + "]";
}
}

class Book implements Serializable{

/**
*
*/
private static final long serialVersionUID = 1L;

}

 

8. 字符流

  • Reader/Writer

9. 字符节点流

  • FileWirter/FileReader

  • package com.qf.io;

    import java.io.FileReader;
    import java.io.FileWriter;

    import com.qf.util.MyUtil;

    /**
    * Reader: 抽象类,是所有的字符输入流的父类。抽象类。 Writer: 抽象类,是所有的字符输出流的父类。抽象类。
    *
    * FileReader: 字符节点输入流。 作用:从文件中读取字符到内存中。 FileWriter: 字节节点输出流。
    * 作用:将内存中的字符数据写出到文件中。 FileReader 和 FileWriter 只能处理纯文本数据。
    *
    * 保存:将字符数据--->底层字节数据。(字符对应的整数的字节数据)。
    * 保存的过程:将字符数据--对应的当前字符集(UTF-8)中对应的整数的字节数据保存到硬盘上。 编码过程:encode。 字符--->字节。
    * 明文--->密文。 解码过程:decode 字节--->字符 密文--->明文 这两个过程中使用的字符集必须一致,不一致就会出现乱码。
    *
    */
    public class TestReaderWriter {

    public static void main(String[] args) {
    copyTextFile();
    }

    private static void copyTextFile() {
    // 解码。读取底层的字节数据,解码为字节数据(使用的字符集是平台默认的--GBK)
    // FileReader 中包含了一个解码器。负责读字节,转成字符。
    FileReader fr = null;
    // 编码。将内存字符数据,编码为字节数(使用的字符集是平台默认的--GBK)
    // FileWriter中包含了一个编码器。负责读字符,转字节。
    FileWriter fw = null;
    try {
    fr = new FileReader("res/3.txt");
    fw = new FileWriter("res/2.txt", true);

    char[] buf = new char[10];

    int count = fr.read(buf);
    while (count != -1) {
    fw.write(buf, 0, count);
    count = fr.read(buf);
    }
    } catch (Exception e) {
    e.printStackTrace();
    } finally {
    MyUtil.closeAll(fr, fw);
    // if(fr != null){
    // try {
    // fr.close();
    // } catch (IOException e) {
    // e.printStackTrace();
    // }
    // }
    // if(fw != null){
    // try {
    // fw.close();
    // } catch (IOException e) {
    // e.printStackTrace();
    // }
    // }
    }
    }
    }
  •  

10. 字符过滤流

  • BufferedWriter/BufferedReader

  • package com.qf.io;

    import java.io.BufferedReader;
    import java.io.BufferedWriter;
    import java.io.FileReader;
    import java.io.FileWriter;

    import com.qf.util.MyUtil;

    /**
    * 带缓冲区的字符流。缓冲区大小 8192 chars
    *
    * BufferedReader:字符输入流。
    * BufferedWriter:字符输出流。
    *
    * 适用于比较大的文本。
    *
    */
    public class TestBufferedReaderAndWriter {

    public static void main(String[] args) throws Exception {
    test();
    }

    // 文本的复制。
    private static void test() throws Exception{
    BufferedReader br = new BufferedReader(new FileReader("./res/3.txt"));
    BufferedWriter bw = new BufferedWriter(new FileWriter("./res/4.txt"));
    // br.read(cbuf)
    // 读取数据源中一行的数据。如果读取到末尾,返回 null
    String line = br.readLine();
    while(line != null){
    bw.write(line);
    // 输出一个当前系统的换行符。
    bw.newLine();
    bw.flush();
    // bw.write("\r\n"); 移植性不好
    line = br.readLine();
    }
    MyUtil.closeAll(br,bw);
    }
    }
  •  

  • PrintWriter

  • package com.qf.io;

    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.PrintStream;
    import java.io.PrintWriter;
    import java.util.Scanner;

    /**
    * PrintStream
    * 1:打印字节流。
    * 2:只有输出流,没有输入流。
    * 3:作用:支持将任意类型转换为字符串,然后再转换为字节数组输出到底层。
    * 4:字节流。依赖编码器,使用平台默认字符集。
    * 5: 该流不会抛出IO 异常。
    * 6: System.out 就是该类的一个实例。
    * 7: 该类还提供了 直接输出字节数据的方法。
    * PrintWrite
    * 1:打印字符流
    * 2:这个类和PrintStream 功能类似。
    * 3:但是没有提供输出基本字节数据的方法。
    *
    *
    */
    public class TestPrint {

    public static void main(String[] args) throws Exception{
    // testPrintWriter();
    test2();
    }

    // PrintStream应用的场景。
    private static void test2() throws Exception {
    try {

    } catch (Exception e) {
    // 产生异常的时候,将异常信息输出到指定的异常日志文件。
    e.printStackTrace(new PrintStream(new FileOutputStream("./res/file.log",true)));
    }
    }

    // 修改 System.in
    private static void test1() throws Exception{
    // 修改System.in
    System.setIn(new FileInputStream("res/1.txt"));

    Scanner scanner  =new Scanner(System.in);
    String nextLine = scanner.nextLine();
    System.out.println(nextLine);
    }

    // 一道面试题。
    private static void test() {
    // 在控制台输出 x = 10
    int x = 10;
    PrintStream ps = new PrintStream(System.out){
    @Override
    public void println(int x) {
    println("x = " +x);
    }
    };

    System.setOut(ps);

    System.out.println(x);

    }


    //PrintStream的基本用法。
    private static void testPrintStream() throws Exception{
    PrintStream ps = new PrintStream(new FileOutputStream("res/4.txt",true));
    // 修改 System .out 对象。
    System.setOut(ps);

    ps.println(1.1);
    ps.println(true);
    ps.println(10);
    ps.println('A');
    ps.println(new Object());
    // ps.write(b);

    System.out.println("hello");

    ps.close();
    }

    // PrintWriter的基本用法。
    private static void testPrintWriter() throws Exception{
    PrintWriter pw = new PrintWriter(new FileOutputStream("res/5.txt",true));

    pw.println(1.1);
    pw.println(true);
    pw.println(10);
    pw.println('A');
    pw.println(new Object());
    // pw.write(b);
    pw.close();
    }
    }
  •  

11. 字符节点流

  • InputStreamReader

  • package com.qf.io;

    import java.io.BufferedReader;
    import java.io.FileInputStream;
    import java.io.InputStreamReader;

    /**
    * InputStreamReader
    * 1:这是一个字符流。
    * 2: InputStreamReader 以字节流为数据源,返回的是字符数据。俗称解码器。
    * 所有的字符输入流的解码工作都靠该类完成的。
    * public class InputStreamReaderextends ReaderInputStreamReader
    * 是字节流通向字符流的桥梁:它使用指定的 charset 读取字节并将其解码为字符。
    * 它使用的字符集可以由名称指定或显式给定,或者可以接受平台默认的字符集。
    * 每次调用 InputStreamReader 中的一个 read() 方法都会导致从底层输入流读取一个或多个字节。
    * 要启用从字节到字符的有效转换,可以提前从底层流读取更多的字节,使其超过满足当前读取操作所需的字节。
    * 为了达到最高效率,可要考虑在 BufferedReader 内包装 InputStreamReader。
    * 例如: BufferedReader in = new BufferedReader(new InputStreamReader(System.in));

    */
    public class TestInputStreamReader {

    public static void main(String[] args)  throws Exception{
    test1();
    }

    private static void test() throws Exception{
    // 使用的平台的默认的字符集。
    InputStreamReader isr = new InputStreamReader(new FileInputStream("res/1.txt"));
    BufferedReader br = new BufferedReader(isr);

    System.out.println(br.readLine());

    br.close();

    }

    private static void test1() throws Exception{
    // 使用的指定的字符集进行解码。
    InputStreamReader isr = new InputStreamReader(new FileInputStream("res/6.txt"),"utf-8");
    BufferedReader br = new BufferedReader(isr);

    System.out.println(br.readLine());

    br.close();

    }
    }
  •  

  • OutputStreamWriter

  • package com.qf.io;

    import java.io.BufferedReader;
    import java.io.BufferedWriter;
    import java.io.FileOutputStream;
    import java.io.InputStreamReader;
    import java.io.OutputStreamWriter;

    import com.qf.util.MyUtil;

    /**
    * OutputStreamWriter
    * 1:字符输出流。
    * 2:转换流,将字符数据转换为字节数据输出到底层。
    * 3:所有的字符输出流,将字符数据转换为字节数据输出到底层的工作都靠该类完成。
    * 4:称为【编码器】
    * 5:吃的字符,吐出去的是字节。
    * 6:将某些字符串按照指定的字符集输出成字节数据。使用该类。
    *
    */
    public class TestOutputStreamWriter {

    public static void main(String[] args) throws Exception{
    // 接收键盘输入,将输入的内容,写到一个文本文件中,要求使用utf-8编码输出字节数据。
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

    BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("res/7.txt"),"utf-8"));

    while(true){
    String line = br.readLine();
    bw.write(line);
    bw.newLine();
    bw.flush();
    // System.out.println(line);
    if("bye".equals(line))
    break;
    }

    MyUtil.closeAll(br,bw);
    }

    }
  •  

12. Properties

 

  • FileReader|FileWriter

  • BufferedInputStream|BufferedOutputStream

  • BufferedReader|BufferedWriter

  • ObjectInputStream|ObjectOutputream

  • PrintStream

  • PrintWriter

  • InputStreamReader

  • OutputStreamWriter

posted @   ITboy搬砖人  阅读(13)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 25岁的心里话
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现
点击右上角即可分享
微信分享提示