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

📄 fileutilities.java

📁 java 调用windows的api
💻 JAVA
字号:
package org.jawin.browser.util;

import java.io.*;
import java.util.Properties;
import org.jawin.browser.log.Log;

/**
 * Bulk standard file utiltities
 *
 * <p>Title: Jawin Code Generation GUI</p>
 * <p>Description: GUI for exploring type libraries and generating Java code</p>
 * <p>Copyright: Copyright (c) 2003</p>
 * <p>Company: Open Source Incentive</p>
 *
 * @author Josh Passenger
 * @version 1.0
 */

public class FileUtilities
{
	public static final int BUFFER_SIZE = 4096;

	/**
	 * Load a file as a byte []
	 *
	 * @param fileName the file to load
	 * @return the loaded bytes
	 * @throws IOException throw an IOException if things go pear shaped
	 */
	public static byte [] loadBytes(String fileName) throws IOException
	{
		byte [] buffer = new byte [BUFFER_SIZE];
		FileInputStream in = null;
		ByteArrayOutputStream out = new ByteArrayOutputStream();

		try
		{
			in = new FileInputStream(fileName);

			int bytesRead = in.read(buffer);

			while (bytesRead > 0)
			{
				out.write(buffer, 0, bytesRead);
				bytesRead = in.read(buffer);
			}
		}
		finally
		{
			if (in != null)
			{
				try
				{
					in.close();
				}
				catch (Exception ex)
				{
					Log.getInstance().exception("FileUtilities.loadBytes() failed to close stream to file: " + fileName, ex);
				}
			}
		}

		return out.toByteArray();
	}

	/**
	 * Load a file as a String
	 *
	 * @param fileName the file to load
	 * @return the contents of thefile as a String
	 * @throws throw an IOException if anything goes wrong
	 */
	public static String loadString(String fileName) throws IOException
	{
		return new String(loadBytes(fileName));
	}

	/**
	 * Load a properties file from disk
	 * @param fileName the file to load from
	 * @return the loaded Properties file
	 * @throws IOException thrwon if a non properties or non-existant file is specified
	 */

	public static Properties loadProperties(String fileName) throws IOException
	{
		Properties prop = new Properties();
		FileInputStream in = null;

		try
		{
			in = new FileInputStream(fileName);
			prop.load(in);
		}
		finally
		{
			if (in != null)
			{
				try
				{
					in.close();
				}
				catch (Exception ex)
				{
					Log.getInstance().exception("FileUtilities.loadProperties() failed to close stream to file: " + fileName, ex);
				}
			}
		}

		return prop;
	}

	public static void writeString(String data, String encoding, String fileName) throws IOException
	{
            if(encoding!=null && encoding.compareToIgnoreCase("Unicode")==0)
            {
		writeChars(data.toCharArray(), fileName);
            }
            else
            {
		writeBytes(data.getBytes(), fileName);
            }
	}

	public static void writeChars(char [] chars, String fileName) throws IOException
	{
		FileOutputStream out = null;
		OutputStreamWriter osw = null;

		try
		{
			out = new FileOutputStream(fileName);
                        osw = new OutputStreamWriter(out, "Unicode");
			osw.write(chars);
		}
		finally
		{
			if (osw != null)
			{
				try
				{
					osw.close();
				}
				catch (Exception ex)
				{

				}
			}
		}
	}

	public static void writeBytes(byte [] bytes, String fileName) throws IOException
	{
		FileOutputStream out = null;

		try
		{
			out = new FileOutputStream(fileName);
			out.write(bytes);
		}
		finally
		{
			if (out != null)
			{
				try
				{
					out.close();
				}
				catch (Exception ex)
				{

				}
			}
		}
	}

	/**
	 * Attempts to make the directory and its parents if it does not already exist
	 *
	 * @param file the file to craete a directory for
	 * @throws IOException if the directory cannot be created
	 */
	public static void createDirectory(File file) throws IOException
	{
		File parent = file.getParentFile();

		if (parent.isDirectory() && parent.exists())
		{
			return;
		}

		parent.mkdirs();
	}

}

⌨️ 快捷键说明

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