逻辑中的 else 部分不起作用并且不更新文件内容
The else part in the logic is not working and not updating file content
要求是:
发票编号将从 XSLT 发送。我必须阅读发票编号并更新计数。
示例:Invoice_no=101,如果 101 不存在,首先应检查文件将计数器初始化为 1,否则将计数器递增为 1。逻辑如下
public static void LookupInsert( int invoiceNumber)
{
Properties properties = new Properties();
File propertiesfile = new File("Sequence.properties");
if(propertiesfile.length()==0)
{
try{
propertiesfile.createNewFile();
properties.load(new FileInputStream(propertiesfile));
} catch (IOException e) {
e.printStackTrace();
}
int value=1;
properties.setProperty(new Integer(invoiceNumber).toString(), new Integer(1).toString());
try {
properties.store(new FileOutputStream(propertiesfile), null);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
Properties props = System.getProperties();
//if there is no invoice number in properties file then get(invoiceNumber) will be null
if(props.get(invoiceNumber)== null)
{
props.setProperty(new Integer(invoiceNumber).toString(), new Integer(1).toString());
try {
props.store(new FileOutputStream(propertiesfile), null);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
else
{
System.out.println("inside else");
int value = Integer.parseInt(props.get(invoiceNumber).toString());
value++;
props.setProperty(new Integer(invoiceNumber).toString(), new Integer(value).toString());
try {
props.store(new FileOutputStream(propertiesfile), null);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
// main() is used for testing
public static void main(String[] a) {
LookupInsert(101);
LookupInsert(102);
LookupInsert(103);
LookupInsert(104);
LookupInsert(105);
LookupInsert(106);
LookupInsert(107);
LookupInsert(108);
LookupInsert(101);
}
这里else部分不工作。它没有增加价值。请帮助我解决这个问题。
提前致谢
错误是您正在阅读 system.properties 和 propertyFile.get
而不是 propertyFile.getProperty
。查看固定码。
public static void lookupInsert(int invoiceNumber) throws IOException {
Properties props = new Properties();
File propertiesfile = new File("d:/rw/dump/Sequence.properties");
if (propertiesfile.length() == 0) {
try {
propertiesfile.createNewFile();
props.load(new FileInputStream(propertiesfile));
} catch (IOException e) {
e.printStackTrace();
}
int value = 1;
props.setProperty(new Integer(invoiceNumber).toString(), new Integer(1).toString());
try {
props.store(new FileOutputStream(propertiesfile), null);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return ;
}
props.load(new FileInputStream(propertiesfile));
System.out.println("props " + props.toString());
//if there is no invoice number in properties file then get(invoiceNumber) will be null
if (props.getProperty(String.valueOf(invoiceNumber)) == null) {
props.setProperty(new Integer(invoiceNumber).toString(), new Integer(1).toString());
try {
props.store(new FileOutputStream(propertiesfile), null);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} else {
System.out.println("inside else");
int value = Integer.parseInt(props.getProperty(String.valueOf(invoiceNumber)).toString());
value++;
props.setProperty(new Integer(invoiceNumber).toString(), new Integer(value).toString());
try {
props.store(new FileOutputStream(propertiesfile), null);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
PFB 完整工作代码:
public static void LookupInsert(int invoiceNumber) {
Properties props = new Properties();
File propertiesfile = new File("Sequence.properties");
if (propertiesfile.length() == 0) {
try {
propertiesfile.createNewFile();
props.load(new FileInputStream(propertiesfile));
} catch (IOException e) {
e.printStackTrace();
}
int value = 1;
props.setProperty(new Integer(invoiceNumber).toString(),
new Integer(1).toString());
try {
props.store(new FileOutputStream(propertiesfile), null);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
try {
props.load(new FileInputStream(propertiesfile));
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
// if there is no invoice number in properties file then
// get(invoiceNumber) will be null
if (props.get(invoiceNumber + "") == null) {
props.setProperty(new Integer(invoiceNumber).toString(),
new Integer(1).toString());
try {
props.store(new FileOutputStream(propertiesfile), null);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} else {
System.out.println("inside else");
int value = Integer.parseInt(props.get(invoiceNumber + "")
.toString());
value++;
props.setProperty(new Integer(invoiceNumber).toString(),
new Integer(value).toString());
try {
props.store(new FileOutputStream(propertiesfile), null);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
要求是:
发票编号将从 XSLT 发送。我必须阅读发票编号并更新计数。
示例:Invoice_no=101,如果 101 不存在,首先应检查文件将计数器初始化为 1,否则将计数器递增为 1。逻辑如下
public static void LookupInsert( int invoiceNumber)
{
Properties properties = new Properties();
File propertiesfile = new File("Sequence.properties");
if(propertiesfile.length()==0)
{
try{
propertiesfile.createNewFile();
properties.load(new FileInputStream(propertiesfile));
} catch (IOException e) {
e.printStackTrace();
}
int value=1;
properties.setProperty(new Integer(invoiceNumber).toString(), new Integer(1).toString());
try {
properties.store(new FileOutputStream(propertiesfile), null);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
Properties props = System.getProperties();
//if there is no invoice number in properties file then get(invoiceNumber) will be null
if(props.get(invoiceNumber)== null)
{
props.setProperty(new Integer(invoiceNumber).toString(), new Integer(1).toString());
try {
props.store(new FileOutputStream(propertiesfile), null);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
else
{
System.out.println("inside else");
int value = Integer.parseInt(props.get(invoiceNumber).toString());
value++;
props.setProperty(new Integer(invoiceNumber).toString(), new Integer(value).toString());
try {
props.store(new FileOutputStream(propertiesfile), null);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
// main() is used for testing
public static void main(String[] a) {
LookupInsert(101);
LookupInsert(102);
LookupInsert(103);
LookupInsert(104);
LookupInsert(105);
LookupInsert(106);
LookupInsert(107);
LookupInsert(108);
LookupInsert(101);
}
这里else部分不工作。它没有增加价值。请帮助我解决这个问题。
提前致谢
错误是您正在阅读 system.properties 和 propertyFile.get
而不是 propertyFile.getProperty
。查看固定码。
public static void lookupInsert(int invoiceNumber) throws IOException {
Properties props = new Properties();
File propertiesfile = new File("d:/rw/dump/Sequence.properties");
if (propertiesfile.length() == 0) {
try {
propertiesfile.createNewFile();
props.load(new FileInputStream(propertiesfile));
} catch (IOException e) {
e.printStackTrace();
}
int value = 1;
props.setProperty(new Integer(invoiceNumber).toString(), new Integer(1).toString());
try {
props.store(new FileOutputStream(propertiesfile), null);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return ;
}
props.load(new FileInputStream(propertiesfile));
System.out.println("props " + props.toString());
//if there is no invoice number in properties file then get(invoiceNumber) will be null
if (props.getProperty(String.valueOf(invoiceNumber)) == null) {
props.setProperty(new Integer(invoiceNumber).toString(), new Integer(1).toString());
try {
props.store(new FileOutputStream(propertiesfile), null);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} else {
System.out.println("inside else");
int value = Integer.parseInt(props.getProperty(String.valueOf(invoiceNumber)).toString());
value++;
props.setProperty(new Integer(invoiceNumber).toString(), new Integer(value).toString());
try {
props.store(new FileOutputStream(propertiesfile), null);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
PFB 完整工作代码:
public static void LookupInsert(int invoiceNumber) {
Properties props = new Properties();
File propertiesfile = new File("Sequence.properties");
if (propertiesfile.length() == 0) {
try {
propertiesfile.createNewFile();
props.load(new FileInputStream(propertiesfile));
} catch (IOException e) {
e.printStackTrace();
}
int value = 1;
props.setProperty(new Integer(invoiceNumber).toString(),
new Integer(1).toString());
try {
props.store(new FileOutputStream(propertiesfile), null);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
try {
props.load(new FileInputStream(propertiesfile));
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
// if there is no invoice number in properties file then
// get(invoiceNumber) will be null
if (props.get(invoiceNumber + "") == null) {
props.setProperty(new Integer(invoiceNumber).toString(),
new Integer(1).toString());
try {
props.store(new FileOutputStream(propertiesfile), null);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} else {
System.out.println("inside else");
int value = Integer.parseInt(props.get(invoiceNumber + "")
.toString());
value++;
props.setProperty(new Integer(invoiceNumber).toString(),
new Integer(value).toString());
try {
props.store(new FileOutputStream(propertiesfile), null);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}