zoukankan      html  css  js  c++  java
  • 节点流(文件流) FileInputStream & FileOutputStream & FileReader & FileWriter

    节点流(文件流)

                       FileInputStream(字节流)处理视频类的
                       FileOutputStream(字节流)

                       FileReader(字符流)处理文本文件
                       FileWriter(字符流

    TestFileInputOutStream 
    package com.aff.file;
    
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.IOException;
    
    import javax.print.DocFlavor.STRING;
    
    import org.junit.Test;
    
    //节点流(文件流)
    
    //           FileInputStream(字节流)处理视频类的
    //           FileOutputStream(字节流)
    //          
    //           FileReader(字符流)处理文本文件
    //           FileWriter(字符流)
    public class TestFileInputOutStream {
        
        //FileInputStream
        //从硬盘存在的文件中,读取其内容到程序中
        //要读取的文件一定要存在,否则抛出FileNotFoundException
        @Test
        public void testFileInputStream() throws IOException {
            // 1.创建一个File类的对象
            File file = new File("heel.txt");//指明要读入文件的路径
            // 2.创建一个FileInputStream类的对象
            FileInputStream fis = new FileInputStream(file);
            // 3.调用FileInputStream的方法,实现file文件的读取
            /*
             * read(): 读取文件的一个字节每执行到文件结尾时, 返回-1
             */
    //        int b = fis.read();
    //        while (b != -1) {
    //            System.out.println((char)b);
    //            b = fis.read();
    //        }
            int b ;
            while((b = fis.read())!=-1){
                System.out.println((char)b);
            }
            // 4.关闭相应的流
            fis.close();
        }
        
        
        
        // 改进, 使用try-catch处理,保证流的关闭操作一定可以执行
        @Test
        public void testFileInputStream1() {
        
            FileInputStream fis = null;
            try {
                // 1.创建一个File类的对象
                File file = new File("heel.txt");//指明要读入文件的路径
                // 2.创建一个FileInputStream类的对象
                fis = new FileInputStream(file);
                // 3.调用FileInputStream的方法,实现file文件的读取
                int b ;
                while((b = fis.read())!=-1){
                    System.out.println((char)b);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }finally {
                // 4.关闭相应的流
                if(fis != null){
                    try {
                        fis.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
        
        
        
        //再改进, 使用使用数组读取,更加快
        @Test
        public void testFileInputStream2() {
            FileInputStream fis = null;
            try {
                File file = new File("heel.txt");
                fis = new FileInputStream(file);
                //使用数组
                byte [] b = new byte[5];//读取到的数据写入数组
                int len ;//每次读入到byte中的字节的长度
                while((len = fis.read(b))!=-1){
    //                for(int i = 0;i<len;i++){
    //                    System.out.println((char)b[i]);
    //                }
                    String str = new String(b,0,len);//读入数组b中,从0开始,每次读入的长度
                    System.out.print(str);
                }
                
            } catch (Exception e) {
                e.printStackTrace();
            }finally {
                if(fis != null){
                    try {
                        fis.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
        
        
        
        
        //FileOutputStream
        @Test
        public void  testFileOutputStream(){
            FileOutputStream fos = null;
            try {
                //创建一个File类的对象。表名要写入的文件位置
                //输出的物理文件可以不存在,当执行过程中,若不存在,会自动创建。若存在会将原来的文件覆盖。
                File file =  new File("aff");
                //创建一个FileOutputStream类的对象,将file类的对象作为形参传递给FileOutputStream的构造器中
                fos =  new FileOutputStream(file);
                //写入操作
                fos.write(new String("i love fangfang").getBytes());//将字符串转为字节数组
            }catch (IOException e) {
                e.printStackTrace();
            }finally {
                //关闭输出流
                if(fos != null){
                    try {
                        fos.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
        
        
        
        //testFileInputOutStream, 实现文件复制
        @Test
        public void  testFileInputOutStream(){
            //从硬盘读取一个文件,并写入到另一个位置。(相当于文件的复制)
            File file1 =  new File("aff.txt");
            File file2 =  new File("aff2.txt");
            FileInputStream fis = null;
            FileOutputStream fos = null;
            //提供相应的流
            try {
                fis =  new FileInputStream(file1);
                fos =  new FileOutputStream(file2);
                //实现文件的复制
                byte[] b = new byte[20];
                int len;
                while((len = fis.read(b)) != -1){
                    fos.write(b, 0, len);
                }
            }catch (IOException e) {
                e.printStackTrace();
            }finally {
                if(fos != null){
                    try {
                        fos.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                if(fis != null){
                    try {
                        fis.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
        
        
        
        
        // 实现文件复制的方法
        public   void copyFile(String src,String dest){
            File file1 =  new File(src);
            File file2 =  new File(dest);
            FileInputStream fis = null;
            FileOutputStream fos = null;
            //提供相应的流
            try {
                fis =  new FileInputStream(file1);
                fos =  new FileOutputStream(file2);
                //实现文件的复制
                byte[] b = new byte[20];
                int len;
                while((len = fis.read(b)) != -1){
                    fos.write(b, 0, len);
                }
            }catch (IOException e) {
                e.printStackTrace();
            }finally {
                if(fos != null){
                    try {
                        fos.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                if(fis != null){
                    try {
                        fis.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
        
        
        @Test
        public void testCopyFile(){
            long start  =  System.currentTimeMillis();
            String src = "C:\Users\lz\Desktop\1.avi";
            String dest = "C:\Users\lz\Desktop\2.avi";
            copyFile(src,dest);
            long end =  System.currentTimeMillis();
            System.out.println("花费的时间:"+(end-start));//21MB     4968
        }
        
    }

    TestFileReaderWriter:

    package com.aff.file;
    
    import java.io.File;
    import java.io.FileReader;
    import java.io.FileWriter;
    import java.io.IOException;
    
    import org.junit.Test;
    
    public class TestFileReaderWriter {
    
        // FileReader
        @Test
        public void testFileReader() {
            FileReader fr = null;
            try {
                File file = new File("license.txt");
                fr = new FileReader(file);
                char[] c = new char[30];
                int len;
                while ((len = fr.read(c)) != -1) {
                    String str = new String(c, 0, len);
                    System.out.println(str);
                }
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                if (fr != null) {
                    try {
                        fr.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    
        // 使用FileReader FileWriter ,实现文本文件的复制
        // 对于非文本文件(视频,图片,音乐等)只能使用字节流
        @Test
        public void testFileReaderWriter() {
            // 1.输入流对应的文件src一定要存在,输出流对应的dest可以不存在,执行过程中会自动创建
            FileReader fr = null;
            FileWriter fw = null;
    
            try {
                File src = new File("license.txt");
                File dest = new File("license1.txt");
                fr = new FileReader(src);
                fw = new FileWriter(dest);
                char[] c = new char[30];
                int len;
                while ((len = fr.read(c)) != -1) {
                    fw.write(c, 0, len);
                }
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                if (fr != null) {
                    try {
                        fr.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                if (fw != null) {
                    try {
                        fw.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    
    }


    复制结果:

    All that work will definitely pay off
  • 相关阅读:
    ISS6 WEB服务器不能访问 grf 报表模板文件的问题
    c# 读取记事本txt文档到DataTable中
    C# 泛型LIST转DataTable
    sql 查出一张表中重复的所有记录数据
    Coolite中GridPanel真实分页(储存过程方式)
    SQL对Xml字段的操作
    反射和特性 自定义转换datatable为强类型集合
    LINQ TO SQL 并发控制
    AS3 加载文件
    使ASP.NET网站Forms验证可以指定多个登录页面
  • 原文地址:https://www.cnblogs.com/afangfang/p/12605131.html
Copyright © 2011-2022 走看看