【问题标题】:UDF for conditional generation of filename用于有条件地生成文件名的 UDF
【发布时间】:2020-06-01 00:06:38
【问题描述】:

我想根据条件动态传递文件名。我已经编写了以下代码,但文件名没有通过。我认为if条件可能存在一些问题。

DynamicConfiguration conf = (DynamicConfiguration) container.getTransformationParameters().get(StreamTransformationConstants.DYNAMIC_CONFIGURATION);
DynamicConfigurationKey key = DynamicConfigurationKey.create("http://sap.com/xi/XI/System","FileName");

//get current timestamp and reformat
Date date = new Date();
DateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");

//define filename
String filename = new String("");
if (orgid == "G"||"ZG"||"S")
{
filename = "N_" + df.format(date) + ".txt"  ;
}
if (orgid == "L")
{
filename = "D_" + df.format(date) + ".txt"  ;
}

if (orgid == "F"||"IV")
{
filename = "F_" + df.format(date) + ".txt"  ;
}


conf.put(key, filename);

return filename;

请告诉我哪里出错了。

【问题讨论】:

    标签: java sap integration sap-xi sap-pi


    【解决方案1】:

    几件事:

    不要使用== 来比较字符串是否相等。请改用equals() 方法。 == 检查两个对象是否指向相同的内存位置,而 String#equals() 方法评估对象中值的比较。

    不要以这种方式初始化您的字符串变量:String filename = new String("");。这是一个字符串构造函数调用。你应该做的是:String filename = "";

    您无法以这种方式检查特定变量是否包含多种可能性之一:

    if (orgid == "G" || "ZG" || "S") {
    

    如前所述,您需要使用 String#equals() 方法并与其他可能性进行比较,您需要将每个字符串与变量进行比较,例如:

    if (orgid.equals("G") || orgid.equals("ZG") || orgid.equals("S")) {
    

    最好使用 switch 语句代替这样的事情,例如:

    /* Using the String#toUpperCase() method ensures that 
       we don't need to worry about what letter case happens
       to be in orgid.    */
    switch (orgid.toUpperCase()) {
        case "G":
        case "ZG":
        case "S":
            filename = "N_" + df.format(date) + ".txt";
            break;
        case "L":
            filename = "D_" + df.format(date) + ".txt";
            break;
        case "F":
        case "IV":
            filename = "F_" + df.format(date) + ".txt";
            break;
        default:
            System.err.println("Can Not Establish A Proper File Name (missing prefix)!");
            return;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-02-14
      • 2022-11-19
      • 1970-01-01
      • 2023-04-09
      • 2018-05-05
      • 1970-01-01
      相关资源
      最近更新 更多