Java基础-IO流对象之字节流(Stream)
作者:尹正杰
版权声明:原创作品,谢绝转载!否则将追究法律责任。
在前面我分享的笔记中,我们一直都是在操作文件或者文件夹,并没有给文件中写任何数据。现在我们就要开始给文件中写数据,或者读取文件中的数据。什么是输入呢?我们这里的输入指的是将文件的内容加载到程序中的过程叫做输入,那上面叫做输出呢?就是将程序的内容持久化到硬盘上叫做输出。
一.字节输出流(outputStream)
java.io.OutputStream此抽象类是表示输出字节流的所有类的超类。
作用:从Java程序将内存中的数据写入到磁盘文件中。
1>.字节输出流FileOutputStream写字节
1 /* 2 @author :yinzhengjie 3 Blog:http://www.cnblogs.com/yinzhengjie/tag/Java%E5%9F%BA%E7%A1%80/ 4 EMAIL:y1053419035@qq.com 5 */ 6 7 package cn.org.yinzhengjie.note5; 8 9 import java.io.FileOutputStream; 10 import java.io.IOException; 11 12 public class FileOutputStreamDemo { 13 public static void main(String[] args) throws IOException { 14 //流对象的构造方法,可以根据路径创建文件,如果文件存在则直接清空文件内容! 15 FileOutputStream fos = new FileOutputStream("yinzhengjie.txt"); 16 //往文件中写一个字节 17 fos.write(50); 18 fos.write(48); 19 fos.write(49); 20 fos.write(56); 21 //释放资源 22 fos.close(); 23 } 24 }
2>.字节输出流FileOutputStream写字节数组
1 /* 2 @author :yinzhengjie 3 Blog:http://www.cnblogs.com/yinzhengjie/tag/Java%E5%9F%BA%E7%A1%80/ 4 EMAIL:y1053419035@qq.com 5 */ 6 7 package cn.org.yinzhengjie.note5; 8 9 import java.io.FileOutputStream; 10 import java.io.IOException; 11 12 public class FileOutputStreamDemo { 13 public static void main(String[] args) throws IOException { 14 //流对象的构造方法,可以根据路径创建文件,如果文件存在则直接清空文件内容! 15 FileOutputStream fos = new FileOutputStream("yinzhengjie.txt"); 16 //写一个字节数组 17 byte[] bytes = {65,66,67,68,69,70}; 18 fos.write(bytes); 19 //释放资源 20 fos.close(); 21 } 22 }
3>.字节输出流FileOutputStream写字节数组的一部分
1 /* 2 @author :yinzhengjie 3 Blog:http://www.cnblogs.com/yinzhengjie/tag/Java%E5%9F%BA%E7%A1%80/ 4 EMAIL:y1053419035@qq.com 5 */ 6 7 package cn.org.yinzhengjie.note5; 8 9 import java.io.FileOutputStream; 10 import java.io.IOException; 11 12 public class FileOutputStreamDemo { 13 public static void main(String[] args) throws IOException { 14 //流对象的构造方法,可以根据路径创建文件,如果文件存在则直接清空文件内容! 15 FileOutputStream fos = new FileOutputStream("yinzhengjie.txt"); 16 byte[] bytes = {65,66,67,68,69,70}; 17 //写一个字节数组的一部分,需要传入数组对象,数组的开始索引,从开始索引开始计算需要写入的长度 18 fos.write(bytes,0,3); 19 //释放资源 20 fos.close(); 21 } 22 }
4>.写入字节数组的简便方式(写入字符串)
1 /* 2 @author :yinzhengjie 3 Blog:http://www.cnblogs.com/yinzhengjie/tag/Java%E5%9F%BA%E7%A1%80/ 4 EMAIL:y1053419035@qq.com 5 */ 6 7 package cn.org.yinzhengjie.note5; 8 9 import java.io.FileOutputStream; 10 import java.io.IOException; 11 12 public class FileOutputStreamDemo { 13 public static void main(String[] args) throws IOException { 14 //流对象的构造方法,可以根据路径创建文件,如果文件存在则直接清空文件内容! 15 FileOutputStream fos = new FileOutputStream("yinzhengjie.txt"); 16 //写入字符串数组 17 fos.write("yinzhengjie".getBytes()); 18 fos.close(); 19 } 20 }
5>.以追加的方式写入
1 /* 2 @author :yinzhengjie 3 Blog:http://www.cnblogs.com/yinzhengjie/tag/Java%E5%9F%BA%E7%A1%80/ 4 EMAIL:y1053419035@qq.com 5 */ 6 7 package cn.org.yinzhengjie.note5; 8 9 import java.io.File; 10 import java.io.FileOutputStream; 11 import java.io.IOException; 12 13 public class FileOutputStreamDemo { 14 public static void main(String[] args) throws IOException { 15 File file = new File("yinzhengjie.txt"); 16 //以追加的方式写入数据,需要在FileOutputStream的构造方法中指定参数为true. 17 FileOutputStream fos = new FileOutputStream(file,true); 18 //写入换行符 19 fos.write(" ".getBytes()); 20 fos.write("yinzhengjie ".getBytes()); 21 fos.close(); 22 } 23 }
二.IO中的异常处理
1 /* 2 @author :yinzhengjie 3 Blog:http://www.cnblogs.com/yinzhengjie/tag/Java%E5%9F%BA%E7%A1%80/ 4 EMAIL:y1053419035@qq.com 5 */ 6 7 package cn.org.yinzhengjie.note5; 8 9 import java.io.FileOutputStream; 10 import java.io.IOException; 11 12 public class FileOutputStreamDemo { 13 public static void main(String[] args) { 14 //try 外面声明变量,try里面建立对象,也就是将fos的作用域提示,这样就可以让finally作用域可以访问到。 15 FileOutputStream fos = null; 16 17 try { 18 //注意,这里给的盘符如果不存在的话就会创建失败,此时fos的值就位null。 19 fos = new FileOutputStream("D:\10.Java\JavaSE\eclipse\Myprogram\workspace\Day8\yinzhengjie.txt"); 20 fos.write("yinzhengjie".getBytes()); 21 22 } catch (IOException e) { 23 //如果硬件问题应该让用户重试,比如用户在写入数据的时候拔掉U盘。 24 System.out.println(e.getMessage()); 25 throw new RuntimeException("文件写入失败,请重试!"); 26 }finally { 27 try { 28 //在释放资源的时候需要对流对象进行判断是否为null,如果不是null。表示对象建立成功,需要关闭资源。 29 if(fos!=null) { 30 fos.close(); 31 } 32 } catch (IOException e) { 33 throw new RuntimeException("关闭资源失败!"); 34 } 35 } 36 } 37 }
三.字节输入流InputStream
java.io.InputStream此抽象类是表示输出字节流的所有类的超类。
作用:将磁盘文件文件内容加载到内存中来。
1>.按照一个字节进行读取
1 /* 2 @author :yinzhengjie 3 Blog:http://www.cnblogs.com/yinzhengjie/tag/Java%E5%9F%BA%E7%A1%80/ 4 EMAIL:y1053419035@qq.com 5 */ 6 7 package cn.org.yinzhengjie.note5; 8 9 import java.io.File; 10 import java.io.FileInputStream; 11 import java.io.FileNotFoundException; 12 import java.io.IOException; 13 14 public class FileInputStreamDemo { 15 public static void main(String[] args) throws IOException { 16 File file = new File("D:\10.Java\JavaSE\eclipse\Myprogram\workspace\Day8\yinzhengjie.txt"); 17 FileInputStream fis = new FileInputStream(file); 18 int index = 0; 19 //依次读取一个字节,当读到文件末尾时index的值为-1,因此称为结束循环的条件。 20 while((index = fis.read()) != -1) { 21 System.out.print((char)index); 22 } 23 fis.close(); 24 } 25 } 26 27 /* 28 以上代码执行结果如下: 29 yinzhengjie 30 Java 31 2018 32 Bg Date 33 golang 34 Python 35 Linux 36 shell 37 */
2>.按照一个字节数组进行读取数据
1 /* 2 @author :yinzhengjie 3 Blog:http://www.cnblogs.com/yinzhengjie/tag/Java%E5%9F%BA%E7%A1%80/ 4 EMAIL:y1053419035@qq.com 5 */ 6 7 package cn.org.yinzhengjie.note5; 8 9 import java.io.File; 10 import java.io.FileInputStream; 11 import java.io.FileNotFoundException; 12 import java.io.IOException; 13 14 public class FileInputStreamDemo { 15 public static void main(String[] args) throws IOException { 16 File file = new File("D:\10.Java\JavaSE\eclipse\Myprogram\workspace\Day8\yinzhengjie.txt"); 17 FileInputStream fis = new FileInputStream(file); 18 //创建字节数组 19 byte[] buf = new byte[4096]; 20 int index ; 21 //依次读取一个字节,当读到文件末尾时index的值为-1,因此称为结束循环的条件。 22 while((index = fis.read(buf)) != -1) { 23 //调用字符串的构造方法,将读取的数据转换成字符串 24 System.out.print(new String(buf,0,index)); 25 } 26 fis.close(); 27 } 28 } 29 30 /* 31 以上代码执行结果如下: 32 yinzhengjie 33 Java 34 2018 35 Bg Date 36 golang 37 Python 38 Linux 39 shell 40 */
四.小试牛刀
1>.字节流复制文件读取单个字节
1 /* 2 @author :yinzhengjie 3 Blog:http://www.cnblogs.com/yinzhengjie/tag/Java%E5%9F%BA%E7%A1%80/ 4 EMAIL:y1053419035@qq.com 5 */ 6 7 package cn.org.yinzhengjie.note5; 8 9 import java.io.File; 10 import java.io.FileInputStream; 11 import java.io.FileOutputStream; 12 import java.io.IOException; 13 14 public class FileCopyDemo { 15 public static void main(String[] args) throws IOException { 16 17 18 FileInputStream fis = null; 19 FileOutputStream fos = null; 20 21 try { 22 File src = new File("D:\10.Java\JavaSE\eclipse\Myprogram\workspace\Day8\yinzhengjie.txt"); 23 File dest = new File("D:\10.Java\JavaSE\eclipse\Myprogram\workspace\Day8\yinzhengjie.backup"); 24 //建立两个流的对象,绑定源文件和目标文件 25 fis = new FileInputStream(src); 26 fos = new FileOutputStream(dest); 27 //字节输入流,读取一个字节,输出流写一个字节 28 int index ; 29 while((index = fis.read()) != -1) { 30 fos.write(index); 31 } 32 }catch(IOException e) { 33 System.out.println(e.getMessage()); 34 throw new RuntimeException("文件复制失败"); 35 }finally { 36 try { 37 if(fos != null) { 38 fos.close(); 39 } 40 }catch(IOException e) { 41 throw new RuntimeException("是否资源失败"); 42 }finally { 43 try { 44 if(fis != null ) { 45 fis.close(); 46 } 47 }catch(IOException e) { 48 throw new RuntimeException("释放资源失败"); 49 } 50 } 51 } 52 } 53 }
2>.字节流复制文件读取字节数组
1 /* 2 @author :yinzhengjie 3 Blog:http://www.cnblogs.com/yinzhengjie/tag/Java%E5%9F%BA%E7%A1%80/ 4 EMAIL:y1053419035@qq.com 5 */ 6 7 package cn.org.yinzhengjie.note5; 8 9 import java.io.File; 10 import java.io.FileInputStream; 11 import java.io.FileOutputStream; 12 import java.io.IOException; 13 14 public class FileCopyDemo { 15 public static void main(String[] args) throws IOException { 16 FileInputStream fis = null; 17 FileOutputStream fos = null; 18 19 try { 20 File src = new File("D:\10.Java\JavaSE\eclipse\Myprogram\workspace\Day8\yinzhengjie.txt"); 21 File dest = new File("D:\10.Java\JavaSE\eclipse\Myprogram\workspace\Day8\yinzhengjie.backup2"); 22 //建立两个流的对象,绑定源文件和目标文件 23 fis = new FileInputStream(src); 24 fos = new FileOutputStream(dest); 25 //定义字节数组,缓冲数据 26 byte[] buf = new byte[4096]; 27 //读取数组,写入数组 28 int index; 29 while((index = fis.read(buf)) != -1) { 30 fos.write(buf,0,index); 31 } 32 }catch(IOException e) { 33 System.out.println(e.getMessage()); 34 throw new RuntimeException("文件复制失败"); 35 }finally { 36 try { 37 if(fos != null) { 38 fos.close(); 39 } 40 }catch(IOException e) { 41 throw new RuntimeException("是否资源失败"); 42 }finally { 43 try { 44 if(fis != null ) { 45 fis.close(); 46 } 47 }catch(IOException e) { 48 throw new RuntimeException("释放资源失败"); 49 } 50 } 51 } 52 } 53 }