Total Pageviews

2020/05/07

[Java] [SonarLint] Remove this hard-coded path-delimiter

Problem
After I run SonarLint check, it complain the following code has problem (Remove this hard-coded path-delimiter):

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
    try (Writer file
            = new java.io.FileWriter(new File(testDir.toAbsolutePath() + "\\" + ftlFileName + ".java"));) {
        Template template = cfg.getTemplate("testcase.ftl");
    
        Map<String, Object> data = new HashMap<>();
        data.put("ftlFileName", ftlFileName);
    
        template.process(data, file);
    } catch (IOException | TemplateException e) {
        throw new FtlFileException("fail to generate file from ftl file : " + e.getMessage(), e);
    }


How-To
Using File.separator instead of hard code

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
    try (Writer file
            = new java.io.FileWriter(new File(testDir.toAbsolutePath() + File.separator + ftlFileName + ".java"));) {
        Template template = cfg.getTemplate("testcase.ftl");
    
        Map<String, Object> data = new HashMap<>();
        data.put("ftlFileName", ftlFileName);
    
        template.process(data, file);
    } catch (IOException | TemplateException e) {
        throw new FtlFileException("fail to generate file from ftl file : " + e.getMessage(), e);
    }


No comments: