FileUtil.java
import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.util.zip.ZipEntry; import java.util.zip.ZipOutputStream; public class FileUtil { // 检测指定目录下 以某个日期命名的文件夹下的文件数 public static int autoDectect(String savePath, String date) throws Exception { java.io.File file = new java.io.File(savePath + "/" + date); if (!file.exists()) { if (file.mkdirs()) { } else { throw new java.lang.Exception("失败:无法创建以下目录:[" + savePath + "/" + date + "]"); } } int totalFiles = 0; java.io.File[] subFiles = file.listFiles(); if (subFiles != null && subFiles.length > 0) { String patternStr = "_" + date + "_"; for (int i = 0; i < subFiles.length; i++) { String fileName = subFiles[i].getName(); if (null != fileName && fileName.indexOf(patternStr) != -1) { totalFiles++; } } } return totalFiles; } public static class Test1{ public static void main(String[] args) { String savePath = "D:\\TT"; String date = "20170607"; int n; try { n = autoDectect(savePath, date); System.out.println(n); } catch (Exception e) { e.printStackTrace(); } } } public static void zipFile(String filePath, String fileName) throws Exception { String[] filenames = new String[] { fileName }; byte[] buf = new byte[1024]; String outFilename = fileName + ".zip"; ZipOutputStream out = new ZipOutputStream(new FileOutputStream(filePath + "/" + outFilename)); for (int i = 0; i < filenames.length; i++) { FileInputStream in = new FileInputStream(filePath + "/" + filenames[i]); out.putNextEntry(new ZipEntry(filenames[i])); int len = 0; while ((len = in.read(buf)) > 0) { out.write(buf, 0, len); } out.closeEntry(); in.close(); } out.close(); } public static class Test2{ public static void main(String[] args) { try { zipFile("D:/TT", "rt.txt"); } catch (Exception e) { e.printStackTrace(); } } } public static String writeFile(String path, String fileName, String content, String charset) throws IOException { mkDir(path); String filePath = path + File.separator + fileName; File file = new File(filePath); if (file.exists()) { file.delete(); } file.createNewFile(); FileOutputStream fops = new FileOutputStream(file); fops.write((content).getBytes(charset)); fops.flush(); fops.close(); return filePath; } public static void mkDir(String path) { File file = new File(path); if (!file.exists()) { file.mkdirs(); } } public static String readFile(String filePath) throws IOException { File file = new File(filePath); FileInputStream fin = new FileInputStream(file); ByteArrayOutputStream bs = new ByteArrayOutputStream(); while (true) { int read = fin.read(); if (read == -1) { break; } else { bs.write(read); } } String content = new String(bs.toByteArray(), "UTF-8"); fin.close(); bs.close(); return content; } public static class Test3{ public static void main(String[] args) { String decontent = null; //读文件内容 try { decontent = readFile("D:\\TT\\rt.txt"); } catch (IOException e) { e.printStackTrace(); } System.out.println(decontent); String filePath = null; //写文件内容 try { filePath = writeFile("D:\\TT", "NEW_rt.txt", decontent, "utf-8"); } catch (IOException e) { e.printStackTrace(); } System.out.println(filePath); } } public static String getStringByFile(File file,String charset) { StringBuilder sb = new StringBuilder(); BufferedReader reader = null; try { reader = new BufferedReader(new InputStreamReader(new FileInputStream(file), charset)); String line = ""; while ((line = reader.readLine()) != null) { sb.append(line); } } catch (UnsupportedEncodingException e) { e.printStackTrace(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { if (reader != null) { try { reader.close(); } catch (IOException e) { e.printStackTrace(); } } } return sb.toString(); } }