zoukankan      html  css  js  c++  java
  • Java自压字符流工具包.设计文件工具包 1、设计创建文件夹方法,入参为路径和文件夹名称,返回值为文件夹全路径 2、设计判断是否为文件夹方法,入参为路径,返回值为是否为文件夹 3、设计查询文件夹下所有文件方法,入参为路径,返回值为所有文件的集合(文件夹中有文件和文件夹)

    题目

    设计文件工具包

    1、设计创建文件夹方法入参为路径和文件夹名称返回值为文件夹全路径

    2、设计判断是否为文件夹方法入参为路径返回值为是否为文件夹

    3、设计查询文件夹下所有文件方法入参为路径返回值为所有文件的集合文件夹中有文件和文件夹

    4、设计查询文件夹下所有文件方法入参为路径扩展名返回值为相同类型文件的集合

    5、设计文件拷贝方法入参为被拷贝文件拷贝到的文件路径返回值判断对应路径下是否存在该文件

    6、设计文件拷贝方法入参为被拷贝文件拷贝到的文件路径拷贝后的文件名返回值判断对应路径下是否存在该文件

    7、设计文件删除方法入参为被删除文件路径返回值为是否删除成果

     

     

    编写测试类测试相关功能

    解答

    package FILEUtils;
    
    import java.io.BufferedInputStream;
    import java.io.BufferedOutputStream;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.FileReader;
    import java.io.IOException;
    import java.util.ArrayList;
    
    /**
     * 文件工具包 
     * @author Administrator
     *
     */
    public class FileUtils {
        /**
         * 创建文件夹方法
         * @param path
         * @param folder
         * @return
         * @throws IOException
         */
        public String CreateFile(String path,String folder) throws IOException
        {
            String s="";
            s=path+"\"+folder;
            File n=new File(s);
            n.mkdir();
            System.out.println("创建文件夹成功");
            return s;
        }
        
        /**
         * 判断是否为文件夹
         * @param path
         * @return
         */
        public String IFISFile(String path)
        {
            String s="";
            File f=new File(path);
            if(f.isFile()){
                s=path+"是文件";
            }else
            {
                s=path+"不是文件";
            }
            return s;
        }
        
        /**
         * 依据后缀名查看文件
         * @param path
         * @param extra
         * @return
         */
        public ArrayList<File> FINDByextra(String path,String extra)
        {
            ArrayList<File> list=new ArrayList<File>();
            File f=new File(path);
            File[] files=f.listFiles();
            
            for(File single:files)
            {
                if(single.getName().indexOf("."+extra)!=-1)
                {
                    list.add(single);
                }
            }
            return list;
    
        }
        
        /**
         * 文件拷贝1
         * @param FromFilePath
         * @param ToPath
         * @return
         * @throws IOException
         */
        public boolean copy(String FromFilePath,String ToPath) throws IOException
        {
            boolean isok=true;
            File f=new File(FromFilePath);
            FileInputStream fis=new FileInputStream(FromFilePath);
            BufferedInputStream bis=new BufferedInputStream(fis,1024);
            int i=0;
            String str="";
            while((i=fis.read())!=-1){
                str+=(char)i;
            }
            FileOutputStream fos=new FileOutputStream(ToPath);
            BufferedOutputStream bos=new BufferedOutputStream(fos,1024);
    
            fos.write(str.getBytes());
            f=new File(ToPath);
            isok=f.exists();
            bos.close();
            fos.close();
            bis.close();
            fis.close();
            return isok;
        }
        
        
        /**
         * 文件拷贝2
         * @param FromFilePath
         * @param ToPath
         * @param ToFilename
         * @return 
         * @throws IOException 
         */
        public boolean copy(String FromFilePath,String ToPath,String ToFilename) throws IOException
        {
            boolean isok=true;
            File f=new File(FromFilePath);
            FileInputStream fis=new FileInputStream(FromFilePath);
            BufferedInputStream bis=new BufferedInputStream(fis,1024);
            int i=0;
            String str="";
            while((i=bis.read())!=-1){
                str+=(char)i;
            }
            FileOutputStream fos=new FileOutputStream(ToPath+"\"+ToFilename);
            BufferedOutputStream bos=new BufferedOutputStream(fos,1024);
            bos.write(str.getBytes());
            f=new File(ToPath+"\"+ToFilename);
            isok=f.exists();
            
            bos.close();
            fos.close();
            bis.close();
            fis.close();
            return isok;
        }
        
        public boolean DELETEFile(String path)
        {
            boolean isok=true;
            File f=new File(path);
            isok=f.delete();
            
            return isok;
        }
        //测试类
        public static void main(String[] args) throws IOException {
            FileUtils f=new FileUtils();
            System.out.println(f.CreateFile("D:\\IOTest", "new1"));
            System.out.println(f.IFISFile("D:\IOTest"));
            System.out.println(f.IFISFile("D:\1.txt"));
            System.out.println(f.FINDByextra("D:\","txt"));
            System.out.println("路径下是否存在拷贝1后的文件:"+f.copy("D:\1.txt","D:\13.txt"));
            System.out.println("路径下是否存在拷贝2后的文件:"+f.copy("D:\1.txt","D:\","12.txt"));
            System.out.println("文件是否删除:"+f.DELETEFile("D:\1.txt"));
        }
    }
  • 相关阅读:
    [转] 接触C# 反射 2
    [转] C#操作Excel文件
    【Leetcode】Path Sum II
    java通用抹去魔,在边界行动,擦除补偿
    python抓取网络内容
    一个合格的程序猿编程
    Android的相关的源代码的方法
    随笔
    使用方便git命令检查记录的版本号
    opengl 扳回一球
  • 原文地址:https://www.cnblogs.com/lumc5/p/15170417.html
Copyright © 2011-2022 走看看