Java基础-IO流对象之字节流(Stream)

                  Java基础-IO流对象之字节流(Stream)
java

                                      做者:尹正杰golang

版权声明:原创做品,谢绝转载!不然将追究法律责任。shell

 

  在前面我分享的笔记中,咱们一直都是在操做文件或者文件夹,并无给文件中写任何数据。如今咱们就要开始给文件中写数据,或者读取文件中的数据。什么是输入呢?咱们这里的输入指的是将文件的内容加载到程序中的过程叫作输入,那上面叫作输出呢?就是将程序的内容持久化到硬盘上叫作输出。数组

 

一.字节输出流(outputStream)eclipse

   java.io.OutputStream此抽象类是表示输出字节流的全部类的超类。spa

  做用:从Java程序将内存中的数据写入到磁盘文件中。code

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写字节数组blog

 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("\r\n".getBytes()); 20         fos.write("yinzhengjie\r\n".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 }

相关文章
相关标签/搜索