zoukankan      html  css  js  c++  java
  • java IO流中文件,图像,视频,拷贝总结

    import java.awt.image.BufferedImageFilter;
    import java.io.BufferedInputStream;
    import java.io.BufferedOutputStream;
    import java.io.BufferedReader;
    import java.io.BufferedWriter;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.FileReader;
    import java.io.FileWriter;
    import java.io.IOException;
    
    /**
     * 用四种流实现文件的拷贝
     * @author Administrator
     *
     */
    public class CopyFileTest {
    
        public static void main(String[] args) {
            // TODO Auto-generated method stub
            String srcPath="d:/test.txt";
            String destPath="f:/aaaa.txt";
            copyFile(srcPath,destPath);  //字节流实现文件的拷贝
            copyFile2(srcPath,destPath);//字节缓冲流实现文件的拷贝
            copyFile3(srcPath,destPath);//字符流实现文件的拷贝
            copyFile4(srcPath,destPath);//字符缓冲流实现文件的拷贝
        }
        
        
    
        
    
    
    
        /**
         * 用字节流实现文件的拷贝
         * @param scrPath  源路径名
         * @param destPath 目标路径名
         */
        private static void copyFile(String srcPath, String destPath) {
            // TODO Auto-generated method stub
            try (FileInputStream fis = new FileInputStream(srcPath);
                    FileOutputStream fos = new FileOutputStream(destPath);){
                //新建一个字节数组
                byte[] b=new byte[1024];
                //声明一个整型常量,用来记录数组的长度
                int len;
                //文件的拷贝
                while((len=fis.read(b))!=-1){
                    fos.write(b,0,len);
                }
                System.out.println("拷贝文件成功");
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        /**
         * 字节缓冲流实现文件的拷贝
         * @param scrPath  源路径名
         * @param destPath 目标路径名
         */
        private static void copyFile2(String srcPath, String destPath) {
            // TODO Auto-generated method stub
            try(BufferedInputStream bis = new BufferedInputStream(new FileInputStream(srcPath));
                    BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(destPath));){
                //创建一个字节数组
                byte[]b=new byte[1024];
                //int常量用来接收位置
                int len;
                //开始循环读取字节,写入文件
                while((len=bis.read(b))!=-1){
                    bos.write(b,0,len);
                }
                System.out.println("文件拷贝成功");
                
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        
        
        /**
         * 字符流实现文件的拷贝
         * @param scrPath   源路径名
         * @param destPath    目标路径名
         */
        private static void copyFile3(String srcPath, String destPath) {
            // TODO Auto-generated method stub
            try (FileReader fr = new FileReader(srcPath);
                    FileWriter fw = new FileWriter(destPath);){
                //创建一个字符数组
                char[] ch=new char[1024];
                //声明一个变量
                int len;
                while((len=fr.read(ch))!=-1){
                    fw.write(ch,0,len);
                }
                System.out.println("拷贝文件成功");
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        /**
         * 字符缓冲流实现文件的拷贝
         * @param srcPath  源路径名
         * @param destPath 目标路径名
         */
        private static void copyFile4(String srcPath, String destPath) {
            // TODO Auto-generated method stub
            try (BufferedReader br = new BufferedReader(new FileReader(srcPath));
                BufferedWriter bw = new BufferedWriter(new FileWriter(destPath));){
                //字符缓冲是对文件一行读取
                String temp;    //临时变量记录文件读取的位置
                while((temp=br.readLine())!=null){
                    bw.write(temp);
                    bw.newLine();//读取一行,并实现换行
                }
                System.out.println("文件拷贝成功");
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            
        }
    
    }

    个人总结:读取数据,可以选用字符流,文件拷贝最好用字节流,字符流拷贝文件容易丢失数据,造成文件、图像视频打不开!

  • 相关阅读:
    转换相关内置函数_hex
    转换相关内置函数_oct
    内置函数help
    内置函数_locals_变量相关
    内置函数_globals_变量相关
    智慧工地支撑平台工地一张图在线管控平台
    轨道交通GIS平台的应用分析
    基于BIM+GIS钢结构全生命周期管理平台项目
    应急指挥中心系统的研究与设计
    基于SuperMap10i 开发的全过程咨询管理平台的研究
  • 原文地址:https://www.cnblogs.com/ll1994/p/8303665.html
Copyright © 2011-2022 走看看