package org.apache.cxf.helpers; import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.FileReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.List; import java.util.Locale; import java.util.regex.Matcher; import java.util.regex.Pattern; import org.apache.cxf.common.util.SystemPropertyAction; public final class FileUtils { private static final int RETRY_SLEEP_MILLIS = 10; private static File defaultTempDir; private FileUtils() { } public static synchronized File getDefaultTempDir() { if (defaultTempDir != null && defaultTempDir.exists()) { return defaultTempDir; } String s = SystemPropertyAction.getPropertyOrNull(FileUtils.class.getName() + ".TempDirectory"); if (s != null) { //assume someone outside of us will manage the directory File f = new File(s); if (f.mkdirs()) { defaultTempDir = f; } } if (defaultTempDir == null) { int x = (int)(Math.random() * 1000000); s = SystemPropertyAction.getProperty("java.io.tmpdir"); File checkExists = new File(s); if (!checkExists.exists() || !checkExists.isDirectory()) { throw new RuntimeException("The directory " + checkExists.getAbsolutePath() + " does not exist, please set java.io.tempdir" + " to an existing directory"); } if (!checkExists.canWrite()) { throw new RuntimeException("The directory " + checkExists.getAbsolutePath() + " is now writable, please set java.io.tempdir" + " to an writable directory"); } File f = new File(s, "cxf-tmp-" + x); while (!f.mkdir()) { x = (int)(Math.random() * 1000000); f = new File(s, "cxf-tmp-" + x); } defaultTempDir = f; final File f2 = f; Thread hook = new Thread() { @Override public void run() { removeDir(f2, true); } }; Runtime.getRuntime().addShutdownHook(hook); } return defaultTempDir; } public static void mkDir(File dir) { if (dir == null) { throw new RuntimeException("dir attribute is required"); } if (dir.isFile()) { throw new RuntimeException("Unable to create directory as a file " + "already exists with that name: " + dir.getAbsolutePath()); } if (!dir.exists()) { boolean result = doMkDirs(dir); if (!result) { String msg = "Directory " + dir.getAbsolutePath() + " creation was not successful for an unknown reason"; throw new RuntimeException(msg); } } } /** * Attempt to fix possible race condition when creating directories on * WinXP, also Windows2000. If the mkdirs does not work, wait a little and * try again. */ private static boolean doMkDirs(File f) { if (!f.mkdirs()) { try { Thread.sleep(RETRY_SLEEP_MILLIS); return f.mkdirs(); } catch (InterruptedException ex) { return f.mkdirs(); } } return true; } public static void removeDir(File d) { removeDir(d, false); } private static void removeDir(File d, boolean inShutdown) { String[] list = d.list(); if (list == null) { list = new String[0]; } for (int i = 0; i < list.length; i++) { String s = list[i]; File f = new File(d, s); if (f.isDirectory()) { removeDir(f, inShutdown); } else { delete(f, inShutdown); } } delete(d, inShutdown); } public static void delete(File f) { delete(f, false); } public static void delete(File f, boolean inShutdown) { if (!f.delete()) { if (isWindows()) { System.gc(); } try { Thread.sleep(RETRY_SLEEP_MILLIS); } catch (InterruptedException ex) { // Ignore Exception } if (!f.delete() && !inShutdown) { f.deleteOnExit(); } } } private static boolean isWindows() { String osName = SystemPropertyAction.getProperty("os.name").toLowerCase(Locale.US); return osName.indexOf("windows") > -1; } public static File createTempFile(String prefix, String suffix) throws IOException { return createTempFile(prefix, suffix, null, false); } public static File createTempFile(String prefix, String suffix, File parentDir, boolean deleteOnExit) throws IOException { File result = null; File parent = (parentDir == null) ? getDefaultTempDir() : parentDir; if (suffix == null) { suffix = ".tmp"; } if (prefix == null) { prefix = "cxf"; } else if (prefix.length() < 3) { prefix = prefix + "cxf"; } result = File.createTempFile(prefix, suffix, parent); //if parentDir is null, we're in our default dir //which will get completely wiped on exit from our exit //hook. No need to set deleteOnExit() which leaks memory. if (deleteOnExit && parentDir != null) { result.deleteOnExit(); } return result; } public static String getStringFromFile(File location) { InputStream is = null; String result = null; try { is = new FileInputStream(location); result = normalizeCRLF(is); } catch (Exception e) { e.printStackTrace(); } finally { if (is != null) { try { is.close(); } catch (Exception e) { //do nothing } } } return result; } public static String normalizeCRLF(InputStream instream) { BufferedReader in = new BufferedReader(new InputStreamReader(instream)); StringBuilder result = new StringBuilder(); String line = null; try { line = in.readLine(); while (line != null) { String[] tok = line.split("\\s"); for (int x = 0; x < tok.length; x++) { String token = tok[x]; result.append(" " + token); } line = in.readLine(); } } catch (Exception ex) { ex.printStackTrace(); } String rtn = result.toString(); rtn = ignoreTokens(rtn, "<!--", "-->"); rtn = ignoreTokens(rtn, "/*", "*/"); return rtn; } private static String ignoreTokens(final String contents, final String startToken, final String endToken) { String rtn = contents; int headerIndexStart = rtn.indexOf(startToken); int headerIndexEnd = rtn.indexOf(endToken); if (headerIndexStart != -1 && headerIndexEnd != -1 && headerIndexStart < headerIndexEnd) { rtn = rtn.substring(0, headerIndexStart - 1) + rtn.substring(headerIndexEnd + endToken.length() + 1); } return rtn; } public static List<File> getFiles(File dir, final String pattern) { return getFiles(dir, pattern, null); } public static List<File> getFilesRecurse(File dir, final String pattern) { return getFilesRecurse(dir, pattern, null); } public static List<File> getFiles(File dir, final String pattern, File exclude) { return getFilesRecurse(dir, Pattern.compile(pattern), exclude, false, new ArrayList<File>()); } public static List<File> getFilesRecurse(File dir, final String pattern, File exclude) { return getFilesRecurse(dir, Pattern.compile(pattern), exclude, true, new ArrayList<File>()); } private static List<File> getFilesRecurse(File dir, Pattern pattern, File exclude, boolean rec, List<File> fileList) { for (File file : dir.listFiles()) { if (file.equals(exclude)) { continue; } if (file.isDirectory() && rec) { getFilesRecurse(file, pattern, exclude, rec, fileList); } else { Matcher m = pattern.matcher(file.getName()); if (m.matches()) { fileList.add(file); } } } return fileList; } public static List<String> readLines(File file) throws Exception { if (!file.exists()) { return new ArrayList<String>(); } BufferedReader reader = new BufferedReader(new FileReader(file)); List<String> results = new ArrayList<String>(); try { String line = reader.readLine(); while (line != null) { results.add(line); line = reader.readLine(); } } finally { reader.close(); } return results; } }
相关推荐
实现文件的创建、删除、复制、压缩、解压以及目录的创建、删除、复制、压缩解压等功能
,复制单个文件到指定路径,复制整个文件夹到指定路径,复制文件夹下所有文件到指定路径,删除单个文件,删除文件夹下所有文件,删除文件夹以及文件下下所有文件。。。等
支持多线程上传下载,支持断点续传功能的一个工具类。
删除文件 文件名称验证 检查文件是否可下载 下载文件名重新编码 返回文件名 是否为Windows或者Linux(Unix)文件分隔符,Windows平台下分隔符为\,Linux(Unix)为/ 百分号编码工具方法
QrcodeUtils.java\防止SQL注入和XSS攻击Filter\获取文件绝对路径最后的文件夹名称\加密工具类 - CryptoUtils.java\日期工具类 - DateUtil.java\图片处理工具类 - ImageUtils.java\文件相关操作工具类——FileUtils....
文件工具类FileUtils,对文件中内容行数lines的总数统计
D:\002 我的工具类\001 流\文件操作整体2\FileUtils.java D:\002 我的工具类\001 流\文件操作整体2\IOUtils.java D:\002 我的工具类\001 流\文件操作整体2\PropertiesUtil.java D:\002 我的工具类\0013数字 D:\002 ...
FileUtils.java 文件处理工具类
通过输入文件地址和目标地址,对文件的复制操作,通过输入File对象和目标File对象,对文件的辅助操作
java web 上传下载工具类,压缩包内包含src和WebRoot,直接新建项目,然后复制这两个目录内的文件,覆盖新建项目中的文件即可。用法参见test用例
- 由第三方研发的工具类 - 要使用commons-io工具包,就需要先从第三方下载该工具包 - 在当前项目工程下,导入commons-io工具包...FileUtils工具类: 复制目录或文件(多个文件) commons-io可以简化IO复制文件的操作
FileUtils 文件操作 HanziToPinyin 拼音汉字处理 IOUtils IOUtils MD5 MD5 MiscUtils 设备信息的获取 NetWorkUtils 网络状态 PhoneUtil 手机组件调用工具类 PreferencesUtils sp工具类 RandomUtils 随机数工具类 ...
关于文件操作工具类相关方法介绍,手工打造描述,请多多指教
规范的文件操作类 FileUtils:writeFile readFile.
fileutils fileutils具有实用程序功能,可以读取,写入和同步文件。用例写文件: write("/tmp/myfile.txt", std::string_view{"Hello, world!"});将文件同步到存储: sync("/tmp/myfile.txt");读取文件: std::...
FileUtils 文件工具类 JExcelUtils excel 工具类2 JsonUtil json 工具类 MyBeanUtils 实体bean 工具 PathUtils 获取路径工具 Pinyin4jUtil 提取汉字拼音的工具 StringUtil 字符转换类 UploadQueue 文件上传队列 ...