文件,文件夹的基本操作--------数据流的传输

import org.apache.commons.io.FilenameUtils;

//FilenameUtils.isExtension("",""); 判断文件拓展名是否一致
    public static void fun3(){
        boolean a=FilenameUtils.isExtension("qq.txt", "txt");
        System.out.println(a);
    }


    //FilenameUtils.getname()获得输入目录的最后的文件名
    public static void fun2(){
    String s=    FilenameUtils.getName("d:\\qq.txt");
    System.out.println(s);
    }   
    //FilenameUtils.getExtension("");获取文件的拓展名
    public static void fun1(){
        String s=FilenameUtils.getExtension("d:\\qq.txt");
        System.out.println(s);
    }

import org.apache.commons.io.FileUtils;
    //复制文件夹

       public static void fun4() throws IOException{
            FileUtils.copyDirectoryToDirectory(new File("d:\\java"), new File("e:\\java999"));
        }

    //复制文件
        public static void fun3() throws Exception{
            FileUtils.copyFile(new File("d:\\java.txt"), new File("e:\\java1.txt"));
        }

    //往文件中填写数据
        public static void fun2() throws Exception{
            FileUtils.writeStringToFile(new File("d:\\java.txt"), "85895");
        }

   //从文件中读取数据
        public static void fun1() throws Exception{
            String s=FileUtils.readFileToString(new File("d:\\java.txt"));
            System.out.println(s);
        }

//数据流的传输

//             1 一次读一个字节   .mp3  
//                2 一次读取读个字节   .mp3
//                3 字节缓冲流复制   一次读一个字节
//                4 字节缓冲流复制   一次读一多个字节
           //copy1();
           //copyn();
           // bytesbuffer() ;
           // bytesbuffer1();
            
  
        public static void copy1() throws IOException{
            FileInputStream fis=null;
            FileOutputStream fos=null;
            int len;
               fis= new FileInputStream("d:\\adel1.mp3");
               fos=new FileOutputStream("e:\\adel1.mp3");
                while((len=fis.read())!=-1){
                    fos.write(len);
                }
                fis.close();
                fos.close();
        }
        public static void copyn() throws IOException{
            FileInputStream fis=null;
            FileOutputStream fos=null;
            
             fis= new FileInputStream("d:\\adel2.mp3");
             fos=new FileOutputStream("e:\\adel2.mp3");
             byte[] b=new byte[1024*10];
             int len=0;
              while((len=fis.read(b))!=-1){
                    fos.write(b,0,len);
                }
                fis.close();
                fos.close();
        }
        public static void bytesbuffer() throws IOException{
            BufferedInputStream ref = new BufferedInputStream(new FileInputStream("d:\\adel2.mp3"));
            BufferedOutputStream  rof=new BufferedOutputStream(new FileOutputStream("e:\\adel2.mp3"));
            int len=0;
            while((len=ref.read())!=-1){
                rof.write(len);
            }
            ref.close();
            rof.close();
        }
        public static void bytesbuffer1() throws IOException{
            BufferedInputStream ref = new BufferedInputStream(new FileInputStream("d:\\adel2.mp3"));
            BufferedOutputStream  rof=new BufferedOutputStream(new FileOutputStream("e:\\adel2.mp3"));
            int len=0;
            byte[] bytes=new byte[1024];
            while((len=ref.read(bytes))!=-1){
            
                rof.write(bytes,0,len);
            }
            ref.close();
            rof.close();
        }

猜你喜欢

转载自www.cnblogs.com/Fisherman13/p/10441605.html