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

📄 fileutil.java

📁 create the email in the server
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
	 */
	public static<T> T readEncryptedFile(File f, String password) throws ClassNotFoundException, IOException
	{
		EncryptionStream e = new EncryptionStream();
		FileInputStream fin = new FileInputStream(f);
//		e.getEncryptedInputStream(fin,password);
		ObjectInputStream in = new ObjectInputStream(e.getEncryptedInputStream(fin,password));
		Object o = in.readObject();
		in.close();
		fin.close();
		return (T) o;
	}

	/**
	 * Reads a text file and returns the contents in a
	 * String.
	 * 
	 * @deprecated Use serialization instead.
	 * @param filename
	 *           The file to read
	 * @return a String conataning all the text in the text
	 *         file.
	 * @throws IOException
	 */
	public static String readFile(String filename) throws IOException
	{
		Stream stream = new Stream(filename, Stream.READ);
		String s = "";

		while (true)
		{
			try
			{
				s += stream.readString();
			}
			catch (EOFException e)
			{
				stream.close();
				return s;
			}
		}
	}

	/**
	 * Removes the entire directory from a full file name.
	 * The default seperator "\" is used to distinguish
	 * directories.
	 * 
	 * @param filename
	 *           the file name to process
	 * 
	 * @return the file name (without the directory name)
	 */
	public static String removeDir(String filename)
	{
		return removeDir(filename, '\\');
	}

	/**
	 * Removes the entire directory from a full fle name,
	 * using the specified seperator to distinguish
	 * directories.
	 * 
	 * @param filename
	 *           the file name to process
	 * @param seperator
	 *           The character that serves as a directory
	 *           seperator on the local system.
	 * 
	 * @return the file name (without the directory name)
	 */
	public static String removeDir(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;
		}

		return s;
	}

	/**
	 * For a full path name "c:\dir\fil" only "dir\fil" is
	 * returned, and "dir\fil", "fil" is returned. Only for
	 * windows systems.
	 * 
	 * 
	 * @param fileName
	 *           the (absolute) filename
	 * @return the absolute filename without the root
	 *         directory
	 * 
	 */
	public static String removeRoot(String fileName)
	{
		int i;
		boolean found = false;

		for (i = 0; i < fileName.length(); i++)
		{
			if (fileName.charAt(i) == '\\')
			{
				found = true;
				break;
			}
		}

		if (found)
		{
			return fileName.substring(i + 1, fileName.length());
		}
		else
			return "";

	}

	/**
	 * Removes the number of directories from a full file
	 * name as specified by level. Only for windows systems.
	 * 
	 * removeRoot("c:/sys/temp/fol/fil", 2) will return
	 * "temp/fol/fil".
	 * 
	 * 
	 * @param filename
	 *           the (absolute) filename
	 * @param level
	 *           the number of directories to remove from the
	 *           file name (starting with the root)
	 * 
	 * @return the absolute filename without the root
	 *         directory
	 */

	public static String removeRoot(String filename, int level)
	{
		String s = filename;

		for (int i = 0; i < level; i++)
			s = removeRoot(s);

		return s;
	}

	/**
	 * Serialises a single byte array to a file.
	 * 
	 * @param f
	 *           The file to which the object is written.
	 * @param obj
	 *           The object to serialise
	 * @throws IOException
	 */
	public static void write(File f, byte[] obj) throws IOException
	{
		FileOutputStream fout = new FileOutputStream(f);
		ObjectOutputStream out = new ObjectOutputStream(fout);

		out.writeObject(obj);
		out.close();
		fout.close();
	}
	
	public static void writeEncryptedFile(File f, Serializable obj, String password) throws IOException
	{
		EncryptionStream e = new EncryptionStream();
		FileOutputStream fout = new FileOutputStream(f);
		
		ObjectOutputStream out = new ObjectOutputStream(e.getEncryptedOutputStream(fout,password));

		out.writeObject(obj);
		out.close();
		fout.close();
	}	

	public static void writeCompressedFile(File f, Serializable obj) throws IOException
	{
		CompressionStream c = new CompressionStream();
		
		FileOutputStream fout = new FileOutputStream(f);
		
		ObjectOutputStream out = new ObjectOutputStream(c.getCompressedOutputStream(fout));

		out.writeObject(obj);
		out.flush();
		out.close();
		fout.close();
	}	

	public static Object readCompressedFile(File f) throws IOException, ClassNotFoundException
	{
		CompressionStream c = new CompressionStream();
		
		FileInputStream fin = new FileInputStream(f);
		
		ObjectInputStream in = new ObjectInputStream(c.getCompressedInputStream(fin));

		Object io = null;
		try {
			io = in.readObject();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		in.close();
		fin.close();
		return io;
	}	
	
	/**
	 * Serialises a single object to a file.
	 * 
	 * @param f
	 *           The file to which the object is written.
	 * @param obj
	 *           The object to serialise
	 * @throws IOException
	 */
	public static void write(File f, Serializable obj) throws IOException
	{
		FileOutputStream fout = new FileOutputStream(f);
		ObjectOutputStream out = new ObjectOutputStream(fout);

		out.writeObject(obj);
		out.close();
		fout.close();
	}

	// private native static long
	// getWin32SystemLastAccessed(String file);

	public static void writeCollectionToFile(Collection objects, File f) throws IOException
	{
		// Vector<Object> objects = new Vector<Object>();

		FileOutputStream fout = new FileOutputStream(f);
		ObjectOutputStream out = new ObjectOutputStream(fout);
		// Bug.pr("Writing to file " + f.getAbsolutePath());
		// Bug.pr("Available " + objects.size());
		for (Object o : objects)
		{
			out.writeObject(o);
			// Bug.pr("Written " + o);
		}
		out.close();
		fout.close();
	}

	/**
	 * Writes a collection of elements to a text file. The
	 * objects' toString methods are used. Each object is
	 * written on a new line.
	 * 
	 * @param <T>
	 *           The type of elements in the collection
	 * @param objects
	 *           The collection of objects to write
	 * @param f
	 *           The file to write to
	 * @throws FileNotFoundException
	 * @throws IOException
	 */
	public static <T> void writeCollectionToTextFile(Collection<T> objects, File f) throws FileNotFoundException, IOException
	{
		Stream s = new Stream(f, Stream.WRITE);
		
		for(T object:objects)
			s.println(object.toString());
		
		s.close();
	}
	
	public static Object readXML(File f) throws FileNotFoundException
	{
      XMLDecoder d = new XMLDecoder(
				new BufferedInputStream(
						new FileInputStream(f)));
		Object result = d.readObject();
		d.close();
		
		return result;
	}
	
	/**
	 * Writes a list of objects in XML format to a file. The
	 * objects are read and written one by one.
	 * 
	 * @param list
	 *           The list of objects to write.
	 * @param filename
	 *           The name of the file to write to.
	 * @throws FileNotFoundException
	 */
	public static <E extends Object> void writeXML(List<E> list, String filename)
			throws FileNotFoundException
	{
		XMLEncoder e = new XMLEncoder(new BufferedOutputStream(new FileOutputStream(filename)));

		for (E object : list)
		{
			e.writeObject(object);
		}
		
		e.close();
	}

	public static void writeXML(Object ob, File f) throws FileNotFoundException
	{
      XMLEncoder e = new XMLEncoder(new BufferedOutputStream( new FileOutputStream(f)));
		e.writeObject(ob);
		e.close();
	}
}

⌨️ 快捷键说明

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