0

I have a java program that's extracting a date from the contents of a file, and then renaming the file with that date string. What I need to account for is that the new file name may end up having the date at the beginning, in the middle, or at the end. What I was trying to do was something like this -

String outputFilePt1 = args[0];
String outputFilePt2 = args[1];
String outputFilePt3 = args[2];

String outputDate = dateFormat.format( date );
String newOutputFile = outputFilePt1 + outputFilePt2 + outputFilePt3;

I would then enter something like "MyFile_", "outputDate", ".csv" for args 0-2, respectively, hoping that the result would be MyFile_outputDatesValue.csv - because I may need it to be MyFile.csv_outputDatesValue

What would be the best way to accomplish taking 3 inputs - FilePart1, FilePart2, FilePart3 where one would end up with the value of "outputDate" in the above code block?

Thanks in advance, I hope what I'm asking makes sense!

EDIT - I do not know the value of "outputDate" at the time I input the arguments. This is calculated based on the input file and a config/properties file that determines the date format/location of the input file.

4

2 回答 2

0

基本上,我最终采用的方式只是为我想要日期的 FilePart 接受一个像“thedate”这样的静态字符串,然后用包含它的变量替换文件名字符串中的“thedate”,就像这样 -

String outputFilePt1 = args[0];
String outputFilePt2 = args[1];
String outputFilePt3 = args[2];

String outputDate = dateFormat.format( date );
newOutputFile = newOutputFile.replaceAll("thedate", outputDate);

感谢所有关注和思考这个问题的人!我的问题的真正答案是“你不能完全按照你的意愿去做”——基本上你必须转换输入,或者按照上面的方法替换它。在提出这个解决方案之前,我基本上已经完成了 -

String outputFilePt1 = args[0];
String outputFilePt2 = args[1];
String outputFilePt3 = args[2];
String lookingFor = "thedate";

if (outputFilePt1.equals(lookingFor))
{
    outputFilePt1 = outputDate;
}
if (outputFilePt2.equals(lookingfor))
{
    outputFilePt2 = outputDate;
}
if (outputFilePt2.equals(lookingfor))
{    
    outputFilePt3 = outputDate;
}

String outputDate = dateFormat.format( date );

显然,最好对我的“thedate”字符串进行替换!

再次感谢。

于 2013-10-29T22:58:44.837 回答
0

看看boolean String.matches(String regexp)方法。您可以像在这篇文章中那样解析它:Regex date format validation on Java,找出哪一行 arg 是日期,然后我认为构建一个合适的字符串会很容易。

于 2013-10-29T00:12:02.373 回答