Reader
是输⼊字符流的⽗类,它是⼀个抽象类, 部分库不推荐使⽤Reader/Writer
| int read() |
| ⼀个字符⼀个字符的读,只能⽤来操作⽂本(不能写图⽚ ⾳频 视频) |
| int read(char cbuf[]) |
| 从输⼊字符流中读取⼀定数量的字符,并将其存储在缓冲区数组cbuf中, 返回实际读取的字符数,如果已经到达流末尾⽽没有可⽤的字节,则返回-1 |
| void close() throws IOException |
| 关闭输⼊流并释放与该流关联的系统资源 |
| |
| public FileReader(String fileName) throws FileNotFoundException { |
| super(new FileInputStream(fileName)); |
| } |
| |
| public FileReader(File file) throws FileNotFoundException { |
| super(new FileInputStream(file)); |
| } |
| public class ReaderTest { |
| |
| public static void main (String [] args) throws Exception { |
| test1(); |
| test2(); |
| } |
| |
| public static void test1()throws Exception { |
| String dir = "C:\\Users\\79466\\Desktop\\test\\1.txt"; |
| File file = new File(dir); |
| |
| Reader input = new FileReader(file); |
| int ch; |
| |
| while ( (ch = input.read())!=-1){ |
| System.out.print((char)ch); |
| } |
| input.close(); |
| } |
| |
| public static void test2()throws Exception { |
| String dir = "C:\\Users\\79466\\Desktop\\test\\1.txt"; |
| File file = new File(dir); |
| Reader input = new FileReader(file); |
| char c[] = new char[1024]; |
| |
| int len = input.read(c); |
| |
| System.out.println("内容为:"+ new String(c,0,len)); |
| input.close(); |
| } |
| |
| } |
| public void write(int c) throws IOException |
| 直接将int型数据作为参数的话,是不会写⼊数字的,⽽是现将数字按照ascll码表转换为相应的字符,然后写⼊ |
| public void write(String str) throws IOException |
| 要想实现数字和中⽂的写⼊,必须要⽤String为参数的Write |
| public abstract void write(char cbuf[], int off, int len) throws IOException; |
| 将cbuf字符数组的⼀部分写⼊到流中,但能不能写len个字符取决于cbuf中是否有那么多 |
| void flush() throws IOException |
| write是写到缓冲区中,可以认为是内存中,当缓冲区满时系统会⾃动将缓冲区的内容写⼊⽂件,但是⼀般还有⼀部分有可能会留在内存这个缓冲区中, 所以需要调⽤flush空缓冲区数据。对⽐BufferWriter需要实时查表,效率低,其实缓冲区IO的各个都有,只不过很⼩被忽略,OutputStream都有flush⽅法,看⼦类是否有重写 |
| void close() throws IOException |
| 关闭输⼊流并释放与该流关联的系统资源 |
| public FileWriter(String fileName) throws IOException |
| 如果⽂件不存在,这会⾃动创建。如果⽂件存在,则会覆盖 |
| public FileWriter(File file) throws IOException |
| 如果⽂件不存在,这会⾃动创建。如果⽂件存在,则会覆盖 |
| public FileWriter(String fileName, boolean append) throws IOException |
| 加⼊true参数,会实现对⽂件的续写,使⽤false则会实现对⽂件的覆盖 |
| public FileWriter(File file, boolean append) throws IOException |
| 加⼊true参数,会实现对⽂件的续写,使⽤false则会实现对⽂件的覆盖 |
| public class WriterTest { |
| |
| public static void main(String [] ags)throws Exception{ |
| test1(); |
| test2(); |
| } |
| |
| public static void test2() throws IOException { |
| String dir = "C:\\Users\\79466\\Desktop\\test\\2.txt"; |
| |
| FileWriter writer = new FileWriter(dir,true); |
| writer.write(23567); |
| writer.write(28404); |
| writer.write(35838); |
| writer.write(22530); |
| writer.write("23567"); |
| writer.flush(); |
| writer.close(); |
| } |
| |
| public static void test1() throws IOException { |
| String dir = "C:\\Users\\79466\\Desktop\\test\\2.txt"; |
| Writer writer = new FileWriter(dir); |
| writer.write(23567); |
| writer.write(28404); |
| writer.write(35838); |
| writer.write(22530); |
| writer.write("23567"); |
| writer.flush(); |
| writer.close(); |
| } |
| |
| } |
| 当BufferedReader在读取⽂本⽂件时,会先尽量从⽂件中读⼊字符数据并放满缓冲区,⽽之后若使⽤read()⽅法,会先从缓冲区中进⾏读取。如果缓冲区数据不⾜,才会再从⽂件中读取 |
| |
| # 构造函数 |
| |
| BufferedReader(Reader in) |
| |
| BufferedReader(Reader in, int sz) |
| boolean ready() |
| 判断此流是否已准备好被读取,依赖其他流,所以⼀般需要做判断 |
| int read() |
| 读取单个字符 |
| int read(char[] cbuf, int off, int len) |
| 读取⼀部分字符到数组⾥⾯,从数组下标off处放置length⻓度的字符 |
| String readLine() |
| 读取⼀整⾏⽂本⾏,返回⼀整⾏字符串,如果读到⾏尾了就返回null,注意返回的⼀⾏字符中不包含换⾏符 |
| void close() |
| 关闭流释放资源 |
| public class BufferReaderTest { |
| |
| public static void main(String [] args) throws Exception { |
| test1("C:\\Users\\79466\\Desktop\\test\\1.txt"); |
| test2("C:\\Users\\79466\\Desktop\\test\\1.txt"); |
| } |
| |
| public static void test1(String path) throws Exception { |
| |
| BufferedReader reader = new BufferedReader(new FileReader(path)); |
| if(!reader.ready()){ |
| System.out.println("文件流暂时无法读取"); |
| return; |
| } |
| int size; |
| char[] cbuf = new char[1024]; |
| |
| while ( (size = reader.read(cbuf,0,cbuf.length)) != -1){ |
| System.out.println(new String(cbuf,0,size)); |
| } |
| reader.close(); |
| } |
| |
| public static void test2(String path) throws Exception { |
| |
| BufferedReader reader = new BufferedReader(new FileReader(path)); |
| if(!reader.ready()){ |
| System.out.println("文件流暂时无法读取"); |
| return; |
| } |
| String str = ""; |
| |
| while ( (str = reader.readLine()) !=null){ |
| System.out.println(str); |
| } |
| reader.close(); |
| } |
| |
| } |
| 写⼊的数据并不会先输出到⽬的地,⽽是先存储⾄缓冲区中。如果缓冲区中的数据满了,才会⼀次对⽬的地进⾏写出 |
| |
| # 构造函数 |
| BufferedWriter(Writer out) |
| BufferedWriter(Writer out, int sz) |
| |
| # 常用api |
| void write(int c) |
| 写⼊⼀个字符 |
| void write(char[] cbuf, int off, int len) |
| 写⼊字符数组的⼀部分,通过off和len控制。 |
| void write(String s, int off, int len) |
| 写⼊字符数组的⼀部分,通过off和len控制。 |
| void newLine() |
| 写如⼀个换⾏符合 |
| void close() |
| 关闭输⼊流并释放与该流关联的系统资源 |
| void flush() |
| write是写到缓冲区中,可以认为是内存中,当缓冲区满时系统会⾃动将缓冲区的内容写⼊⽂件,但是⼀般还有⼀部分有可能会留在内存这个缓冲区中, 所以需要调⽤flush空缓冲区数据 |
| |
| public class BufferedWriterTest { |
| |
| public static void main(String [] args)throws Exception{ |
| test1("C:\\Users\\79466\\Desktop\\test\\8.txt"); |
| } |
| |
| public static void test1(String path)throws Exception{ |
| |
| BufferedWriter writer = new BufferedWriter(new FileWriter(path)); |
| |
| char ch = '小'; |
| writer.write(ch); |
| |
| String other = "aaaaxdclass.net"; |
| writer.write(other.toCharArray(),0,other.length()); |
| |
| writer.newLine(); |
| writer.close(); |
| } |
| |
| } |
InputStreamReader(继承Reader)
| # 将字节流转换为字符流, 字节流通向字符流的桥梁,如果不指定字符集编码,则解码过程将使⽤平台默认的字符编码,如:UTF-8 |
| # ⽂本⽂件存储是A编码,然后如果以B编码进⾏读取则会乱码 |
| |
| # 构造函数 |
| |
| public InputStreamReader(InputStream in) |
| |
| public InputStreamReader(InputStream in, String charsetName) |
| |
| # api |
| int read() |
| 读取单个字符 |
| int read(char[] cbuf, int off, int len) |
| 读取⼀部分字符到数组⾥⾯,从数组下标off处放置length⻓度的字符 |
| int read(char []cbuf) |
| 将读取到的字符存到数组中,返回读取的字符数 |
| void close() |
| 关闭流释放资源 |
| |
| public class InputStreamReaderTest { |
| |
| public static void main(String [] args)throws Exception{ |
| test1("C:\\Users\\79466\\Desktop\\test\\8.txt"); |
| } |
| |
| public static void test1(String path) throws Exception{ |
| |
| InputStream in = new FileInputStream(path); |
| |
| InputStreamReader isr = new InputStreamReader(in); |
| |
| BufferedReader reader = new BufferedReader(isr); |
| String line; |
| |
| while ((line = reader.readLine())!= null){ |
| System.out.println(line); |
| } |
| isr.close(); |
| reader.close(); |
| } |
| |
| } |
OutputStreamWriter(继承writer)
| # 将字符流转换为字节流(看源码解释), 字符流通向字节流的桥梁,如果不指定字符集编码,则编码过程将使⽤平台默认的字符编码,如:GBK |
| |
| # 构造函数 |
| |
| public OutputStreamWriter(OutputStream out) |
| |
| public OutputStreamWriter(OutputStream out, String charsetName) |
| |
| # api |
| void write(int c) |
| 写⼊⼀个字符 |
| void write(char[] cbuf, int off, int len) |
| 写⼊字符数组的⼀部分,通过off和len控制 |
| void write(String s, int off, int len) |
| 写⼊字符数组的⼀部分,通过off和len控制 |
| void newLine() |
| 写如⼀个换⾏符合 |
| void close() |
| 关闭输⼊流并释放与该流关联的系统资源 |
| void flush() |
| write是写到缓冲区中,可以认为是内存中,当缓冲区满时系统会⾃动将缓冲区的内容写⼊⽂件,但是⼀般还有⼀部分有可能会留在内存这个缓冲区中, 所以需要调⽤flush空缓冲区数据 |
| |
| public class OutputStreamWriterTest { |
| |
| public static void main(String [] args)throws Exception{ |
| test1("C:\\Users\\79466\\Desktop\\test\\9.txt"); |
| } |
| |
| public static void test1(String path)throws Exception{ |
| OutputStream out = new FileOutputStream(path); |
| |
| OutputStreamWriter osr = new OutputStreamWriter(out,"GBK"); |
| BufferedWriter bufw = new BufferedWriter(osr); |
| String str = "欢迎来到课堂xdclass.net"; |
| bufw.write(str); |
| bufw.newLine(); |
| bufw.write("学习java课程"); |
| bufw.flush(); |
| bufw.close(); |
| } |
| |
| } |
| public class TryCatchTest { |
| |
| |
| public static void main(String [] args){ |
| BufferedInputStream bis = null; |
| BufferedOutputStream bos = null; |
| try { |
| FileInputStream fis = new FileInputStream("C:\\Users\\79466\\Desktop\\test\\1.txt"); |
| bis = new BufferedInputStream(fis); |
| FileOutputStream fos = new FileOutputStream("C:\\Users\\79466\\Desktop\\test\\1111.txt"); |
| bos = new BufferedOutputStream(fos); |
| int size; |
| byte [] buf =new byte[1024]; |
| while ((size = bis.read(buf))!= -1){ |
| bos.write(buf,0,size); |
| } |
| }catch (Exception e){ |
| e.printStackTrace(); |
| }finally { |
| if(bis != null){ |
| try { |
| bis.close(); |
| }catch (Exception e){ |
| e.printStackTrace(); |
| }finally { |
| if(bos!=null){ |
| try{ |
| bos.close(); |
| }catch (Exception e){ |
| e.printStackTrace(); |
| } |
| } |
| } |
| } |
| } |
| |
| } |
| |
| |
| public static void test1(){ |
| try( FileInputStream fis = new FileInputStream("C:\\Users\\79466\\Desktop\\test\\1.txt"); |
| BufferedInputStream bis = new BufferedInputStream(fis); |
| FileOutputStream fos = new FileOutputStream("C:\\Users\\79466\\Desktop\\test\\1111.txt"); |
| BufferedOutputStream bos = new BufferedOutputStream(fos); |
| ){ |
| int size; |
| byte [] buf =new byte[1024]; |
| while ((size = bis.read(buf))!= -1){ |
| bos.write(buf,0,size); |
| } |
| }catch (Exception e){ |
| e.printStackTrace(); |
| } |
| |
| } |
| |
| } |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 字符编码:从基础到乱码解决
· 提示词工程——AI应用必不可少的技术