zoukankan      html  css  js  c++  java
  • JAVA学习--文件流FileInputStream和FileOutputStream操作

     * 1.流的分类:
     * 按照数据流向的不同:输入流  输出流
     * 按照处理数据的单位的不同:字节流  字符流(处理的文本文件)
     * 按照角色的不同:节点流(直接作用于文件的)  处理流
     *
     * 2.IO的体系
     * 抽象基类            节点流(文件流)                                缓冲流(处理流的一种)
     * InputStream        FileInputStream            BufferedInputStream
     * OutputStream        FileOutputStream        BufferedOutputStream
     * Reader            FileReader                BufferedReader
     * Writer            FileWriter                BufferedWriter

     1     // 从硬盘存在的一个文件中,读取其内容到程序中。使用FileInputStream
     2     // 要读取的文件一定要存在。否则抛FileNotFoundException
     3     @Test
     4     public void testFileInputStream1() throws Exception {
     5         // 1.创建一个File类的对象。
     6         File file = new File("hello.txt");
     7         // 2.创建一个FileInputStream类的对象
     8         FileInputStream fis = new FileInputStream(file);
     9         // 3.调用FileInputStream的方法,实现file文件的读取。
    10         /*
    11          * read():读取文件的一个字节。当执行到文件结尾时,返回-1
    12          */
    13         // int b = fis.read();
    14         // while(b != -1){
    15         // System.out.print((char)b);
    16         // b = fis.read();
    17         // }
    18         int b;
    19         while ((b = fis.read()) != -1) {
    20             System.out.print((char) b);
    21         }
    22         // 4.关闭相应的流
    23         fis.close();
    24     }
     1     // 使用try-catch的方式处理如下的异常更合理:保证流的关闭操作一定可以执行
     2     @Test
     3     public void testFileInputStream2() {
     4         // 2.创建一个FileInputStream类的对象
     5         FileInputStream fis = null;
     6         try {
     7             // 1.创建一个File类的对象。
     8             File file = new File("hello.txt");
     9             fis = new FileInputStream(file);
    10             // 3.调用FileInputStream的方法,实现file文件的读取。
    11             int b;
    12             while ((b = fis.read()) != -1) {
    13                 System.out.print((char) b);
    14             }
    15         } catch (IOException e) {
    16             e.printStackTrace();
    17         } finally {
    18             // 4.关闭相应的流
    19             if (fis != null) {
    20                 try {
    21                     fis.close();
    22                 } catch (IOException e) {
    23                     // TODO Auto-generated catch block
    24                     e.printStackTrace();
    25                 }
    26             }
    27         }
    28     }
     1     @Test
     2     public void testFileInputStream3() { // abcdefgcde
     3         FileInputStream fis = null;
     4         try {
     5             File file = new File("hello.txt");
     6             fis = new FileInputStream(file);
     7             byte[] b = new byte[5];// 读取到的数据要写入的数组。
     8             int len;// 每次读入到byte中的字节的长度
     9             while ((len = fis.read(b)) != -1) {
    10                 // for (int i = 0; i < len; i++) {
    11                 // System.out.print((char) b[i]);
    12                 // }
    13                 String str = new String(b, 0, len);
    14                 System.out.print(str);
    15             }
    16         } catch (IOException e) {
    17             e.printStackTrace();
    18         } finally {
    19             if (fis != null) {
    20                 try {
    21                     fis.close();
    22                 } catch (IOException e) {
    23                     // TODO Auto-generated catch block
    24                     e.printStackTrace();
    25                 }
    26 
    27             }
    28 
    29         }
    30     }
     1     // FileOutputStream
     2     @Test
     3     public void testFileOutputStream() {
     4         // 1.创建一个File对象,表明要写入的文件位置。
     5         // 输出的物理文件可以不存在,当执行过程中,若不存在,会自动的创建。若存在,会将原有的文件覆盖
     6         File file = new File("hello2.txt");
     7         // 2.创建一个FileOutputStream的对象,将file的对象作为形参传递给FileOutputStream的构造器中
     8         FileOutputStream fos = null;
     9         try {
    10             fos = new FileOutputStream(file);
    11             // 3.写入的操作
    12             fos.write(new String("I love China!").getBytes());
    13         } catch (Exception e) {
    14             e.printStackTrace();
    15         } finally {
    16             // 4.关闭输出流
    17             if (fos != null) {
    18                 try {
    19                     fos.close();
    20                 } catch (IOException e) {
    21                     // TODO Auto-generated catch block
    22                     e.printStackTrace();
    23                 }
    24             }
    25         }
    26     }
     1     // 从硬盘读取一个文件,并写入到另一个位置。(相当于文件的复制)
     2     @Test
     3     public void testFileInputOutputStream() {
     4         // 1.提供读入、写出的文件
     5         File file1 = new File("C:\Users\shkstart\Desktop\1.jpg");
     6         File file2 = new File("C:\Users\shkstart\Desktop\2.jpg");
     7         // 2.提供相应的流
     8         FileInputStream fis = null;
     9         FileOutputStream fos = null;
    10         try {
    11             fis = new FileInputStream(file1);
    12             fos = new FileOutputStream(file2);
    13             // 3.实现文件的复制
    14             byte[] b = new byte[20];
    15             int len;
    16             while ((len = fis.read(b)) != -1) {
    17                 // fos.write(b);//错误的写法两种: fos.write(b,0,b.length);
    18                 fos.write(b, 0, len);
    19             }
    20         } catch (Exception e) {
    21             e.printStackTrace();
    22         } finally {
    23             if (fos != null) {
    24                 try {
    25                     fos.close();
    26                 } catch (IOException e) {
    27                     e.printStackTrace();
    28                 }
    29             }
    30             if (fis != null) {
    31                 try {
    32                     fis.close();
    33                 } catch (IOException e) {
    34                     e.printStackTrace();
    35                 }
    36             }
    37 
    38         }
    39     }
     1     // 实现文件复制的方法
     2     public void copyFile(String src, String dest) {
     3         // 1.提供读入、写出的文件
     4         File file1 = new File(src);
     5         File file2 = new File(dest);
     6         // 2.提供相应的流
     7         FileInputStream fis = null;
     8         FileOutputStream fos = null;
     9         try {
    10             fis = new FileInputStream(file1);
    11             fos = new FileOutputStream(file2);
    12             // 3.实现文件的复制
    13             byte[] b = new byte[1024];
    14             int len;
    15             while ((len = fis.read(b)) != -1) {
    16                 // fos.write(b);//错误的写法两种: fos.write(b,0,b.length);
    17                 fos.write(b, 0, len);
    18             }
    19         } catch (Exception e) {
    20             e.printStackTrace();
    21         } finally {
    22             if (fos != null) {
    23                 try {
    24                     fos.close();
    25                 } catch (IOException e) {
    26                     e.printStackTrace();
    27                 }
    28             }
    29             if (fis != null) {
    30                 try {
    31                     fis.close();
    32                 } catch (IOException e) {
    33                     e.printStackTrace();
    34                 }
    35             }
    36 
    37         }
    38     }
     1     @Test
     2     public void testCopyFile(){
     3         long start = System.currentTimeMillis();
     4 //        String src = "C:\Users\shkstart\Desktop\1.avi";
     5 //        String dest = "C:\Users\shkstart\Desktop\2.avi";
     6         String src = "dbcp.txt";
     7         String dest = "dbcp2.txt";
     8         copyFile(src,dest);
     9         long end = System.currentTimeMillis();
    10         System.out.println("花费的时间为:" + (end - start));//3198
    11     }
  • 相关阅读:
    搜狗输入法--评价
    课堂练习--找水王
    学习进度条---第十二周
    Java接口与继承动手动脑
    Java数组课后作业
    Java语言String字符串课后作业
    Java类和对象课后作业
    Java语言第四讲
    Java语法基础----课后实践作业
    Java课堂作业-------参数求和
  • 原文地址:https://www.cnblogs.com/zhangfan94/p/4312076.html
Copyright © 2011-2022 走看看