⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 fileutil.java

📁 create the email in the server
💻 JAVA
📖 第 1 页 / 共 2 页
字号:

package za.co.halo.SecureCommunications.util;

import java.beans.XMLDecoder;
import java.beans.XMLEncoder;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.EOFException;
import java.io.File;
import java.io.FileFilter;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.Collection;
import java.util.List;
import java.util.Vector;

import za.co.halo.SecureCommunications.compress.CompressionStream;
import za.co.halo.SecureCommunications.encryption.EncryptionStream;

/**
 * This class contains static methods to manipulate string
 * presentations of file names
 */
public class FileUtil
{
	public static void deltree(File f)
	{
//		Bug.pr("isDir" + f.isDirectory());

		File[] files = f.listFiles(new FileFilter()
		{
			public boolean accept(File arg0)
			{
				return true;
			}
		});
		// Bug.pr(files);

		for (int i = 0; i < files.length; i++)
		{
//			Bug.pr(files[i]);
			if (files[i].isDirectory())
				deltree(files[i]);
			files[i].delete();
		}
	}

	/**
	 * Returns the basename of the given file.
	 * 
	 * @param f
	 *           The file for wehich the base name is to be
	 *           returned.
	 * @return the basename of the file.
	 */
	public static String getBaseName(File f)
	{
		return getBaseName(f.getName());
	}

	/**
	 * Returns the part of the string before and excluding
	 * the last period. Useful for extracting the base names
	 * from files with extensions. For example, given
	 * "c:/dir/file.ext" the method will return "file".
	 * 
	 * @param path
	 * @return part of String
	 */
	public static String getBaseName(String path)
	{
		int index, a, b;
		a = path.lastIndexOf("\\");
		b = path.lastIndexOf("/");
		if (a > b)
			index = a + 1;
		else if (a < b)
			index = b + 1;
		else
			index = 0;

		int index2 = path.lastIndexOf(".");
		if (index2 == -1 || index2 < index)
			index2 = path.length();

		return path.substring(index, index2);
	}

	/**
	 * Returns the directory part of the filename, using the
	 * indicated sperator to distinguish between the
	 * directories.
	 * 
	 * @param filename
	 *           the file name to process
	 * @param seperator
	 *           the seperator to use to distinguish
	 *           directories
	 * 
	 * @return the directory part of the filename
	 */
	public static String getDir(String filename, char seperator)
	{
		int i = filename.length() - 1;
		char c = filename.charAt(i);
		String s;

		while (c != seperator)
		{
			// s = "" + c + s;
			i--;
			if (i >= 0)
				c = filename.charAt(i);
			else
				break;

		}

		s = filename.substring(0, i + 1);
		return s;
	}

	/**
	 * Returns the directory path to the file. Receives the
	 * path as a String and then searches for the last / or \
	 * and returns the String up to that point.
	 * 
	 * @param path
	 *           The path of the file.
	 * @return The directory up to the file specified in the
	 *         parameter. Returns a null if a / or a \ does
	 *         not occur.
	 */
	public static String getDirPath(String path)
	{
		int index, index2;
		index = path.lastIndexOf("\\");
		index2 = path.lastIndexOf("/");
		if (index > index2)
			return path.substring(0, index);
		else if (index < index2)
			return path.substring(0, index2);
		else
			return null;
	}

	/**
	 * Returns the extension of the file.
	 * 
	 * @param f
	 *           The file for which to return the extension
	 *           of.
	 * 
	 * @return a String containing the file extensions if it
	 *         has one, otherwise null.
	 */
	public static String getExtension(File f)
	{

		return getExtension(f.getName());

	}

	/**
	 * Returns the part of the string after the last period.
	 * Useful for extraxting the extension of a file.
	 * 
	 * @param s
	 *           The String for which this operation is
	 *           performed.
	 * @return a String containing the part of the string
	 *         after the lst period.
	 */
	public static String getExtension(String s)
	{
		String ext = null;
		int i = s.lastIndexOf('.');

		if (i > 0 && i < s.length() - 1)
		{
			ext = s.substring(i + 1).toLowerCase();
		}
		return ext;
	}

	/**
	 * Returns the file in the specified path with the
	 * specified base name and using one of the extensions
	 * provided in the Vector
	 * 
	 * @param path
	 *           The Path (excluding the file name) to the
	 *           file
	 * @param basename
	 *           The base name of the file (excluding the
	 *           directory path and the extension).
	 * @param extensions
	 *           The extensions to search for in a Vector of
	 *           Strings. The extensions should not include
	 *           the period, that is "jpg" but not ".jpeg".
	 * @return A File if one of the extensions matches with a
	 *         base name in the specified path.
	 */
	public static File getFile(String path, String basename, Vector<String> extensions)
	{
		File file;
		String link;
		for (String str : extensions)
		{
			if (path != null)
				link = path + "/" + basename + "." + str;
			else
				link = basename + "." + str;
			// Bug.pr("Testing "+link);
			file = new File(link);
			if (file.exists())
				return file;
		}
		return null;
	}

	// static {
	// System.loadLibrary("FileInformation");
	// }
	/**
	 * 
	 * @param file
	 */
	public static long getLastAccessed(File file)
	{
		// String file_s = file.getAbsolutePath();
		// return getWin32SystemLastAccessed(file_s);
		return file.lastModified();
	}

	/**
	 * Reads a serialised object from the given file. It is
	 * assumed that only one object has been written to the
	 * file.
	 * 
	 * @param f
	 *           The file to read.
	 * @return the object that was serialised in the file.
	 * @throws ClassNotFoundException
	 * @throws IOException
	 */
	public static byte[] readByteArray(File f) throws ClassNotFoundException, IOException
	{
		FileInputStream fin = new FileInputStream(f);
		ObjectInputStream in = new ObjectInputStream(fin);

		byte[] byteArray = (byte[]) in.readObject();
		in.close();
		fin.close();
		return byteArray;

	}
	
	public static Vector<Object> readCollectionFromFile(File f) throws ClassNotFoundException,
			IOException
	{
		Vector<Object> objects = new Vector<Object>();

		FileInputStream fin = new FileInputStream(f);
		ObjectInputStream in = new ObjectInputStream(fin);
		Object o = null;
//		Bug.pr("Reading from file " + f.getAbsolutePath());
//		Bug.pr("Available " + in.available());
		try
		{
			while (/* in.available() > 0) */true)
			{
				o = in.readObject();
				// Bug.pr("Read - " + o.toString());
				objects.addElement(o);
			}
		}
		catch (IOException ioex)
		{
			in.close();
			fin.close();
			// Bug.pr("Objects read " + objects.size());
		}
		// Bug.pr(objects);
		return objects;
	}

	/**
	 * Reads a serialised object from the given file. It is
	 * assumed that only one object has been written to the
	 * file.
	 * 
	 * @param f
	 *           The file to read.
	 * @return the object that was serialised in the file.
	 * @throws ClassNotFoundException
	 * @throws IOException
	 */
	public static<T> T readFile(File f) throws ClassNotFoundException, IOException
	{
		FileInputStream fin = new FileInputStream(f);
		ObjectInputStream in = new ObjectInputStream(fin);
		Object o = in.readObject();
		in.close();
		fin.close();
		return (T) o;
	}
	
	/**
	 * Reads a serialised object from the given file. It is
	 * assumed that only one object has been written to the
	 * file.
	 * 
	 * @param f
	 *           The file to read.
	 * @return the object that was serialised in the file.
	 * @throws ClassNotFoundException
	 * @throws IOException

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -