zoukankan      html  css  js  c++  java
  • 2

     public static boolean clear(String path) {
    if (isFile(path)) {
    return clearFile(path);
    }
    return clearFolder(path);
    }

    public static boolean clearFolder(String path) {
    boolean delete = deleteFolder(path);
    boolean create = createFolder(path);
    return ((delete) && (create));
    }

    public static boolean clearFile(String path) {
    boolean delete = deleteFile(path);
    boolean create = createFile(path);
    return ((delete) && (create));
    }

    public static long getSize(String path) {
    if (isFile(path)) {
    return getFileSize(path);
    }
    return getFolderSize(path);
    }

    public static long getFileSize(String path) {
    File file = new File(path);
    return getFileSize(file);
    }

    public static long getFolderSize(String path) {
    File file = new File(path);
    return getFolderSize(file);
    }

    public static long getSize(File file) {
    if (isFile(file)) {
    return getFileSize(file);
    }
    return getFolderSize(file);
    }

    public static long getLastModifiedTime(String path) {
    File file = new File(path);
    return getLastModifiedTime(file);
    }

    public static long getLastModifiedTime(File file) {
    return file.lastModified();
    }

    public static long getFileSize(File file) {
    if ((isDirectory(file)) || (!(file.exists()))) {
    return 0L;
    }
    return file.length();
    }

    public static long getFolderSize(File file) {
    if ((isFile(file)) || (!(file.exists()))) {
    return 0L;
    }
    long sum = 0L;
    String[] files = file.list();
    if (files != null) {
    for (String f : files) {
    File ff = new File(file.getAbsoluteFile() + "/" + f);
    if (ff != null) {
    if (ff.isDirectory())
    sum += getFolderSize(ff.getAbsolutePath());
    else {
    sum += getFileSize(ff.getAbsolutePath());
    }
    }
    }
    }
    return sum;
    }

    public static List<String> getAllFiles(String path) {
    File file = new File(path);
    List fs = new ArrayList();
    if (isFile(path)) {
    return null;
    }
    String[] files = file.list();
    if (files != null) {
    for (String f : files) {
    File ff = new File(file.getAbsoluteFile() + File.separator + f);
    if ((ff != null) && (ff.isFile())) {
    fs.add(ff.getAbsolutePath());
    }
    }
    }
    return fs;
    }

    public static List<String> getAllFiles(String path, boolean all) {
    File file = new File(path);
    List fs = new ArrayList();
    if (null == fs || isFile(path)) {
    return null;
    }
    String[] files = file.list();
    fs.add(path);
    if ((files != null) && (files.length > 0)) {
    for (String f : files) {
    File ff = new File(file.getAbsoluteFile() + File.separator + f);
    if (ff != null) {
    if (ff.isFile()) {
    fs.add(ff.getAbsolutePath());
    } else {
    List list = getAllFiles(ff.getAbsolutePath(), all);
    if (list != null) {
    fs.addAll(list);
    }
    }
    }
    }
    }
    return fs;
    }

    public static List<String> getAllDirectorys(String path) {
    File file = new File(path);
    List fs = new ArrayList();
    if (isFile(path)) {
    return null;
    }
    String[] files = file.list();
    if (files != null) {
    for (String f : files) {
    File ff = new File(file.getAbsoluteFile() + File.separator + f);
    if ((ff != null) && (ff.isDirectory())) {
    fs.add(ff.getAbsolutePath());
    }
    }
    }
    return fs;
    }

    public static List<String> getAllFiles(String path, String reg) {
    List fs = getAllFiles(path);
    if (null == fs) {
    return null;
    }
    for (int i = 0; i < fs.size(); ++i) {
    Pattern pattern = Pattern.compile(reg);
    Matcher matcher = pattern.matcher((CharSequence) fs.get(i));
    if (!(matcher.find())) {
    fs.remove(i--);
    }
    }
    return fs;
    }

    public static List<String> getAllFiles(String path, String reg, boolean deep) {
    List fs = getAllFiles(path, true);
    if (CommonUtils.isNotEmpty(fs)){
    for (int i = 0; i < fs.size(); ++i) {
    Pattern pattern = Pattern.compile(reg);
    Matcher matcher = pattern.matcher((CharSequence) fs.get(i));
    if (!(matcher.find())) {
    fs.remove(i--);
    }
    }
    return fs;
    }
    return null;
    }

    public static List<String> getAllFiles(String path, String startsWith,
    String endsWith) {
    List fs = getAllFiles(path);
    if (null == fs) {
    return null;
    }
    for (int i = 0; i < fs.size(); ++i) {
    String[] files = null;
    if (File.separator.equals("/"))
    files = ((String) fs.get(i)).split("/");
    else {
    files = ((String) fs.get(i)).split("\\");
    }
    String name = files[(files.length - 1)];
    if ((!(name.startsWith(startsWith)))
    || (!(name.endsWith(endsWith)))) {
    fs.remove(i--);
    }
    }
    return fs;
    }

    public static void delete(String path, String startsWith, String endsWith) {
    List fs = getAllFiles(path, startsWith, endsWith);
    if (fs != null){
    for (int i = 0; i < fs.size(); ++i){
    delete((String) fs.get(i));
    }

    }

    }

    public static void writeFile(InputStream content, String fileName) {
    create(fileName);
    FileOutputStream fs = null;
    try {
    int bytesum = 0;
    int byteread = 0;
    fs = new FileOutputStream(fileName);
    byte[] buffer = new byte[1024];
    while ((byteread = content.read(buffer)) != -1) {
    bytesum += byteread;
    fs.write(buffer, 0, byteread);
    }
    content.close();
    content = null;
    fs.close();
    fs = null;
    } catch (Exception e) {
    logger.error("writeFile error, ", e);
    } finally {
    try {
    if (content != null)
    content.close();
    } catch (Exception e) {
    logger.error("writeFile close content error, ", e);
    }
    try {
    if (fs != null)
    fs.close();
    } catch (Exception e) {
    logger.error("writeFile close fs error, ", e);
    }
    }
    }

    public static List<String> readByLine(String file) {
    List<String> li = new ArrayList<>();

    FileInputStream fr = null;
    BufferedReader r = null;
    InputStreamReader read = null;
    try {
    fr = new FileInputStream(file);
    read = new InputStreamReader(fr, "UTF-8");
    r = new BufferedReader(read);
    // String line = null;
    // while ((line = r.readLine()) != null) {
    // li.add(line);
    // }
    LineIterator lineIter = new LineIterator(r);
    while (lineIter.hasNext()) {
    li.add(lineIter.next());
    }
    } catch (Exception e) {
    // e.printStackTrace();
    // C_381612 FORTIFY.System_Information_Leak
    logger.error("readByLine close r error, ", e);
    } finally {
    if (r != null) {
    try {
    r.close();
    } catch (IOException e) {
    logger.error("readByLine close r error, ", e);
    }
    }
    if (fr != null) {
    try {
    fr.close();
    } catch (IOException e) {
    logger.error("readByLine close fr error, ", e);
    }
    }
    if (read != null) {
    try {
    read.close();
    } catch (IOException e) {
    logger.error("readByLine close read error, ", e);
    }
    }
    }

    return li;
    }

    public static void writeByLine(List<String> str, String filename) {
    FileOutputStream fop = null;
    OutputStreamWriter writer = null;
    try {
    File file = new File(filename);
    fop = new FileOutputStream(file);
    writer = new OutputStreamWriter(fop, "UTF-8");
    create(filename);
    for (int i = 0; i < str.size(); ++i) {
    writer.write(((String) str.get(i)) + " ");
    }
    writer.flush();
    } catch (IOException e) {
    logger.error("writeByLine IOException error, ", e);
    } finally {
    if (writer != null) {
    try {
    writer.close();
    } catch (IOException e) {
    logger.error("writeByLine error,", e);
    }
    }
    if (fop != null)
    try {
    fop.close();
    } catch (IOException e) {
    logger.error("writeByLine error,", e);
    }
    }
    }
  • 相关阅读:
    基于javascript 上传
    mysql 分组查询
    php 遍历指定路径所有目录与文件夹
    设置Tomcat的内存
    SQL语句执行顺序
    XFire发布Webservice
    Java排序算法
    Oracle定时任务DBMS_JOB
    JAXWS发布WebService
    Mogodb基础知识和安装学习
  • 原文地址:https://www.cnblogs.com/sg9527/p/8568935.html
Copyright © 2011-2022 走看看