Java常用库和工具类

压缩文件:

 public static boolean oneFileToZip(String sourceFilePath,String[] fileNames, String zipFilePath,String zipName){//压缩目标所在位置,要压缩的文件名字数组,压缩文件的保存位置,压缩文件的保存名字
        boolean flag = false;
        FileInputStream fis = null;
        BufferedInputStream bis = null;
        FileOutputStream fos = null;
        ZipOutputStream zos = null;
        File sourceFile = new File(sourceFilePath);

        if(sourceFile.exists() == false){
            System.out.println("待压缩的文件路径:"+sourceFilePath+"不存在.");
        }else{
            try {
                File zipFile = new File(zipFilePath + "/" + zipName +".zip");//创建文件保存的压缩文件

                if(zipFile.exists()){
                    System.out.println(zipFilePath + "目录下存在名字为:" + zipName +".zip" +"打包文件.");//对文件存在性进行判断
                }else{


                    File[] sourceFiles = new File[fileNames.length];
                    for (int i=0;i<fileNames.length;i++){
                        File file = new File(sourceFilePath+"/"+fileNames[i]);
                        sourceFiles[i] = file;
                    }

                    if(null == sourceFiles || sourceFiles.length<1){
                        System.out.println("不存在需要压缩的文件");
                    }else{
                        fos = new FileOutputStream(zipFile);
                        zos = new ZipOutputStream(new BufferedOutputStream(fos));
                        byte[] bufs = new byte[1024*10];
                        for(int i=0;i<sourceFiles.length;i++){
                            //创建ZIP实体,并添加进压缩包
                            ZipEntry zipEntry = new ZipEntry(sourceFiles[i].getName());
                            zos.putNextEntry(zipEntry);
                            //读取待压缩的文件并写进压缩包里
                            fis = new FileInputStream(sourceFiles[i]);
                            bis = new BufferedInputStream(fis, 1024*10);
                            int read = 0;
                            while((read=bis.read(bufs, 0, 1024*10)) != -1){
                                zos.write(bufs,0,read);
                            }
                        }
                        flag = true;
                    }
                }
            } catch (FileNotFoundException e) {
                e.printStackTrace();
                throw new RuntimeException(e);
            } catch (IOException e) {
                e.printStackTrace();
                throw new RuntimeException(e);
            } finally{
                try {
                    if(null != bis) bis.close();
                    if(null != zos) zos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                    throw new RuntimeException(e);
                }
            }
        }
        return flag;
    }

在一定范围内产生随机数:

Integer MaxNum = new Integer(Max);
Integer MinNum = new Integer(Min);
//在范围内产生随机数
DecimalFormat df = new DecimalFormat("#.000");
double x=MinNum+(Math.random()*(MaxNum-MinNum));
String str = df.format(x);
System.out.println(str);

 在项目同级目录下进行文件读写(SpringBoot下修改设置Properties)

public static Boolean setRemoteProperty(String key,String value) {

        File directory = new File("");
        String FileName = "remoteProject.properties";
        try{
            String courseFile = directory.getCanonicalPath();
            File file = new File(courseFile,FileName);
            if(file.isFile()){
                //修改文件
                Properties prop = new Properties();
                prop.load(new FileInputStream(file));
                OutputStream fos = new FileOutputStream(file);
                prop.setProperty(key.trim(), value.trim());
                prop.store(fos, null);
                fos.close();
            }else{
                //创建文件
                file.createNewFile();
                Properties prop = new Properties();
                prop.load(new FileInputStream(file));
                OutputStream fos = new FileOutputStream(file);
                prop.setProperty(key.trim(), value.trim());
                prop.store(fos, null);
                fos.close();
            }
        }catch (IOException e){
            e.printStackTrace();
        }

        return true;
    }

  

猜你喜欢

转载自www.cnblogs.com/Congratulate/p/11008377.html