📄 fileutil.java
字号:
package dms.util;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.Properties;
public class FileUtil {
private static Properties props = new Properties();
// 使用类装载器以跨平台的方式从类路径中查找配置文件
static {
InputStream input = null;
String platform = System.getenv("DBUS_SESSION_BUS_ADDRESS");
if(platform.contains("unix")){
input = FileUtil.class.getClassLoader()
.getResourceAsStream("dms/config/config_unix.properties");
}else{
input = FileUtil.class.getClassLoader()
.getResourceAsStream("dms/config/config_windows.properties");
}
try {
props.load(input);
input.close();
} catch (IOException e) {
e.printStackTrace();
throw new ExceptionInInitializerError();
}
}
/**
* 读源文件
*
* @return
*/
public static String getSourceFile() {
return props.getProperty("sourceFile");
}
/**
* 读上次采集结束位置
*
* @return
*/
public static String getHistoryLocationFile() {
return props.getProperty("historyLocationFile");
}
/**
* 读服务器IP
*
* @return
*/
public static String getLabIP() {
return props.getProperty("labIP");
}
/**
* 保存匹配未成功的登录记录对象
*
* @return
*/
public static String getHistoryFile() {
return props.getProperty("historyFile");
}
/**
* 采集服务器端IP
*/
public static String getServerIP() {
return props.getProperty("serverIp");
}
/**
* 采集服务器端Port
*/
public static String getServerPort() {
return props.getProperty("serverPort");
}
/**
* 发送失败时,保存记录的文件
*/
public static String getStoreFile() {
return props.getProperty("storeFile");
}
/**
* 钝化操作
*
* @param fileName 钝化到哪个文件
* @param obj 将要钝化的内容
* @throws IOException
*/
public static void passivate(String fileName, Object obj)
throws IOException {
FileOutputStream fos = new FileOutputStream(fileName, false);
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(obj);
oos.close();
}
/**
* 锐化操作
*
* @param fileName 从哪个文件锐化
* @return 锐化得到的内容
* @throws IOException
* @throws ClassNotFoundException
*/
public static Object active(String fileName) throws IOException,
ClassNotFoundException {
Object obj = null;
File file = new File(fileName);
if (file.exists() && file.length() > 0) {
FileInputStream fis = new FileInputStream(file);
ObjectInputStream ois = new ObjectInputStream(fis);
obj = ois.readObject();
ois.close();
}
return obj;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -