📄 pubfunc.java
字号:
/**
* PubFunc.java 07/01/2002,
* Author:
*
* Copyright (c) 2002 Censoft Corp.
* Beijing China
* All rights reserved.
*
* Modifier:
* Time:
***/
package com.gs.util;
import java.io.*;
import java.sql.*;
import java.util.*;
import javax.sql.*;
import com.gs.util.*;
public class PubFunc{
static private PubFunc instance;
static private String configfile = " Config";
static private String endline = "------分隔符------";
private Properties myProps; //配置文件读取
//建构造函数防止调用
private PubFunc(){
}
//创建实例
public static synchronized PubFunc getInstance(){
if(instance == null){
instance = new PubFunc();
}
return instance;
}
//分隔字符串 参数说明:source为要分隔的串,sign为分隔符
public Enumeration split(String source,String sign){
Vector ar = new Vector();
int start = 0;
String st = source;
while(true){
int ipos = st.indexOf(sign);
if(ipos < 0){
ar.addElement(st.substring(0));
break;
} else{
ar.addElement(st.substring(0,ipos));
st = st.substring(ipos + 1);
}
}
return ar.elements();
}
//是否包括索引
public boolean isChecked(String all,int index){
String s = String.valueOf(index);
String s1 = "{" + s + " ";
String s2 = "{" + s + "}";
String s3 = " " + s + " ";
String s4 = " " + s + "}";
if((all.indexOf(s1) >= 0) || (all.indexOf(s2) >= 0) ||
(all.indexOf(s3) >= 0) || (all.indexOf(s4) >= 0)){
return true;
} else{
return false;
}
}
public static boolean IsNumeric(String s){
if(Integer.parseInt(s) > 0){
return true;
} else{
return false;
}
}
//删除配制项匹配的记录
public static boolean delConfItem(String conffile,String item,
String value){
File f = new File(conffile);
boolean found = false;
try{
if(!f.exists()){
f.createNewFile();
}
deleteFile(conffile + ".bak");
deleteFile(conffile + ".tmp");
FileWriter fw = new FileWriter(conffile + ".tmp");
FileReader cf = new FileReader(conffile);
BufferedReader is = new BufferedReader(cf);
String str = is.readLine();
while(str != null){
if(str.equals(decodeGB(item + "=" + value))){
found = true;
str = is.readLine();
while((str != null) && (!str.equals(endline))){
str = is.readLine();
}
} else{
fw.write(str + "\r\n");
}
if(str != null){
str = is.readLine();
}
}
is.close();
cf.close();
fw.close();
moveFile(conffile,conffile + ".bak");
moveFile(conffile + ".tmp",conffile);
} catch(Exception e){
System.err.println("不能读或写属性文件: " + conffile + " \n");
return false;
}
return found;
}
//修改配制项匹配的记录
public static boolean updateConfItem(String conffile,String condition,
String item,String value){
if((condition == null) || (condition.equals(""))){
return writeConf(conffile,item,value);
}
value = decodeGB(replace(value,"\r\n","</p><p>"));
File f = new File(conffile);
boolean found = false;
try{
if(!f.exists()){
f.createNewFile();
}
deleteFile(conffile + ".bak");
deleteFile(conffile + ".tmp");
FileWriter fw = new FileWriter(conffile + ".tmp");
FileReader cf = new FileReader(conffile);
BufferedReader is = new BufferedReader(cf);
String str = is.readLine();
while(str != null){
if(str.equals(decodeGB(condition))){
while((str != null) && (!str.equals(endline))){
if(str.startsWith(item + "=",0)){
str = item + "=" + value;
found = true;
}
fw.write(str + "\r\n");
str = is.readLine();
}
if(!found){
fw.write(item + "=" + value + "\r\n");
}
found = true;
if(str != null){
fw.write(str + "\r\n");
}
} else
if(str != null){
fw.write(str + "\r\n");
}
if(str != null){
str = is.readLine();
}
}
is.close();
cf.close();
fw.close();
moveFile(conffile,conffile + ".bak");
moveFile(conffile + ".tmp",conffile);
} catch(Exception e){
System.err.println("不能读或写属性文件: " + conffile + " \n");
return false;
}
return found;
}
//增加配制项
public static boolean addConfItem(String conffile,String item,
String value){
value = decodeGB(replace(value,"\r\n","</p><p>"));
try{
FileWriter fw = new FileWriter(conffile,true);
fw.write(item + "=" + value + "\r\n");
fw.close();
return true;
} catch(Exception e){
return false;
}
}
//增加分隔行
public static boolean addEndLine(String conffile){
try{
FileWriter fw = new FileWriter(conffile,true);
fw.write(endline + "\r\n");
fw.close();
return true;
} catch(Exception e){
return false;
}
}
//读配制文件中项的列表
public static Enumeration getConfList(String conffile,String item){
Vector ar = new Vector();
try{
FileReader cf = new FileReader(conffile);
BufferedReader is = new BufferedReader(cf);
String str = is.readLine();
while(str != null){
if(str.startsWith(item + "=",0)){
str = str.substring(new String(item + "=").length());
ar.addElement(encodeGB(str));
str = is.readLine();
} else{
str = is.readLine();
}
}
is.close();
cf.close();
} catch(Exception ex){
Debug.println("读文件" + conffile + "出错!" + ex.getMessage());
}
return ar.elements();
}
//修改配制项匹配的记录
public String readConfItem(String conffile,String condition,String item){
if((condition == null) || (condition.equals(""))){
return readConf(conffile,item);
}
String value = "";
File f = new File(conffile);
boolean found = false;
try{
if(!f.exists()){
return "";
}
FileReader cf = new FileReader(conffile);
BufferedReader is = new BufferedReader(cf);
String str = is.readLine();
while(str != null){
if(str.equals(decodeGB(condition))){
while((str != null) && (!str.equals(endline))){
if(str.startsWith(item + "=",0)){
found = true;
value = str.substring(new String(item + "=").length());
str = null; //找到,结束
} else{
str = is.readLine();
}
}
str = null; //找到,结束
}
if(str != null){
str = is.readLine();
}
}
is.close();
cf.close();
} catch(Exception e){
System.err.println("不能读属性文件: " + conffile + " \n" + e.getMessage());
return "";
}
return encodeGB(replace(value,"</p><p>","\r\n"));
}
//读配制文件的项
public String readConf(String conffile,String item){
String ret = "";
myProps = new Properties();
try{
InputStream is = new FileInputStream(conffile);
myProps.load(is);
ret = myProps.getProperty(item,"");
is.close();
} catch(Exception e){
System.err.println("不能读取属性文件: " + conffile + " \n");
return "";
}
return replace(ret,"</p><p>","\r\n");
}
//写配制文件的项
public static boolean writeConf(String conffile,String item,String value){
value = decodeGB(replace(value,"\r\n","</p><p>"));
File f = new File(conffile);
boolean found = false;
try{
if(!f.exists()){
f.createNewFile();
}
deleteFile(conffile + ".bak");
deleteFile(conffile + ".tmp");
FileWriter fw = new FileWriter(conffile + ".tmp");
FileReader cf = new FileReader(conffile);
BufferedReader is = new BufferedReader(cf);
String str = is.readLine();
while(str != null){
if(str.startsWith(item + "=",0)){
str = item + "=" + value;
found = true;
}
fw.write(str + "\r\n");
str = is.readLine();
}
if(!found){
str = item + "=" + value;
fw.write(str + "\r\n");
}
is.close();
cf.close();
fw.close();
Debug.println(item + "=" + value);
Debug.println("rename file " + conffile + " to " + conffile +
".bak");
System.out.println(moveFile(conffile,conffile + ".bak"));
Debug.println("rename file " + conffile + ".tmp to " +
conffile);
System.out.println(moveFile(conffile + ".tmp",conffile));
} catch(Exception e){
System.err.println("不能读或写属性文件: " + conffile + " \n");
return false;
}
return true;
}
//读config配制文件
public String readDomainCfg(String confpath,String item){
String conffile = confpath + "/" + configfile;
return readConf(conffile,item);
}
//读config配制文件
public String readDomainCfg(String confpath,String item,
String defaultvalue){
String ret = readDomainCfg(confpath,item);
if((ret == null) || (ret.trim().length() == 0)){
ret = defaultvalue;
}
return ret;
}
//写config配置文件
public static boolean writeDomainCfg(String confpath,String item,
String value){
String conffile = confpath + "/" + configfile;
return writeConf(conffile,item,value);
}
//读config配制文件
public String readTemplateCfg(String confpath,String item){
String conffile = confpath + "/" + configfile;
return readConf(conffile,item);
}
//读config配制文件
public String readTemplateCfg(String confpath,String item,
String defaultvalue){
String ret = readTemplateCfg(confpath,item);
if((ret == null) || (ret.trim().length() == 0)){
ret = defaultvalue;
}
return ret;
}
//写config配置文件
public static boolean writeTemplateCfg(String confpath,String item,
String value){
String conffile = confpath + "/" + configfile;
return writeConf(conffile,item,value);
}
//文件是否存在
public static boolean fileExists(String filename){
File f = new File(filename);
return f.exists();
}
public static boolean folderExists(String path){
File f = new File(path);
return f.isDirectory();
}
//删除指定的文件
public static boolean deleteFile(String filename){
File f = new File(filename);
return f.delete();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -