📄 config.java
字号:
/*
* @(#)Config.java ver 1.2 6/20/2005
*
* Copyright 2005 Weishuai Yang (wyang@cs.binghamton.edu).
* All rights reserved.
*
*/
package gps.util;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
/**
* a global object to deal with configuraiton files
*
*
* @author Weishuai Yang
* @version 1.2, 6/20/2005
*/
public class Config {
/**
* loads configuration from file at the specified path
* @param path file path
* @param properties destination properties
*/
public static void loadConfFromFile(String path, Properties properties){
if((path!=null)&&(path.length()!=0))
loadConfFromFile(new File(path), properties);
}
/**
* loads configuration from File object
* @param f File object
* @param properties destination properties
*/
public static void loadConfFromFile(File f, Properties properties){
try
{
FileInputStream input = new FileInputStream(f);
properties.load(input);
input.close();
}
catch (FileNotFoundException e)
{
System.err.println(f + " not found");
}
catch (IOException e){
System.err.println(f + " io exception");
}
catch (Exception e){
System.err.println("reading config "+f + " exception");
e.printStackTrace();
}
}
/**
* saves configuration to file at the specified path
* @param path file path
* @param properties properties to be saved
*/
public static void saveConfToFile(String path, Properties properties){
if((path!=null)&&(path.length()!=0))
saveConfToFile(new File(path), properties);
}
/**
* saves configuration to File object
* @param f File object
* @param properties properties to be saved
*/
public static void saveConfToFile(File f, Properties properties){
try
{
FileOutputStream output = new FileOutputStream(f);
properties.store(output,"#Generated Configuration, be careful about the relative path of configuration file and other files\n");
output.close();
}
catch (FileNotFoundException e)
{
System.err.println(f + " not found");
}
catch (IOException e){
System.err.println(f + " io exception");
}
catch (Exception e){
System.err.println("reading config "+f + " exception");
e.printStackTrace();
}
}
/**
* loads configuration from file at the specified path, with messagebox if any error exists.
* @param path file path
* @param properties destination properties
* @param frame parent frame
*/
public static void loadConfFromFileGUI(String path, Properties properties, JFrame frame){
if((path!=null)&&(path.length()!=0))
loadConfFromFileGUI(new File(path), properties, frame);
}
/**
* loads configuration from File object, with messagebox if any error exists.
* @param f File object
* @param properties destination properties
* @param frame parent frame
*/
public static void loadConfFromFileGUI(File f, Properties properties, JFrame frame){
try
{
FileInputStream input = new FileInputStream(f);
properties.load(input);
input.close();
}
catch (FileNotFoundException e)
{
JOptionPane.showMessageDialog(frame,""+ f + " not found", "Error!", JOptionPane.ERROR_MESSAGE);
return;
}
catch (IOException e){
JOptionPane.showMessageDialog(frame,""+ f + " io exception", "Error!", JOptionPane.ERROR_MESSAGE);
return;
}
catch (Exception e){
JOptionPane.showMessageDialog(frame,"reading config "+ f + "error", "Error!", JOptionPane.ERROR_MESSAGE);
e.printStackTrace();
return;
}
}
/**
* saves configuration to file at the specified path, with messagebox if any error exists.
* @param path file path
* @param properties properties to be saved
* @param frame parent frame
*/
public static void saveConfToFileGUI(String path, Properties properties, JFrame frame){
if((path!=null)&&(path.length()!=0))
saveConfToFileGUI(new File(path), properties, frame);
}
/**
* saves configuration to File object, with messagebox if any error exists.
* @param f File object
* @param properties properties to be saved
* @param frame parent frame
*/
public static void saveConfToFileGUI(File f, Properties properties, JFrame frame){
try
{
FileOutputStream output = new FileOutputStream(f);
properties.store(output,"#Generated Configuration, be careful about the relative path of configuration file and other files\n");
output.close();
}
catch (FileNotFoundException e)
{
JOptionPane.showMessageDialog(frame,""+ f + " not found", "Error!", JOptionPane.ERROR_MESSAGE);
return;
}
catch (IOException e){
JOptionPane.showMessageDialog(frame,""+ f + " io exception", "Error!", JOptionPane.ERROR_MESSAGE);
return;
}
catch (Exception e){
JOptionPane.showMessageDialog(frame,"reading config "+ f + "error", "Error!", JOptionPane.ERROR_MESSAGE);
e.printStackTrace();
return;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -