写入字符流类的典型继承体系树
Writer(java.io) (绝大部分都会抛出 IOException)
|--BufferedWriter:提高流的操作效率,也就是装饰设计模式
主要方法:1.public BufferedWriter?(Writer out);//将需要被提高效率的流对象作为参数传递给缓冲区的该构造函数;
2.public void close() throws IOException;//.class文件中有该方法,但是API中没有该方法!关闭的是和该对象保定的流,不用再关闭输出流;
3.public void newLine() throws IOException;//写入一个换行符,跨平台;
4.public void write?(String s, int off, int len) throws IOException;//将字符串写入指定文件中。
|--LineNumberReader:带行号的装饰类。
主要方法:1.public int getLineNumber();//获取当前行号,默认从0开始;
2.public void setLineNumber?(int lineNumber);//设置起始行号。
|--OutputStreamWriter:字符流转成字节流的桥梁,方便将字符流转成字节流并输出到屏幕上。
主要方法:1.构造方法:public OutputStreamWriter?(OutputStream out);
2.构造方法:public OutputStreamWriter?(OutputStream out, String charsetName) throws UnsupportedEncodingException;//明确解码表
|--FileWriter:操作字符流的便捷类,底层使用的还是字节流。
主要方法:1.构造方法:public FileWriter?(String fileName) throws IOException;:在指定目录下创建并覆盖名为fileName的文件,编码使用的是计算机的默认编码表;
2.构造方法:public FileWriter?(String fileName, boolean true) throws IOException;:在已有文件的末尾添加字符;
3.public void write?(char[] cbuf, int off, int len) throws IOException;//将字符数组中的指定字符写入输入流中;
4.public void flush() throws IOException;//将流中的数据刷新到缓冲区中。
|--PrintWriter:字符打印流,使用其中的方法可以更加方便的进行文件的输出。
主要方法:1.构造方法:public PrintWriter?(Writer out, boolean autoFlush);//当autoFlush为true时,会启动自动刷新缓冲区功能。则仅当调用println,printf或format方法之一时
才执行此操作。
2.public void println?(String x);//会自动添加换行符,开启自动刷新缓冲区功能时会刷新缓冲区。
读取字符流类的典型继承体系树
Reader
|--BufferedReader:提高流的操作效率
主要方法:1.构造方法:public BufferedReader?(Reader in);//将需要被提高效率的流对象作为参数传递给缓冲区的该构造函数;
2.public String readLine() throws IOException;/读取一行,不包含换行符,读取完毕后返回null。
|--InputStreamReader:字节流转成字符流的桥梁,主要是为了将系统键盘录入的字节流转换成字符流,方便操作。
主要方法:1.构造方法:public InputStreamReader?(InputStream in);
2.构造方法:public InputStreamReader?(InputStream in, String charsetName) throws UnsupportedEncodingException;//明确转换的编码表
|--FileReader:
主要方法:1.构造方法:public FileReader?(String fileName) throws FileNotFoundException;:取出指定目录下名为fileName的文件;
2.public int read?(char[] cbuf, int offset, int length) throws IOException;:将文件读取到cbuf数组中。当读取完毕后返回-1;
写入字节流类的典型继承体系树
OutputStream
|--FileOutputStream:文件输出流
主要方法:1.构造方法:public FileOutputStream?(String name, boolean append) throws FileNotFoundException;//append为true时在已有文件结尾处添加数据;
2.public void write?(byte[] b, int off, int len) throws IOException;//将字节数组写入到指定的文件中;
|--FilterOutputStream
|--BufferedOutputStream:提高输入字节流效率并提供一些方法
主要方法:1.构造方法:public BufferedOutputStream?(OutputStream out);
2.public void write?(byte[] b, int off, int len) throws IOException;
|--PrintStream:方便输出操作。
读取字节流类的典型继承体系树
InputStream
|--FileInputStream:文件读取流
|--FilterInputStream
|--BufferedInputStream:提高效率
流操作的基本规律:
1.明确源和目的:
源:输入流,InputStream Reader
目的:输出流 OutputStream Writer
2.操作的数据是否是纯文本:
是,字符流;
不是,字节流;
3.明确要使用哪个具体的对象。
测试系统默认使用的编码表
package second.study; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; public class Test { public static void main(String[] args) throws IOException { write(); } public static void write() throws IOException { BufferedReader bufr = new BufferedReader(new InputStreamReader(System.in));//标准键盘输入 BufferedWriter bufw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("Res1.txt"),"utf-8"));//可以理解为字符传入标准写法。 String str = null; while((str = bufr.readLine()) != null) { if("over".contentEquals(str)) { break; } bufw.write(str); bufw.newLine(); } bufw.close(); } }
原文:https://www.cnblogs.com/star-491849224/p/12510253.html