All Downloads are FREE. Search and download functionalities are using the official Maven repository.

cn.dreampie.FileUtilities Maven / Gradle / Ivy

The newest version!
package cn.dreampie;

import com.google.common.collect.Lists;
import org.apache.commons.io.filefilter.DirectoryFileFilter;
import org.apache.commons.io.filefilter.SuffixFileFilter;
import org.apache.maven.model.FileSet;
import org.codehaus.plexus.util.FileUtils;

import java.io.File;
import java.io.FileFilter;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;

/**
 * Copyright 2011 Mark Derricutt.
 * 

* Contributing authors: * Daniel Bower *

* Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at *

* http://www.apache.org/licenses/LICENSE-2.0 *

* Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. *

* Utilities for working with Files and FileSets */ public class FileUtilities { @SuppressWarnings("unchecked") public static List getFilesFromFileSet(FileSet fileSet) throws IOException { List files = Lists.newArrayList(); try { File directory = new File(fileSet.getDirectory()); String includes = getCommaSeparatedList(fileSet.getIncludes()); String excludes = getCommaSeparatedList(fileSet.getExcludes()); files = FileUtils.getFiles(directory, includes, excludes); } catch (IOException e) { throw new IOException("Unable to access files in fileSet", e); } return files; } /** * Turn a list of Strings into a concatenated string of filenames */ public static String getCommaSeparatedList(List list) { StringBuffer sb = new StringBuffer(); for (Iterator i = list.iterator(); i.hasNext(); ) { sb.append(i.next()); if (i.hasNext()) { sb.append(","); } } return sb.toString(); } /** * Turn a list of files into a comma separated list of filenames */ public static String getCommaSeparatedListOfFileNames(List fileList) { StringBuffer sb = new StringBuffer(); for (Iterator i = fileList.iterator(); i.hasNext(); ) { sb.append(i.next().getAbsolutePath()); if (i.hasNext()) { sb.append(","); } } return sb.toString(); } /** * Convenience Method for turn a string path containing .js files into a list of files to be processed * * @param sourceDirectory */ public static List directoryToFileList(String sourceDirectory) { File srcDir = new File(sourceDirectory); return getDirFiles(srcDir); } public static List directoryToFileList(File sourceDirectory) { return getDirFiles(sourceDirectory); } private static List getDirFiles(File srcDir) { List files = Lists.newArrayList(); if (srcDir.exists() && srcDir.isDirectory()) { //js文件 File[] filesInSrcDir = srcDir.listFiles((FileFilter) new SuffixFileFilter(".js")); for (File file : filesInSrcDir) { files.add(file); } //目录 File[] dirs = srcDir.listFiles((FileFilter) DirectoryFileFilter.DIRECTORY); for (File file : dirs) { files.addAll(getDirFiles(file)); } } return files; } /** * Convenience Method for turning a string path for a single file into a list of files to be processed * * @param sourceFile */ public static List fileToFileList(String sourceFile) { List files = Lists.newArrayList(); File srcFile = new File(sourceFile); if (srcFile.exists() && srcFile.isFile()) { files.add(srcFile); } return files; } /** * Convenience Method for turning a FileSet into a list of files to be processed */ public static List fileSetToFileList(FileSet fileset) throws IOException { return FileUtilities.getFilesFromFileSet(fileset); } public static void main(String[] args) { // System.out.println(FileUtilities.getCommaSeparatedListOfFileNames(directoryToFileList("/home/ice/IdeaProjects/icedog/src/main/"))); File file = new File("/home/ice/IdeaProjects/icedog/src/main/webapp/javascript/min/a.js"); System.out.println(file.getParentFile().mkdir()); } }





© 2015 - 2025 Weber Informatics LLC | Privacy Policy