IO操作——字符流

来源:互联网 发布:asap2020软件 64bit 编辑:程序博客网 时间:2024/06/10 20:31
一.Reader,Writer是所有字符输入流与字符输出流的父类

二.字符转换流

1.InputStreamReader(fos)——>字符输入流

package day05;import java.io.FileInputStream;import java.io.IOException;import java.io.InputStreamReader;/** * 转换流 * InputStreamReader * @author soft01 * */public class InputStreamReader_read {public static void main(String[] args) throws IOException {FileInputStream fis = new FileInputStream("osw.txt");InputStreamReader isr = new InputStreamReader(fis,"utf-8");/*char[] data = new char[100];int len = isr.read(data);String str = new String(data,0,len);System.out.println(str);*/StringBuilder builder = new StringBuilder();String str = "";int d =-1;while((d=isr.read())!=-1){builder.append((char)d);}str = builder.toString();System.out.println(str);isr.close();}}

2.OutputStreamWriter(fos,"utf-8");——>字符输出流

package day05;import java.io.FileOutputStream;import java.io.IOException;import java.io.OutputStreamWriter;/** * java根据读写数据的单位划分了:字节流,字符流 * InputStream,OutputStream是所有字节输入流与 * 字节输出流的父类,常用实现类:FileInputStream, * BufferedInputStream等。 *  * Reader,Writer是所有字符输入流与字符输出流的父类 *  * 字节流的读写单位是字节,而字符流的读写单位是字符。 * 所以字符流有局限性,只适合读写字符数据。 * 但实际字符流底层本质还是读写字节,只是字符与字节 * 的转换工作不用我们来做了。 *  * 转换流 * OutputStreamWriter,InputStreamReader * @author soft01 * */public class OutputStreamWriter_write {public static void main(String[] args) throws IOException {FileOutputStream fos = new FileOutputStream("osw.txt");/* * 构造方法若只传入流,那么通过当前转换流写出的字符 * 会按照系统默认的字符集转化为对应的字节,不推荐。 * 可以使用重载的构造方法,在第二个参数中明确使用的字符集。 */OutputStreamWriter osw = new OutputStreamWriter(fos,"utf-8");String str = "linsa";osw.write(str);osw.write("hello");System.out.println("写出完毕!");osw.close();}}


package day05;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.OutputStreamWriter;import java.io.PrintWriter;import java.io.UnsupportedEncodingException;import java.util.Scanner;/** * 简易记事本 * 使用PW将用户输入的每行字符串写入用户指定的文件中 * 构造方法使用流连接形式,不使用直接对文件操作的。 * @author soft01 * */public class Note {public static void main(String[] args) throws UnsupportedEncodingException, FileNotFoundException {Scanner scan = new Scanner(System.in);String fileName,line;System.out.println("请输入一个文件名:");fileName = scan.nextLine();FileOutputStream fos = new FileOutputStream(fileName);OutputStreamWriter osw = new OutputStreamWriter(fos,"utf-8");/* * 当PrintWriter的构造方法第一个参数为流(字节流,字符流均可)时, * 那么支持一个重载的构造方法可以传入一个boolean值,该值若为true, * 则当前PrintWriter具有自动行刷新功能 * 即:每当调用println方法写出一行字符串后 * 会自动调用flush方法将其真实写出。 * 需要注意,调用print方法是不会flush的。 */PrintWriter pw = new PrintWriter(osw,true);System.out.println("请输入要写入的数据:");do {line = scan.nextLine();if(line.equals("exit")) {break;}pw.println(line);}while(true);scan.close();pw.close();}}

三. 缓冲字符输出流——>PrintWriter(fileName)
PrintWriter pw = new PrintWriter(osw);
pw.println("How are you?");

package day05;import java.io.FileNotFoundException;import java.io.PrintWriter;import java.io.UnsupportedEncodingException;/** * 缓冲字符流 * 缓冲字符流由于内部有缓冲区,读写字符的效率高。 * 并且字符流的特点是可以按行读写字符串。 * BufferedWriter,BufferedReader *  * PrintWriter也是缓冲区字符输出流,它内部总是连接 * BufferedWriter,除此之外PW还提供了自动刷新功能,所以更常用。 * @author soft01 * */public class PrintWriter_println {public static void main(String[] args) throws FileNotFoundException, UnsupportedEncodingException {/* * PrintWriter提供了直接对文件进行写操作的构造方法: * PrintWriter(File file) * PrintWriter(String fileName) * 若希望按照指定的字符集向文件写出字符串, * 可以使用对应重载的构造方法: * PrintWriter(File file,String csn) * PrintWriter(String fileName,String csn) * 第二个参数可以指定字符集的名字(charSetName) */PrintWriter pw = new PrintWriter("pw.txt","utf-8");pw.println("hello");pw.println("monky");System.out.println("写出完毕!");pw.close();}}

package day05;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.OutputStreamWriter;import java.io.PrintWriter;import java.io.UnsupportedEncodingException;/** * PrintWriter提供了常规的构造方法,允许传入 * 一个字节流或者字符流完成流连接的创建形式 * @author soft01 * */public class PrintWriter_println2 {public static void main(String[] args) throws UnsupportedEncodingException, FileNotFoundException {FileOutputStream fos = new FileOutputStream("pw2.txt");/* * 若希望指定字符集,需要自行连接转换流 * 因为转换流可以将字符按照指定的字符集转换为字节 */OutputStreamWriter osw = new OutputStreamWriter(fos,"utf-8");PrintWriter pw = new PrintWriter(osw);/* * PW的构造方法允许直接传入字节流, * 但实际内部还是会根据流连接最终变为PW的。 * 只是这样做不能指定字符集。 */pw.println("How are you?");pw.println("I'm fine, Thank you.");pw.close();}}

四. 缓冲字符输入流——>BufferedReader(isr)

package day05;import java.io.BufferedReader;import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.IOException;import java.io.InputStreamReader;/** * java.io.BufferedReader * 缓冲字符输入流。特点:可以按行读取字符串 * 由于有缓冲,读取字符时效率好 * @author soft01 * */public class BufferedReader_readLine {public static void main(String[] args) throws IOException {/* * ./src/main/java/day05/BufferedReader_readLine.java */FileInputStream fis = new FileInputStream("src"+File.separator+"main"+File.separator+"java"+File.separator+"day05"+File.separator+"BufferedReader_readLine.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();}}

阅读全文
0 0
原创粉丝点击