深入了解IO流

    技术2025-11-09  2

    IO流

    流的概念

    在程序中所有的数据都是以流的方式进行传输或保存的,程序需要数据的时候要使用输入流读取数据,而当程序需要将一些数据保存起来的时候,就要使用输出流完成。

    程序中的输入输出都是以流的形式保存的,流中保存的实际上全都是字节文件。 IO流在java中从输入输出角度分类:

    输入流: 程序从输入流读取数据源。数据源包括外界(键盘、文件、网络…),即是将数据源读入到程序的通信通道 输出流: 程序向输出流写入数据。将程序中的数据输出到外界(显示器、打印机、文件、网络…)的通信通道。

    IO流在java中从数据的角度来分类:

    字符流: 文本,我们能读的懂的都可以认为是字符流。比如:文章,java文件等等 字节流: 二进制的数据,这种数据- - 般用文本打开我们读不懂。比如,图片文件,mp3文件,等等。

    1.字符流 文本,我们能读的懂的都可以认为是字符流。比如:文章,java文件等等

    字符输入流的超类: Reader: 子类FileReader, BufferedReader 字符输出流的超类: Writer: 子类FileWriter, BufferedWriter 字符流的类的命名规则: 如果是输出流就以Writer结尾 如果是输入流就以Reader结尾

    字符流的写入

    示例: 代码:

    import java.io.File; import java.io.FileWriter; import java.io.IOException; import java.io.Writer; public class Test2 { public static void main(String[] args) { //题目:使用字符流向文件输入hellWorld Writer writer = null; try { //IO流是需要关闭的,如果不这样设计就不能关闭资源 writer = new FileWriter(“test1.txt”,true);//加上true则是追加,不会覆盖 writer.write("helloWorld"); } catch (Exception ex) { ex.printStackTrace(); } finally { //判断writer不是空,防止空指针异常 if (writer != null) { try { //再关闭前会做flush(清空缓存)的事情,close()方法自带这种功能 writer.close(); } catch (IOException e) { e.printStackTrace(); } } } } }

    FileWriter的写入功能:

    补充: writer.Close();自带flush()清空功能,资源如果不关闭,则写不进去,flush()作用 当写入的字符较多时,压力会比较大,应每固定次数writer.flush();清空,会减小压力,性能会提高。

    输出换行 Windows: \r\n Linux:\n Mac:\r

    字符流的读取

    三个步骤:

    创建出入流对象FileReader读取数据关闭输入流

    FileReader的读取方法: 示例:

    import java.io.File; import java.io.FileReader; import java.io.IOException; import java.io.Reader; import java.util.Arrays; public class Test5 { public static void main(String[] args) { File file = new File("test.txt"); Reader reader = null; try { reader = new FileReader(file); //定义一个数组 char[] cs=new char[5]; //向字符数组中填数据 //如果没有数据,返回的时-1 // int read = reader.read(cs); // System.out.println("读取的长度"+read+",读取的内容"+ Arrays.toString(cs)); int length=-1; while((length =reader.read(cs)) != -1){ String str=new String (cs,0,length); System.out.print(str); } } catch (Exception e) { e.printStackTrace(); } finally { if(reader !=null) { try { reader.close(); } catch (IOException e) { e.printStackTrace(); } } } } }

    通过以上的内容,相比对IO大概有所了解,下面我们了解一下文件的拷贝

    文件拷贝

    import java.io.*; public class Test6 { public static void main(String[] args) { //创建一个文件兑现 File file=new File("F:\\aaa\\aaa.txt"); File file1=new File("aaa.txt"); Writer writer=null; Reader reader=null; try { reader=new FileReader(file); writer=new FileWriter(file1); char[] cs=new char[1024]; int len=-1; while((len=reader.read( cs))!= -1){ //把输入流读取到的数据写入字符输出流 writer.write(cs,0,len); } writer.flush(); } catch (Exception e) { e.printStackTrace(); } finally { try { if(writer!=null){ writer.close(); } if(reader != null) { reader.close(); } } catch (IOException e) { e.printStackTrace(); } } } }

    高效缓存区输入流

    读取

    package cn; import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; public class BufferReader1 { public static void main(String[] args) { BufferedReader reader = null; try { //创建一个高效缓存字符输入对象 reader = new BufferedReader(new FileReader("aaa.txt")); String s = null; String line=null; while((line=reader.readLine())!=null){ System.out.println(line); } } catch (Exception e) { e.printStackTrace(); } finally { //关闭外层对象时候,内层的对象资源会自动关闭 if (reader != null) { try { reader.close(); } catch (IOException e) { e.printStackTrace(); } } } } }

    高效缓存区输出流

    拷贝:

    package cn; import java.io.*; public class BufferedWirter1 { public static void main(String[] args) { //创建搞笑缓冲字符输入流 BufferedReader reader=null; BufferedWriter writer=null; try { //创建高效缓冲输出流 reader=new BufferedReader(new FileReader("aaa.txt")); //创建高效缓冲输出流 writer=new BufferedWriter(new FileWriter("bbb.txt")); //读取一行的变量 String line =null; while((line =reader.readLine())!= null){ //把读入的这一行数据写入到高效输出缓冲区 writer.write(line); writer.newLine(); writer.flush(); } }catch (Exception ex){ ex.printStackTrace(); }finally { try { if(writer != null){ writer.close(); } if(reader != null){ reader.close(); } } catch (IOException e) { e.printStackTrace(); } } } }

    字节流

    字节流概述 字节输入流: InputStream: 常用子类: FileInputStream 字节输出流: OutputStream 常用子类: FileOutputStream

    字节输出流

    字节输入流

    OutputStream提供的方法

    InputStream提供的方法

    读取:

    package cn.zj; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; public class Demo5 { public static void main(String[] args) { InputStream in = null; try { byte[] b = new byte[5]; in = new FileInputStream("a.txt"); //定义读取到的长度的标志 int len = -1; while ((len = in.read(b)) != -1) { String s = new String(b, 0, len); System.out.println(s); } } catch (Exception e) { e.printStackTrace(); } finally { if (in != null) { try { in.close(); } catch (IOException e) { e.printStackTrace(); } } } } }

    字节流拷贝文件

    package cn; import java.io.*; public class Demo1 { public static void main(String[] args) { InputStream in=null; OutputStream out=null; try { //创建字节输入流 in=new FileInputStream("C:\\Users\\23792\\Music\\力量.mp3"); //创建字节输出流 out=new FileOutputStream("力量.mp3"); //创建一个byte数组 byte [] bs=new byte[1024]; int len=-1; while ((len= in.read(bs)) != -1){ //读取数组中的内容,写入字节输出流 out.write(bs,0,len); } } catch (Exception e) { e.printStackTrace(); } finally { try { if(out != null){ out.close(); } if(in != null){ in.close(); } } catch (IOException e) { e.printStackTrace(); } } } }

    高效缓冲字节流

    package cn.txt; import java.io.*; public class Demo3 { public static void main(String[] args) { BufferedInputStream in = null; BufferedOutputStream ow = null; try { in = new BufferedInputStream(new FileInputStream("aaa.txt")); ow = new BufferedOutputStream(new FileOutputStream("d.txt")); byte[] b = new byte[1024]; int len = -1; while ((len = in.read(b)) != -1) { ow.write(b, 0, len); } } catch (Exception e) { e.printStackTrace(); } finally { try { if (ow != null) { ow.close(); } if (in != null) { in.close(); } } catch (IOException e) { e.printStackTrace(); } } } }

    不可撼动的野心!唯有努力

    Processed: 0.011, SQL: 9