📄 utilities.java
字号:
// informationMessage("done.");
// }
// catch (IOException e) {
// throw e;
// }
// catch (Exception e) {
// throw e;
// }
// finally {
// if (in != null) {
// try {
// in.close();
// }
// catch (IOException e) {
// in = null;
// throw e;
// }
// in = null;
// }
// if (out != null) {
// try {
// out.close();
// }
// catch (IOException e) {
// out = null;
// throw e;
// }
// out = null;
// }
// }
// return true;
// }
/**
* Create a directory, plus all parent directories necessary to contain it.
* For example, if c:\this\is\an\example\file.name was encapsulated in File,
* and c:\this existed but contained no subdirectories, then the remaining
* directories is\an\example\ would all be created.
*
* @param f Directory path to create.
* @return true if successful.
*/
public static boolean createDirectoryRecursively(File f) {
if (f==null) return false;
if (f.exists()) {
return true;
}
if (!f.isAbsolute()) {
f = new File(f.getAbsolutePath());
}
String par = f.getParent();
if (par == null) {
return false;
}
if (!createDirectoryRecursively(new File(par))) {
return false;
}
f.mkdir();
return f.exists();
}
/**
* Description of the Method
*
* @param fileName Description of Parameter
* @return Description of the Returned Value
*/
public static final Properties readPropertiesFromFile(Frame parentFrame, File file) {
try {
return readPropertiesFromFile(parentFrame, file.getAbsolutePath());
}
catch (Exception e) {
// errorMessage(parentFrame, "Error reading properties file: " + file);
e.printStackTrace();
return null;
}
}
public static final Properties readPropertiesFromFile(Frame parentFrame, String fileName) {
try {
return readPropertiesFromFile(fileName);
}
catch (Exception e) {
// errorMessage(parentFrame, "Error reading properties file: " + fileName);
e.printStackTrace();
return null;
}
}
public static final Properties readPropertiesFromFile(String fileName) throws IOException, Exception {
File propFile = null;
FileInputStream in = null;
Properties properties = new Properties();
try {
// informationMessage("Looking for properties file: " + fileName);
propFile = new File(fileName);
if (propFile.exists()) {
in = new FileInputStream(propFile);
// informationMessage("Reading properties file: " + fileName);
properties.load(in);
}
else {
// informationMessage("Missing properties file: " + fileName);
}
}
catch (IOException e) {
throw e;
}
catch (Exception e) {
throw e;
}
finally {
if (in != null) {
try {
in.close();
}
catch (IOException e) {
in = null;
throw e;
}
in = null;
}
}
return properties;
}
// =========================================================================
/**
* Description of the Method
*
* @param properties Description of Parameter
* @param fileName Description of Parameter
*/
public static final void writePropertiesToFile(Frame parentFrame, Properties properties, File file, String fileHeader) {
try {
writePropertiesToFile(parentFrame, properties, file.getAbsolutePath(), fileHeader);
}
catch (Exception e) {
// errorMessage(parentFrame, "Error writing properties file: " + file);
e.printStackTrace();
}
}
public static final void writePropertiesToFile(Frame parentFrame, Properties properties, String fileName, String fileHeader) {
try {
writePropertiesToFile(properties, fileName, fileHeader);
}
catch (Exception e) {
// errorMessage(parentFrame, "Error writing properties file: " + fileName);
e.printStackTrace();
}
}
public static final void writePropertiesToFile(Properties properties, String fileName, String fileHeader) throws IOException, Exception {
FileOutputStream out = null;
try {
if (properties != null) {
out = new FileOutputStream(fileName);
// informationMessage("Writing properties file: " + fileName);
System.out.println("Writing properties file: " + fileName);
// should use store(), but it's missing from Java 1.1
properties.save(out, fileHeader);
}
}
catch (IOException e) {
throw e;
}
catch (Exception e) {
throw e;
}
finally {
if (out != null) {
try {
out.close();
}
catch (IOException e) {
out = null;
throw e;
}
out = null;
}
}
}
// =========================================================================
/**
* This is the preferred way to access resources in order to be compatible
* with 1)normal execution, 2)running from a jar, and 3)running from Webstart.
* This requires that the name include the full package directory structure.
* For example if root package is c:\proj\src\rootpkg, and icons are in
* c:\proj\src\rootpkg\subpackage1\icons\ then an icon named 'anIcon.gif'
* would be referenced using the following string:
* rootpkg/subpackage1/icons/anIcon.gif
*
*@param fileName Description of Parameter
*@return An ImageIcon instance if icon is found or null if resource is not found.
*@see For naming issues see
* http://java.sun.com/products/javawebstart/docs/developersguide.html#dev
* and http://www.vamphq.com/resources.html
*/
// public static final ImageIcon getIcon(String fileName) {
// URL url = null;
// if (fileName == null) {
// return null;
// }
// else {
// url = Utils.class.getClassLoader().getResource(fileName);
// }
// return(url == null) ? null : new ImageIcon(url);
// }
// =========================================================================
// public static final String fixTrailingBackSlash(String path) {
// if (path == null)
// return path;
// else
// return(path + (path.endsWith("\\") ? "" : "\\"));
// }
// ==============================================================================
// Debug & Logging utils
// ==============================================================================
// public static final boolean getLogMessageStatus() {
// return m_logMessages;
// }
// public static final void setLogMessageStatus(boolean status) {
// m_logMessages = status;
// }
//
// public static final String getLogfilePath() {
// return m_logfilePath;
// }
// public static final void setLogfilePath(String path) {
// m_logfilePath = path;
// }
// public static final String getLogfileName() {
// return m_logfileName;
// }
// public static final void setLogfileName(String filename) {
// m_logfileName = filename;
// }
// // =========================================================================
// /**
// * Initialize logging
// *
// * @param path path for log file
// * @param filename name of log file
// * @param deleteIfExists delete old logfile if it exists
// */
// public static final void initLogging(String path, String filename, boolean deleteIfExists) {
// File logFile = new File(path + filename);
// if (deleteIfExists && logFile.exists() && logFile.isFile()) {
// logFile.delete();
// }
// setLogfilePath(path);
// setLogfileName(filename);
// setLogMessageStatus(true);
// }
//
// // =========================================================================
// /**
// * Prints a debug message if in developer mode.
// *
// * @param msg Description of Parameter
// */
// public static final void debugMessage(String msg) {
// if (developerVersion || displayToConsole) {
// System.out.println(msg);
// }
// logMessage(msg);
// }
//
// /**
// * Prints a debug message if in developer mode.
// *
// * @param e Description of Parameter
// */
// public static final void debugMessage(Exception e) {
// StringWriter s = new StringWriter();
// e.printStackTrace(new PrintWriter(s));
// debugMessage(s.toString());
// }
//
// // =========================================================================
// /**
// * Prints an error message.
// *
// * @param obj Description of Parameter
// */
// public static final void errorMessage(Frame parentFrame, Object obj) {
// errorMessage(parentFrame, obj.toString());
// }
//
// /**
// * Prints an error message.
// *
// * @param msg Description of Parameter
// */
// public static final void errorMessage(Frame parentFrame, String msg) {
// errorMessage(parentFrame, null, msg);
// }
//
// /**
// * Prints an error message.
// *
// * @param msg Description of Parameter
// */
// public static final void errorMessage(Frame parentFrame, String dialogTitle, String msg) {
// if (displayErrorMsgs) {
// try {
// JOptionPane.showMessageDialog(parentFrame, msg, (dialogTitle==null) ? "ERROR" : dialogTitle, JOptionPane.ERROR_MESSAGE);
// }
// catch (Exception e) {
// // do nothing
// }
// }
// debugMessage(msg);
// }
//
// /**
// * Prints an error message.
// *
// * @param e Description of Parameter
// */
// public static final void errorMessage(Exception e) {
// errorMessage(null, "Exception Thrown:\n" + e.toString());
// }
//
// /**
// * Prints an error message.
// *
// * @param e Description of Parameter
// */
// public static final void errorMessage(Frame parentFrame, Exception e) {
// errorMessage(parentFrame, "Exception Thrown:\n" + e.toString());
// }
//
// /**
// * Prints an error message.
// *
// * @param msg Textual message of the error encountered.
// * @param e Actual error exception.
// */
// public static final void errorMessage(Frame parentFrame, String msg, Exception e) {
// errorMessage(parentFrame, msg + "\n\n" + "Exception Thrown:\n" + e.toString());
// }
//
// /**
// * Prints an error message.
// *
// * @param msg Textual message of the error encountered.
// * @param e Actual error exception.
// */
// public static final void errorMessage(String msg, Exception e) {
// errorMessage(null, msg, "Exception Thrown:\n" + e.toString());
// }
//
// // =========================================================================
// /**
// * Prints an information message.
// *
// * @param msg Description of Parameter
// */
// public static final void informationMessage(String msg) {
// if (displayToConsole) {
// System.out.println(msg);
// }
// logMessage(msg);
// }
//
// // =========================================================================
// /**
// * Prints an error message and exits system with error flag.
// *
// *@param msg Description of Parameter
// *@param e Description of Parameter
// */
// public static final void fatalError(Frame parentFrame, String msg, Exception e) {
// errorMessage(parentFrame, msg, e);
// System.exit(-1);
// }
//
// // =========================================================================
// /**
// * Writes out a message to the log file.
// *
// * @param msg Description of Parameter
// */
// public static final void logMessage(String msg) {
// if (m_logMessages) {
// m_loggedMessages.addElement(msg);
// if (m_loggedMessages.size() == 1000) {
// flushMessages();
// }
// }
// }
//
// // =========================================================================
// /**
// * Writes out message cache to file.
// */
// public static final void flushMessages() {
// try {
// RandomAccessFile logFile = new RandomAccessFile(m_logfilePath + m_logfileName, "rw");
// logFile.seek(logFile.length());
// for (int i = 0; i < m_loggedMessages.size(); ++i) {
// logFile.writeBytes((String)m_loggedMessages.elementAt(i));
// logFile.writeBytes(System.getProperty("line.separator"));
// }
// logFile.close();
// m_loggedMessages.removeAllElements();
// }
// catch (Exception e) {
// }
// }
// =========================================================================
/** Print contents of an array.
* @param out PrintStream to print to.
* @param array Array to dump.
* @param prefix Prefix for each line of output (ie. tab or spaces).
*/
// public static final void printArray(PrintStream out, Object[] array, String prefix) {
// if (array == null) {
// out.println(prefix + "null");
// return;
// }
//
// out.println(prefix + array.getClass().getComponentType().getName() + "[" + array.length + "]:");
// prefix += " ";
//
// for (int i=0; i<array.length; i++) {
// if (array[i].getClass().isArray()) {
// out.println(prefix + i + " = Array:");
// printArray(out, (Object[])array[i], prefix);
// }
// else {
// out.println(prefix + i + " = " + array[i]);
// }
// }
// }
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -