Java - 转换文件名 - 获取错误?

时间:2016-09-08 12:34:14

标签: java selenium testing selenium-webdriver runtime-error

(Hello world level automation tester)

我正在尝试下载文件并将其重命名,以便于查找。我收到错误....这是代码

    @Test
public void allDownload() throws Exception {
    PiDownloadScreen();
    WebElement oK = driver.findElement(By.xpath
            ("/html/body/div[4]/div[11]/div/button"));
    String timeStamp = new SimpleDateFormat("yyyyMMddHHmmss").format(Calendar.getInstance().getTime());
    oK.click();
    System.out.println(timeStamp);
    File fileName = new File("C:/Users/HIQMD017/Downloads/" + timeStamp + (".csv"));
    File newName = new File("C:/Users/HIQMD017/Downloads/allPI.csv");
    if (fileName.renameTo(newName)) {
        System.out.println("AllPI was renamed");
    }
    else
    {
        int timeStampInt = Integer.parseInt(timeStamp);
        int completeStamp = timeStampInt - 1;
        String completeString = Integer.toString(completeStamp);
        File fileNameNew = new File("C:/Users/HIQMD017/Downloads/" + completeString + (".csv"));
        fileNameNew.renameTo(newName);
        System.out.println("All pi was renamed after conversion");
    }
    System.out.println(completeString);
}

我不确定为什么我会收到这些错误以及他们的意思,我对调试器也不太好,有人可以提供一些见解,我已经查看了我的代码似乎还可以,我可以说我出错的地方。如果需要,我很乐意添加其他代码,当然我需要修改以保密。

     java.lang.NumberFormatException: For input string: "20160908132027"
at java.lang.NumberFormatException.forInputString(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at TestingPIs.piTest.allDownload(piTest.java:51)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)

亲切的问候,

中号

1 个答案:

答案 0 :(得分:1)

您使用的字符串(时间戳)超出整数范围。这就是它抛出NumberFormatException的原因。您可以替换以下代码:

 int timeStampInt = Integer.parseInt(timeStamp);
 int completeStamp = timeStampInt - 1;
 String completeString = Integer.toString(completeStamp);

通过:

long timeStampLong = Long.parseLong(timeStamp);
long completeStamp = timeStampLong - 1;
String completeString = Long.toString(completeStamp);

我认为这样可行。