2 sinat 33415638 sinat_33415638 于 2016.02.26 09:34 提问

程序里面有静态变量,我想执行好几遍,每次的变量值都从外面获取,我的静态变量应该怎么改

我发下主要代码吧,涉及到静态变量的。主要是想把文件路径filename和获取的source的名字改成非静态的
public class ProcessRow {
static Properties source;
static {
try {
source = new Properties();
FileInputStream fis = new FileInputStream(new File(
inputPropertieName()));
InputStreamReader isr = new InputStreamReader(fis, "UTF-8");
source.load(isr);

    fis.close();
} catch (Exception e) {
    // throw new ExceptionInInitializerError(e);
}

}

public static String filterAlphabet(String alph) {
alph = alph.replaceAll("[^(A-Z)]", "");
return alph;
}

public String Pname() {
String hai = UseProcess.fileName;
String a = filterAlphabet(hai);
String b = hai.substring(hai.indexOf(a), hai.indexOf(a) + 6);
return b;

}

public static String inputPropertieName() {
/*
* System.out.println("请输入配置文件名称:"); Scanner a = new Scanner(System.in);
* String propertieName = a.next();
*/
// a.close();
ProcessRow a = new ProcessRow();
String propertieName = a.Pname();
return propertieName;
}

// 判断方法1,2

public boolean panduan1(XSSFRow from) {
return from.getCell(8).toString().trim()
.equals(source.getProperty("I列值1"))
&& (from.getCell(2).toString().trim().equals(source
.getProperty("C列值1")));// 1 I列值为“I列值1” 且C列值为“C列值1”
}

public boolean panduan2(XSSFRow from) {
return from.getCell(8).toString().trim()
.equals(source.getProperty("I列值2"))
&& (from.getCell(2).toString().trim().equals(source
.getProperty("C列值2")));// 2 I列值为“I列值2” 且C列值为“C列值2”
}
.........

Output类
public class Output {
static Date d = new Date();
static DateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");
final static String fileName = df.format(d);
static String b = null;
static String c = ProcessRow.source.getProperty("输出路径")+fileName + "_main" + ".xlsx";
public static FileOutputStream init() {
FileOutputStream s = null;
try {
s = new FileOutputStream(c);
} catch (FileNotFoundException e) {
}
return s;
}
public static XSSFWorkbook getWorkbook() throws IOException{
FileInputStream fis = new FileInputStream(c);
XSSFWorkbook wb = new XSSFWorkbook( fis );
return wb;
}

}
UseProcess类
public class UseProcess {
static String fileName=UseProcess.fileName();
static XSSFSheet readsheet;
static {
try {
readsheet = readWorkbook();
} catch (Exception e) {
// TODO: handle exception
}
}
static XSSFWorkbook sc = null;
static XSSFWorkbook sr = null;
public static String fileName() {
System.out.println("请输入文件路径(将文件拖入程序):");
Scanner a = new Scanner(System.in);
String fileName = a.next();
//a.close();
return fileName;
}
ArrayList date1 = new ArrayList();
ArrayList value1 = new ArrayList();
ArrayList date2 = new ArrayList();
ArrayList value2 = new ArrayList();

int lastnum = getNum(fileName);
int i = Integer.parseInt(ProcessRow.source.getProperty("表单数据从第几行开始")) - 1;
static int j = 1;

public static int getNum(String Filename) {
try {
InputStream myxls;
myxls = new FileInputStream(Filename);
sr = new XSSFWorkbook(myxls);
XSSFSheet sheet = sr.getSheetAt(0);// 第一个工作表
return sheet.getLastRowNum();// 获取第一个工作表的行数
} catch (IOException e) {
return 0;
}
}
主函数
public class TestMain {
public static void test() throws IOException, NumberFormatException, ParseException{
UseProcess.createWorkbook();
UseProcess n = new UseProcess();
for (int i = 1; i <= Integer.parseInt(ProcessRow.source
.getProperty("condition-process的个数")); i++) {
n.UseProcess1(String.valueOf(i));
}
n.UseProcessNull();
System.out.println("输入完毕");
}
public static void main(String[] args) throws NumberFormatException, IOException, ParseException {
TestMain.test();
}

1个回答

sinat_31535993
sinat_31535993   Rxr 2016.02.26 09:44

改就改呗,最多就是每次调用的时候在内存中重新生成一下。

sinat_33415638
sinat_33415638 不知道怎么改,我写成非静态,用new一个对象调用,就会报内存溢出的错
接近 2 年之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!