`
ErHuo
  • 浏览: 21539 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类
最新评论

Java文件操作类

阅读更多
package util;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.Reader;
import java.io.UnsupportedEncodingException;
import java.io.Writer;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Properties;
import java.util.jar.JarEntry;
import java.util.jar.JarOutputStream;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.io.IOUtils;

public class FileUtil {
	public static String getFilePath(String source_filename,
			String pointFilePath) {
		String ext = "";
		String source_filename_no_ext = source_filename;
		if (source_filename.indexOf(".") > 0) {
			ext = source_filename.substring(source_filename.lastIndexOf("."));
			source_filename_no_ext = source_filename.substring(0,
					source_filename.lastIndexOf("."));
		}

		String uploadRefPath = (pointFilePath == null || pointFilePath.trim()
				.length() == 0) ? getSystemTempPath() : pointFilePath;
		if (uploadRefPath != null
				&& !uploadRefPath
						.endsWith(System.getProperty("file.separator"))
				&& !uploadRefPath.endsWith("/")
				&& !uploadRefPath.endsWith("\\")) {
			uploadRefPath += System.getProperty("file.separator");
		}
		String uploadPath = uploadRefPath;
		File uploadDir = new File(uploadPath);
		if (!uploadDir.exists())
			uploadDir.mkdirs();
		Date today = new Date();
		String formatString = "yyyyMMddHHmmss";
		SimpleDateFormat dateformat = new SimpleDateFormat(formatString);
		String filename = source_filename_no_ext + dateformat.format(today);
		for (int i = 0; i < 10000; i++) {
			String real_filename = filename + "_" + i + ext;
			File file = new File(uploadDir, real_filename);
			if (!file.exists()) {
				return uploadRefPath + real_filename;
			}
		}
		return null;
	}

	/**
	 * 获取系统的临时目录
	 * 
	 * @return
	 */
	public static String getSystemTempPath() {
		return System.getProperty("java.io.tmpdir");
	}

	// ~ Methods add by wubing
	// -------------------------------------------------------------

	/**
	 * 获得指定配置文件属性
	 * 
	 * @param fileName
	 *            文件名称
	 * @return Properties 文件中所包含的所有属性
	 * @throws Exception
	 *             读取属性过程中的任何异常
	 */
	public static Properties getProperties(String fileName) throws Exception {
		Properties initProps = new Properties();
		InputStream in = null;

		try {
			in = getInputStream(fileName);
			initProps.load(in);
		} finally {
			try {
				if (in != null) {
					in.close();
				}
			} catch (Exception e) {
			}
		}

		return initProps;
	}

	/**
	 * 获取指定配置文件属性的读取流,读取路径为本类的CLASSLOADER或者父CLASSLOADER
	 * 
	 * @param fileName
	 *            文件名称
	 * 
	 * @return InputStream 该文件所对应的输入流
	 * @throws Exception
	 *             读取输入流过程中的任何异常
	 */
	public static InputStream getInputStream(String fileName) throws Exception {
		return getInputStream(getFile(getFileURL(fileName).getFile()));
	}

	public static InputStream getInputStream(File file) throws Exception {
		return new BufferedInputStream(new FileInputStream(file));
	}

	/**
	 * 根据文件名获取其在应用中的相对路径
	 * 
	 * @param fileName
	 *            文件名
	 * @return URL 文件在应用中的相对路径
	 * @throws Exception
	 *             获取路径过程中的任何异常
	 */
	public static URL getFileURL(String fileName) throws Exception {
		// 从本类CLASSLOADER相应路径中读取文件
		URL fileURL = FileUtil.class.getClassLoader().getResource(fileName);

		if (fileURL == null) {
			FileUtil.class.getClassLoader().getResource("/" + fileName);
		}

		if (fileURL == null) {
			Thread.currentThread().getContextClassLoader()
					.getResource(fileName);
		}

		if (fileURL == null) {
			fileURL = ClassLoader.getSystemResource(fileName);
		}

		if (fileURL == null) {
			fileURL = new File(fileName).toURL();
		}

		return fileURL;
	}

	/**
	 * 保存属性到文件
	 * 
	 * @param fileName
	 *            文件名
	 * @param prop
	 *            需存储的属性
	 * @throws Exception
	 *             存储属性过程中的任何异常
	 */
	public static void setProperties(String fileName, Properties prop)
			throws Exception {
		FileOutputStream fs = null;
		try {
			fs = new FileOutputStream(getFileURL(fileName).getFile());
			prop.store(fs, "store at " + new Date());
		} finally {
			// 关闭流
			IOUtils.closeQuietly(fs);
		}
	}

	/**
	 * 判断是否需要重新LOAD地址配置文件
	 * 
	 * @param fileName
	 *            文件名
	 * @param lastModify
	 *            原最后修改时间
	 * @return boolean 是否需要重新读取
	 * @throws Exception
	 *             判断重新LOAD过程中的任何异常
	 */
	public static boolean needReload(String fileName, long lastModify)
			throws Exception {
		// 判断文件最后更新时间,决定是否RELOAD
		if (getLastModify(fileName) > lastModify) {
			return true;
		}

		return false;
	}

	/**
	 * 根据文件名获取文件对象,在当前CLASSLOADER路径下寻找文件
	 * 
	 * @param fileName
	 *            文件名
	 * @return File 文件对象
	 * @throws Exception
	 *             获取文件过程中的任何异常
	 */
	public static File getFile(String fileName) throws Exception {
		return new File(getFileURL(fileName).getFile());
	}

	/**
	 * 获取文件最后修改时间
	 * 
	 * @param fileName
	 *            文件名
	 * @return long 文件最后修改时间
	 * @throws Exception
	 *             获取最后修改时间过程中的任何异常
	 */
	public static long getLastModify(String fileName) throws Exception {
		// 获取当前文件最新修改时间
		return getFile(fileName).lastModified();
	}

	/**
	 * 创建一个目录
	 * 
	 * @param dir
	 *            目录路径
	 * @param ignoreIfExitst
	 *            如果已经存在该目录是否忽略
	 * @return boolean 是否创建成功
	 * @throws Exception
	 *             创建目录过程中的任何异常
	 */
	public static boolean createDir(String dir, boolean ignoreIfExitst)
			throws Exception {
		File file = getFile(dir);

		if (ignoreIfExitst && file.exists()) {
			return false;
		}

		return file.mkdir();
	}

	/**
	 * 创建一个目录,如果它的父目录不存在,则自动创建
	 * 
	 * @param dir
	 *            目录路径
	 * @param ignoreIfExitst
	 *            如果已经存在该目录是否忽略
	 * @return boolean 是否创建成功
	 * @throws Exception
	 *             创建目录过程中的任何异常
	 */
	public static boolean createDirs(String dir, boolean ignoreIfExitst)
			throws Exception {
		File file = getFile(dir);

		if (ignoreIfExitst && file.exists()) {
			return false;
		}

		return file.mkdirs();
	}

	/**
	 * 删除文件
	 * 
	 * @param filename
	 *            被删除文件的文件名
	 * @return boolean 是否删除成功
	 * @throws Exception
	 *             删除文件过程中的任何异常
	 */
	public static boolean deleteFile(String filename) throws Exception {
		File file = getFile(filename);

		return deleteFile(file);
	}

	/**
	 * 删除文件
	 * 
	 * @param file
	 *            被删除文件
	 * @return boolean 是否删除成功
	 * @throws Exception
	 *             删除文件过程中的任何异常
	 */
	public static boolean deleteFile(File file) throws Exception {
		if (file.isDirectory()) {
			return deleteDir(file);
		}

		if (!file.exists()) {
			return false;
		}

		return file.delete();
	}

	/**
	 * 删除目录,包括其下的所有子目录和文件
	 * 
	 * @param dir
	 *            被删除的目录名
	 * @return boolean 是否删除成功
	 * @throws Exception
	 *             删除目录过程中的任何异常
	 */
	public static boolean deleteDir(File dir) throws Exception {
		if (dir.isFile()) {
			deleteFile(dir);
		}

		File[] files = dir.listFiles();

		if (files != null) {
			for (int i = 0; i < files.length; i++) {
				File file = files[i];

				if (file.isFile()) {
					file.delete();
				} else {
					deleteDir(file);
				}
			}
		}

		return dir.delete();
	}

	/**
	 * 使用流里的内容创建一个新文件
	 * 
	 * @param stream
	 *            原文件流
	 * @param fileName
	 *            指定的文件路径及文件名
	 * @return File 生成的新文件
	 * @throws Exception
	 *             生成文件过程中的任何异常
	 */
	public static File createFile(InputStream stream, String fileName)
			throws Exception {
		File newFile = new File(fileName);
		OutputStream fileOut = new BufferedOutputStream(new FileOutputStream(
				newFile));
		byte[] buffer = new byte[8192];
		int bytesRead = 0;

		while ((bytesRead = stream.read(buffer, 0, 8192)) != -1) {
			fileOut.write(buffer, 0, bytesRead);
		}

		fileOut.close();
		stream.close();

		return newFile;
	}

	public static void createFile(String output, String content)
			throws Exception {
		try {
			OutputStreamWriter fw = new OutputStreamWriter(
					new FileOutputStream(output));
			PrintWriter out = new PrintWriter(fw);
			out.print(content);
			out.close();
			fw.close();
		} catch (Exception ex) {
			throw new Exception(ex);
		}
	}

	// 获取可写流
	public static Writer openWithWrite(String file, boolean append)
			throws Exception {
		return new BufferedWriter(
				new FileWriter(FileUtil.getFile(file), append));
	}

	// 获取可读流
	public static Reader openWithRead(String file) throws Exception {
		return new BufferedReader(new FileReader(FileUtil.getFile(file)));
	}

	// 获得文件字节流
	public static byte[] getFileBytes(InputStream inputStream) throws Exception {
		ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(
				1024);
		byte[] block = new byte[512];

		while (true) {
			int readLength = inputStream.read(block);

			if (readLength == -1) {
				break; // end of file
			}

			byteArrayOutputStream.write(block, 0, readLength);
		}

		byte[] retValue = byteArrayOutputStream.toByteArray();

		byteArrayOutputStream.close();

		return retValue;
	}

	// 获得文件字节流
	public static byte[] getFileBytes(String file) throws Exception {
		return getFileBytes(getInputStream(file));
	}

	public static void move(String input, String output) throws Exception {
		File inputFile = new File(input);
		File outputFile = new File(output);
		try {
			inputFile.renameTo(outputFile);
		} catch (Exception ex) {
			throw new Exception("Can not mv" + input + " to " + output
					+ ex.getMessage());
		}
	}

	public static boolean copy(String input, String output) throws Exception {
		int BUFSIZE = 0x10000;
		FileInputStream fis = new FileInputStream(input);
		FileOutputStream fos = new FileOutputStream(output);
		try {
			byte buf[] = new byte[BUFSIZE];
			int i;
			while ((i = fis.read(buf)) > -1) {
				fos.write(buf, 0, i);
			}
		} catch (Exception ex) {
			throw new Exception("makeHome" + ex.getMessage());
		} finally {
			fis.close();
			fos.close();
		}
		return true;
	}

	public static void makeHome(String home) throws Exception {
		File homedir = new File(home);
		if (!homedir.exists()) {
			try {
				homedir.mkdirs();
			} catch (Exception ex) {
				throw new Exception("Can not mkdir :" + home
						+ " Maybe include special charactor!");
			}
		}
	}

	public static void copyDir(String sourcedir, String destdir)
			throws Exception {
		File dest = new File(destdir);
		File source = new File(sourcedir);
		String files[] = source.list();
		try {
			makeHome(destdir);
		} catch (Exception ex) {
			throw new Exception("CopyDir:" + ex.getMessage());
		}
		for (int i = 0; i < files.length; i++) {
			String sourcefile = source + File.separator + files[i];
			String destfile = dest + File.separator + files[i];
			File temp = new File(sourcefile);
			if (temp.isFile()) {
				try {
					copy(sourcefile, destfile);
				} catch (Exception ex) {
					throw new Exception("CopyDir:" + ex.getMessage());
				}
			}
		}

	}

	public static void recursiveRemoveDir(File directory) throws Exception {
		if (!directory.exists()) {
			throw new IOException(directory.toString() + " do not exist!");
		}
		String filelist[] = directory.list();
		File tmpFile = null;
		for (int i = 0; i < filelist.length; i++) {
			tmpFile = new File(directory.getAbsolutePath(), filelist[i]);
			if (tmpFile.isDirectory()) {
				recursiveRemoveDir(tmpFile);
			} else if (tmpFile.isFile()) {
				try {
					tmpFile.delete();
				} catch (Exception ex) {
					throw new Exception(tmpFile.toString()
							+ " can not be deleted " + ex.getMessage());
				}
			}
		}

		try {
			directory.delete();
		} catch (Exception ex) {
			throw new Exception(directory.toString() + " can not be deleted "
					+ ex.getMessage());
		} finally {
			filelist = null;
		}
	}

	// 从远程读文件,且保存在本地
	public static boolean remoteFileRead(String sUrl, String path) {
		try {
			URL url = new URL(sUrl);
			URLConnection conn = url.openConnection();
			conn.connect();
			HttpURLConnection httpConn = (HttpURLConnection) conn;
			if (httpConn.getResponseCode() == 200) {
				System.out.println("Connect to " + sUrl
						+ " failed,return code:" + httpConn.getResponseCode());
				return false;
			}
			File file = createFile(conn.getInputStream(), path);
			return true;
		} catch (Exception ex) {
			ex.printStackTrace();
			return false;
		}
	}

	public static String getFileString(String fileName) throws Exception {
		StringBuffer sb = new StringBuffer();
		BufferedReader br = null;
		String line = null;
		try {
			br = new BufferedReader(new InputStreamReader(new FileInputStream(
					fileName)));
			while ((line = br.readLine()) != null) {
				sb.append(line + "\n");
			}
		} finally {
			// 关闭流
			IOUtils.closeQuietly(br);
		}
		return sb.toString();
	}

	public static String getFileString(String fileName, String charSet)
			throws Exception {
		StringBuffer sb = new StringBuffer();
		BufferedReader br = null;
		String line = null;
		try {
			br = new BufferedReader(new InputStreamReader(new FileInputStream(
					fileName), charSet));
			while ((line = br.readLine()) != null) {
				sb.append(line);
			}
		} finally {
			// 关闭流
			IOUtils.closeQuietly(br);
		}
		return sb.toString();
	}

	// 将文本转成流
	public static InputStream stringToInputStream(String str)
			throws UnsupportedEncodingException {
		ByteArrayInputStream stream = new ByteArrayInputStream(str
				.getBytes("UTF-8"));
		return stream;
	}

	/**
	 * 搜索文件
	 * 
	 * @param dir
	 *            起始文件夹
	 * @param s
	 * @return
	 */
	public static File[] searchFiles(String dir, String s) {
		// 起始文件夹
		File file = new File(dir);
		s = s.replace('.', '#');
		s = s.replaceAll("#", "\\\\.");
		s = s.replace('*', '#');
		s = s.replaceAll("#", ".*");
		s = s.replace('?', '#');
		s = s.replaceAll("#", ".?");
		s = "^" + s + "$";
		Pattern p = Pattern.compile(s);
		List<File> list = filePattern(file, p);
		if (list == null) {
			return null;
		}
		File[] rtn = new File[list.size()];
		list.toArray(rtn);
		return rtn;
	}

	/**
	 * @param file
	 *            File 起始文件夹
	 * @param p
	 *            Pattern 匹配类型
	 * @return ArrayList 其文件夹下的文件夹
	 */
	private static List<File> filePattern(File file, Pattern p) {
		if (file == null) {
			return null;
		} else if (file.isFile()) {
			Matcher fMatcher = p.matcher(file.getName());
			if (fMatcher.matches()) {
				List<File> list = new ArrayList<File>();
				list.add(file);
				return list;
			}
		} else if (file.isDirectory()) {
			File[] files = file.listFiles();
			if (files != null && files.length > 0) {
				List<File> list = new ArrayList<File>();
				for (int i = 0; i < files.length; i++) {
					List<File> rlist = filePattern(files[i], p);
					if (rlist != null) {
						list.addAll(rlist);
					}
				}
				return list;
			}
		}
		return null;
	}

	/**
	 * 打jar、war、zip包
	 * 
	 * @param jarFileName
	 * @param inputFilePath
	 * @param base
	 */
	public static void pack(String jarFileName, String inputFilePath,
			String base) {
		JarOutputStream jarOutputStream = null;
		try {
			File inputFile = new File(inputFilePath);
			jarOutputStream = new JarOutputStream(new FileOutputStream(
					jarFileName));
			pack(jarOutputStream, inputFile, base);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				jarOutputStream.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

	private static void pack(JarOutputStream jarOutputStream, File inputFile,
			String base) throws IOException {
		if (inputFile.isDirectory()) {
			File[] files = inputFile.listFiles();
			jarOutputStream.putNextEntry(new JarEntry(base + "/"));
			base = base.length() == 0 ? "" : base + "/";
			for (int i = 0; i < files.length; i++) {
				pack(jarOutputStream, files[i], base + files[i].getName());
			}
		} else {
			jarOutputStream.putNextEntry(new JarEntry(base));
			FileInputStream in = null;
			try {
				in = new FileInputStream(inputFile);
				int b;
				while ((b = in.read()) != -1) {
					jarOutputStream.write(b);
				}
			} finally {
				in.close();
			}
		}
	}
}

分享到:
评论

相关推荐

    java文件操作类

    java文件操作类java文件操作类java文件操作类java文件操作类java文件操作类java文件操作类java文件操作类java文件操作类java文件操作类java文件操作类java文件操作类java文件操作类java文件操作类java文件操作类java...

    java 文件操作类

    下面将详细讨论Java中的文件操作类和相关知识点。 1. **`java.io`包**: `java.io`包是Java中处理输入输出的核心包,包含了众多用于文件操作的类。其中,`File`类是文件和目录路径名的抽象表示,提供了大量的静态...

    JAVA文件操作类和文件夹的操作.doc

    【JAVA文件操作类和文件夹的操作】 在Java编程中,处理文件和文件夹是常见的任务。文件操作类和方法使得开发者能够创建、读取、写入、删除文件以及管理文件夹结构。以下是一些关键的Java类和方法,用于进行文件和...

    Java文件操作封装类

    Java文件操作封装类

    Java文件操作类 File实现代码

    "Java文件操作类File实现代码详解" Java文件操作类File实现代码是Java中最基本的文件操作类,它提供了对文件和目录的创建、重命名、删除、文件列表的操作以及判读是否存在等功能。 概述 Java文件操作类File实现...

    java文件操作类封装

    读取、删除、写文件、文本追加、复制文件、复制文件夹及其下面文件,读取目录下所有文件等操作

    Java 功能丰富的文件操作类.rar

    与大家分享一个功能丰富的Java文件操作类,类中封装了一些常用的文件操作,大部分涉及文件的修改、内容替换等。类中的方法都是静态方法,不需要生成此类的实例, 为避免生成此类的实例,构造方法被申明为private类型...

    java 文件操作

    以上就是Java文件操作的核心知识点。在实际开发中,根据具体需求,可以灵活运用这些方法和类,实现对文件和目录的高效管理。同时,理解并掌握源码能够帮助我们更好地解决问题,而利用工具类能提高开发效率。

    一个常用的Java文件操作类FileUtil.java源码下载

    一个常用的Java文件操作类FileUtil.java源代码,类中的所有方法都是静态方法,不需要生成此类的实例,这些Java文件操作类主要有修改文件的最后访问时间、判断指定的文件是否存在、创建指定的目录、清空指定目录中的...

    java文件操作工具类

    java文件操作工具类是java中针对文件操作的一个工具类,可以方便的读取,下载,上传文件等操作,希望可以帮到大家。

    有关java文件操作的类和用法

    总的来说,理解和熟练使用Java的文件操作类是任何Java开发者必备的技能之一。无论是处理文本文件还是二进制文件,无论是简单的读写还是复杂的流操作,这些类都能提供强大的支持。记住,始终确保在操作完成后正确关闭...

    java操作文件工具类

    文件工具类java操作文件工具类java操作文件工具类java操作文件工具类java操作文件工具类

    java文件操作

    Java文件操作是编程中不可或缺的一部分,尤其是在处理I/O流、文件读写以及文件管理时。Apache Commons IO是一个非常实用的库,它为Java开发者提供了大量的工具类和方法,以简化文件操作。在这个场景中,我们关注的是...

    java实现对文件的各种操作的工具类.md

    # java实现对文件的各种操作的工具类 ## 可以实现的操作有: 1. 删除单个文件 2. 删除文件夹及文件夹下的文件 3. 使用文件流对单个文件进行复制 4. 复制整个文件夹内容(包含子文件夹中的所有内容) 5. ...

    java视频教程—Java文件操作

    java视频教程 Java文件操作 JavaFile

    java 文件操作工具类

    java 文件操作工具类

    java properties文件操作工具类,可追加修改

    此工具类只用于Java后端在操作Properties文件的时候写的工具类,方便properties文件的存取操作

Global site tag (gtag.js) - Google Analytics