Compressed file:
public static boolean oneFileToZip (String sourceFilePath, String [] fileNames, String zipFilePath, String zipName) {// compress the target location where to compress the file name of the array, save the file compression, file compression to save the name 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 ( "compressed file path to be:" + sourceFilePath + "does not exist."); }else{ try { File zipFile = new File (zipFilePath + "/" + zipName + "zip."); // create a compressed file saved file if(zipFile.exists()){ System.out.println ( "exists in the directory named:" + zipFilePath + zipName + ". Zip" + ". Package file"); // for file existence judgment }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 ( "there is no need to compress the file"); }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++){ // Create ZIP entity, and added to the archive ZipEntry zipEntry = new ZipEntry(sourceFiles[i].getName()); zos.putNextEntry (zipEntry); // Read the file to be compressed and written to the compressed bag 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; }
Generating a random number within a certain range:
Integer MaxNum = new Integer(Max); Integer memory = new Integer (Min); // generates a random number in the range of DecimalFormat df = new DecimalFormat("#.000"); double x = my + (Math.random () * (MaxNum memory)); String str = df.format(x); System.out.println(str);
Read and write files in the same directory project (under SpringBoot modify the settings 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()){ // modify the file 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{ //Create a file 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; }