-1

I'm developing in Windows and somewhere in the flow I have to access sftp (Linux) server. I do some logic to prepare filenames I have to copy from the sftp server and I need to generate the full path, therefore I write this line in my code:

Paths.get(configuration.getSftpServerConfiguration().getRemotePath(), filename).toString();

Because I’m running on Windows the paths generated with Windows slash, for example \public\directory\filename.csv

Can I define Paths to work with Linux separator? (I know I can concatenate ‘/’ by myself but looks for me like a bad practice..)

AsfK
  • 3,328
  • 4
  • 36
  • 73
  • 1
    `Paths.get()` creates a path relative to your default filesystem. A `Path` is a pointer to a local filesystem. If you do not mount the sftp server as a local filesystem, you shouldn't access it using `Path`. – RealSkeptic May 31 '20 at 09:24
  • @RealSkeptic Thank you, does there is another java package I can use or I have to do it by myself? – AsfK May 31 '20 at 09:26
  • 1
    I would check in the sftp library that you use whether it has a "separator" property or a path concatenation facility. – RealSkeptic May 31 '20 at 09:32
  • 1
    If you want to use `Path` then consider looking for a `FileSystemProvider` implementation for SFTP. Then you can interact with the remote server like it was just another file system. You'd have to use the implementation's `FileSystem` to create the paths, of course. Otherwise check your current library for the necessary functionality, as suggest by RealSkeptic. – Slaw May 31 '20 at 09:37

1 Answers1

2

Check this post: Is there a Java utility which will convert a String path to use the correct File separator char?

Basically FilenameUtils.separatorsToSystem(String path), of Apache Commons, will help achieve what you are trying to do.

If you do not want to import the entire dependency this is what the method does:

String separatorsToSystem(String res) {
    if (res==null) return null;
    if (File.separatorChar=='\\') {
        // From Windows to Linux/Mac
        return res.replace('/', File.separatorChar);
    } else {
        // From Linux/Mac to Windows
        return res.replace('\\', File.separatorChar);
    }
}

UPDATE: As @Slaw said, this solution is still Platform-dependent. You could modify the method to take an extra parameter, and decide if you want the output string in "Unix" or "Windows", something like this:

String changeFileSeparators(String res, boolean toUnix) {
    if (res==null) return null;
    if (toUnix) {
        // From Windows to Linux/Mac
        return res.replace('\\', '/');
    } else {
        // From Linux/Mac to Windows
        return res.replace('/', '\\');
    }
}
leobia
  • 58
  • 8
  • Thank you! so I understand that there is no direct solution.. btw, nice trick :) – AsfK May 31 '20 at 09:25
  • 1
    @AsfK This implementation is still platform-dependent, just like `Paths#get(String,String...)`. In other words, it uses the separator used by the host operating system. – Slaw May 31 '20 at 09:39
  • @Slaw, Thanks I notice it, but it still nice trick, in my case I just replace \\ with / .. Now I read your commnet, I'll look in the sftp impl. – AsfK May 31 '20 at 09:40