IO流介绍
IO流的分类
按照数据的流向
按照数据类型来分
字节流
字符流
IO流的使用场景
字节流抽象基类
字节输出流
使用字节输出流写数据的步骤
示例代码
xpublic class FileOutputStreamDemo01 { public static void main(String[] args) throws IOException { //创建字节输出流对象 /* 注意点: 1.如果文件不存在,会帮我们创建 2.如果文件存在,会把文件清空 */ //FileOutputStream(String name):创建文件输出流以指定的名称写入文件 FileOutputStream fos = new FileOutputStream("myByteStream\\fos.txt"); //void write(int b):将指定的字节写入此文件输出流 fos.write(97);// fos.write(57);// fos.write(55); //最后都要释放资源 //void close():关闭此文件输出流并释放与此流相关联的任何系统资源。 fos.close(); }}写数据的方法分类
| 方法名 | 说明 |
|---|---|
| void write(int b) | 将指定的字节写入此文件输出流 一次写一个字节数据 |
| void write(byte[] b) | 将 b.length字节从指定的字节数组写入此文件输出流 一次写一个字节数组数据 |
| void write(byte[] b, int off, int len) | 将 len字节从指定的字节数组开始,从偏移量off开始写入此文件输出流 一次写一个字节数组的部分数据 |
示例代码
xxxxxxxxxxpublic class FileOutputStreamDemo02 { public static void main(String[] args) throws IOException { //FileOutputStream(String name):创建文件输出流以指定的名称写入文件 FileOutputStream fos = new FileOutputStream("myByteStream\\fos.txt"); //FileOutputStream(File file):创建文件输出流以写入由指定的 File对象表示的文件// FileOutputStream fos = new FileOutputStream(new File("myByteStream\\fos.txt")); //void write(int b):将指定的字节写入此文件输出流// fos.write(97);// fos.write(98);// fos.write(99);// fos.write(100);// fos.write(101);// void write(byte[] b):将 b.length字节从指定的字节数组写入此文件输出流// byte[] bys = {97, 98, 99, 100, 101}; //byte[] getBytes():返回字符串对应的字节数组 byte[] bys = "abcde".getBytes();// fos.write(bys); //void write(byte[] b, int off, int len):将 len字节从指定的字节数组开始,从偏移量off开始写入此文件输出流// fos.write(bys,0,bys.length); fos.write(bys,1,3); //释放资源 fos.close(); }}字节流写数据如何实现换行
字节流写数据如何实现追加写入
示例代码
xxxxxxxxxxpublic class FileOutputStreamDemo03 { public static void main(String[] args) throws IOException { //创建字节输出流对象// FileOutputStream fos = new FileOutputStream("myByteStream\\fos.txt"); FileOutputStream fos = new FileOutputStream("myByteStream\\fos.txt",true); //写数据 for (int i = 0; i < 10; i++) { fos.write("hello".getBytes()); fos.write("\r\n".getBytes()); } //释放资源 fos.close(); }}异常处理格式
try-catch-finally
xxxxxxxxxxtry{ 可能出现异常的代码;}catch(异常类名 变量名){ 异常的处理代码;}finally{ 执行所有清除操作;}finally特点
示例代码
xxxxxxxxxxpublic class FileOutputStreamDemo04 { public static void main(String[] args) { //加入finally来实现释放资源 FileOutputStream fos = null; try { fos = new FileOutputStream("myByteStream\\fos.txt"); fos.write("hello".getBytes()); } catch (IOException e) { e.printStackTrace(); } finally { if(fos != null) { try { fos.close(); } catch (IOException e) { e.printStackTrace(); } } } }}字节输入流
字节输入流读取数据的步骤
示例代码
xxxxxxxxxxpublic class FileInputStreamDemo01 { public static void main(String[] args) throws IOException { //创建字节输入流对象 //FileInputStream(String name) FileInputStream fis = new FileInputStream("myByteStream\\fos.txt"); int by; /* fis.read():读数据 by=fis.read():把读取到的数据赋值给by by != -1:判断读取到的数据是否是-1 */ while ((by=fis.read())!=-1) { System.out.print((char)by); } //释放资源 fis.close(); }}案例需求
把“E:\itcast\窗里窗外.txt”复制到模块目录下的“窗里窗外.txt” (文件可以是任意文件)
实现步骤
复制文本文件,其实就把文本文件的内容从一个文件中读取出来(数据源),然后写入到另一个文件中(目的地)
数据源:
E:\itcast\窗里窗外.txt --- 读数据 --- InputStream --- FileInputStream
目的地:
myByteStream\窗里窗外.txt --- 写数据 --- OutputStream --- FileOutputStream
代码实现
xxxxxxxxxxpublic class CopyTxtDemo { public static void main(String[] args) throws IOException { //根据数据源创建字节输入流对象 FileInputStream fis = new FileInputStream("E:\\itcast\\窗里窗外.txt"); //根据目的地创建字节输出流对象 FileOutputStream fos = new FileOutputStream("myByteStream\\窗里窗外.txt"); //读写数据,复制文本文件(一次读取一个字节,一次写入一个字节) int by; while ((by=fis.read())!=-1) { fos.write(by); } //释放资源 fos.close(); fis.close(); }}一次读一个字节数组的方法
示例代码
xxxxxxxxxxpublic class FileInputStreamDemo02 { public static void main(String[] args) throws IOException { //创建字节输入流对象 FileInputStream fis = new FileInputStream("myByteStream\\fos.txt"); byte[] bys = new byte[1024]; //1024及其整数倍 int len; //循环读取 while ((len=fis.read(bys))!=-1) { System.out.print(new String(bys,0,len)); } //释放资源 fis.close(); }}案例需求
把“E:\itcast\mn.jpg”复制到模块目录下的“mn.jpg” (文件可以是任意文件去)
实现步骤
代码实现
xxxxxxxxxxpublic class CopyJpgDemo { public static void main(String[] args) throws IOException { //根据数据源创建字节输入流对象 FileInputStream fis = new FileInputStream("E:\\itcast\\mn.jpg"); //根据目的地创建字节输出流对象 FileOutputStream fos = new FileOutputStream("myByteStream\\mn.jpg"); //读写数据,复制图片(一次读取一个字节数组,一次写入一个字节数组) byte[] bys = new byte[1024]; int len; while ((len=fis.read(bys))!=-1) { fos.write(bys,0,len); } //释放资源 fos.close(); fis.close(); }}