Java SE核心API(10) —— 文件数据IO操作(字符流、转换流、缓冲字符流)

版权声明:本文为博主原创文章,遵循GPL开源协议精神,转载请注明出处。 https://blog.csdn.net/Robot__Man/article/details/80443882

一、字符流

1.1 字符流原理

  java.io.Reader是字符输入流的顶级父类;java.io.Writer是字符输出流的顶级父类。
  字符流是以字符(char)为单位读写数据的,一次处理一个unicode。字符流都是高级流,虽然以字符为单位读写数据,但是实际底层还是读写字节,只是从字节到字符的转换工作交给了字符流来完成。

1.2 Reader的常用方法

  int read(),读取一个字符,返回的int值,“低16位”有效。
  int read(char[] chs),从该流中读取一个字符数组的length个字符并存入该数组,返回值为实际读取到的字符量。

1.3 Writer的常用方法

  void write(int c),写出一个字符,写出给定int值“低16位”表示的字符。
  void write(char[] chs),将给定字符数组中所有字符写出。
  void write(String str),将给定的字符串写出。
  void write(char[] chs,int offset,int len),将给定的字符数组中从offset处开始连续的len个字符写出。


二、转换流

  OutputStreamWriter:字符输出流,使用该流可以设置字符集,并按照指定的字符集将字符转换为对应字节后通过该流写出。
  InputStreamReader:字符输入流,使用该流可以设置字符集,并按照指定的字符集从流中按照该编码将字节数据转换为字符并读取。

2.1 字符转换输出流

package day08;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;

/**
 * 转换流
 * java.io.OutputStreamWriter
 * 特点是可以按照指定的字符集写出字符 
 * 
 * 之所以称OutputStreamWriter与InputStreamReader为转换流,是因为大多数
 * 的字符流都只处理其他字符流,而低级流又是字节流,这就导致字符流不能处理字节流的问题,
 * 转换流相当于是一个转换器的作用。他们可以将字节流先转变为字符流,这样其他的字符流
 * 就可以处理了。
 * 
 * @author xxx
 *
 */
public class OSWDemo {
    public static void main(String[] args) throws IOException {
        FileOutputStream fos = new FileOutputStream("osw.txt");
        OutputStreamWriter osw = new OutputStreamWriter(fos, "utf-8");

        osw.write("我爱北京天安门");
        osw.write("天安门上太阳升");

        System.out.println("写出完毕!");
        osw.close();
    }
}

2.2 字符转换输入流

package day08;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;

/**
 * java.io.InputStreamReader
 * 字符输入流,可以按照给定的字符集读取字符
 * @author xxx
 *
 */
public class ISRDemo {
    public static void main(String[] args) throws IOException {
        FileInputStream fis = new FileInputStream("osw.txt");
        InputStreamReader isr = new InputStreamReader(fis,"utf-8");

        int d = -1;
        while ((d = isr.read()) != -1) {
            System.out.println((char)d);
        }

        isr.close();
    }
}

三、缓冲字符流

3.1 缓冲字符输出流

package day08;

import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;


/**
 * 缓冲字符流
 * BufferedWriter,BufferedReader
 * 特点是可以按行读写字符串
 * 
 * java.io.PrintWriter
 * 具有自动行刷新的缓冲字符输出流
 * 创建PW时,它一定会在内部创建BufferedWriter作为缓冲功能的叠加
 * 
 * @author xxx
 *
 */
public class PWDemo1 {
    public static void main(String[] args) throws FileNotFoundException, UnsupportedEncodingException{
        /*
         * 提供了多种构造方法
         * 其中有两个可以直接对文件进行写出操作的构造方法:
         * PrintWriter(File file)
         * PrintWriter(String path)
         */
        PrintWriter pw = new PrintWriter("pw.txt","UTF-8");

        pw.println("好好学习");
        pw.println("天天向上");

        System.out.println("写出完毕!");
        pw.close();
    }
}
package day08;

import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.FileNotFoundException;
import java.io.UnsupportedEncodingException;

/**
 * PrintWriter也提供了可以处理其他流的构造方法
 * 提供的方法可以传入字节流,也可以处理字符流,并且,当使用这类构造方法时,可以再传入
 * 第二个参数,该参数为boolean值,该值为true时,则具有了自动行刷新功能。
 * @author xxx
 *
 */
public class PWDemo2 {
    public static void main(String[] args) throws FileNotFoundException, UnsupportedEncodingException {
        FileOutputStream fos = new FileOutputStream("pw1.txt");

        OutputStreamWriter osw = new OutputStreamWriter(fos, "UTF-8");

        PrintWriter pw = new PrintWriter(osw);

        pw.println("哈哈");
        pw.println("嘿嘿");
        System.out.println("写出完毕!");
        pw.close();
    }
}
package day08;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.util.Scanner;
import java.util.concurrent.LinkedTransferQueue;


/**
 * 完成记事本功能
 * 要求:
 * 程序启动后,要求用户输入一个文件名,然后创建该文件,之后提示用户开始输入内容,
 * 并将用户输入的每一行内容都写入到该文件。
 * 当用户输入“exit”时,退出程序。
 * @author xxx
 *
 */
public class Note {
    public static void main(String[] args) throws FileNotFoundException, UnsupportedEncodingException {
        System.out.println("请输入文件名:");
        Scanner scan = new Scanner(System.in);
        String filename = scan.nextLine();

        FileOutputStream fos = new FileOutputStream(filename);
        OutputStreamWriter osw = new OutputStreamWriter(fos, "UTF-8");
        PrintWriter pw = new PrintWriter(osw,true);

        System.out.println("请输入内容:");
        String line = null;
        while (true) {
            line = scan.nextLine();
            if ("exit".equals(line)) {
                System.out.println("再见!");
                break;
            }
            /*
             * 若PrintWriter具有自动行刷新功能,那么每当调用println方法后就会自动flush。
             */
            pw.println(line);
            //pw.flush();
        }

        scan.close();
        pw.close();
    }
}

3.2 缓冲字符输入流

package day08;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;

/**
 * java.io.BufferedReader
 * 缓冲字符输入流,其内部提供了缓冲区,可以提高读取效率,特点:按行读取字符串。
 * @author xxx
 *
 */
public class BRDemo {
    public static void main(String[] args) throws IOException {
        FileInputStream fis = new FileInputStream("src/day08/BRDemo.java");
        InputStreamReader isr = new InputStreamReader(fis);
        BufferedReader br = new BufferedReader(isr);

        /*
         * BufferedReader提供了按行读取方法:
         * String readLine()
         * 连续读取若干字符,直到读取到换行符为止,并将换行符之间读取的字符以一个字符串返回。
         * 若返回值为NULL,则表示读取到末尾。
         * 注意:该字符串不包含最后的换行符。
         */
        String line = null;
        while ((line = br.readLine()) != null) {
            System.out.println(line);
        }

        br.close();
    }
}

猜你喜欢

转载自blog.csdn.net/Robot__Man/article/details/80443882