逻辑中的else部分不工作,也不更新文件内容



要求是:
发票号将从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部分不工作。它没有增加值。请帮助我解决这个问题。

Thanks in advance

错误是你正在阅读系统。性质和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();
        }
    }
}

相关内容

  • 没有找到相关文章

最新更新