Home>

The task is to replace fileSeparator with Formatter, but I don't fully understand how to use it here. The code itself creates a new file at the relative path

public static String сreateFile (String fileName) throws IOException {
        String fileSeparator= System.getProperty ("file.separator");
        String relativePath= "Files" + fileSeparator + fileName;
        File file= new File (relativePath);
        if (file.createNewFile ()) {
            return relativePath;
        } else {
            return relativePath;
       }
    }

It is also not clear what the Formatter has to do with it. Can you clarify the requirements? It is still unclear why you need an if in which both branches are the same.

default locale2021-02-23 11:41:45
  • Answer # 1

    Of course, I ask you not to hate. But I think that's what you wanted.

    public static String сreateFile (String fileName) throws IOException {
        Formatter formatter= new Formatter (Locale.US);
        String fileSeparator= System.getProperty ("file.separator");
        formatter.format ("Files% 1 $ s% 2 $ s", fileSeparator, fileName);
        String relativePath= formatter.toString ();
        File file= new File (relativePath);
        file.createNewFile ();
        return relativePath;
    }
    
Trends