1

I have this java code that renames a file (or directory). There is a problem on Linux when I use some special characters, it works on Windows with these special characters. The way I try it is like this: In windows

  • I create a direcotory called "326½_6"

  • I build a jar-file, and call it (java -jar) directly in the windows cmd (or linux shell), first param is the directory above, second param is a path to a new directory. This works

I then transfer the directory to a Linux server using SFTP (WinSCP). I repeat the steps above, but it doesn't work. I get this output:

Moving /home/user/testarea/326�_6/ to /home/user/testarea/test5/

--- could not perform rename -------

Is there anyway to make this work on a Linux machine???

the code:

  public static void main(String [] args) {
    String source = args[0];
    String dest = args[1];
    System.out.println(" - Moving " + source + " to " + dest);

    File sourceFile = new File(source);
    File destinationFile = new File(dest);

    if (!sourceFile.renameTo(destinationFile)) {
        System.out.println("--- could not perform rename -------");
    }

    System.out.println("Finished moving");
 }

thanks!

Jojje
  • 1,749
  • 3
  • 25
  • 44

3 Answers3

2

It looks like Java on Linux is expecting file and directory names to be encoded in UTF-8, but when WinSCP creates the directory it encodes the name in latin1 or something similar, and the new name is not valid in UTF-8. Apparently this was the default behavior for WinSCP, the newer versions use UTF-8 by default.

An easy solution to make Java use the same encoding that SFTP used when creating the directory. This is done by changing the locale when running the JVM:

LANG=en_US.iso8859_1 java -jar YourProgram.jar

The locale en_US.iso8859_1 has to exist for this to work, though. You might be able to install new locales from the package repositories of your distribution. If not, you can read about how to define a locale with a specific encoding (and about this file name problem in general) in my blog.

Joni
  • 108,737
  • 14
  • 143
  • 193
  • IS it always encoded as "en_US.iso8859_1" when transferring via SFTP? – Jojje Oct 24 '12 at 15:18
  • I think SFTP does not change the encoding when transferring, so file names are created with the encoding used on Windows, which is probably windows-1252. It's the same as iso-8859-1 except for the bytes 0x80-0x9f. – Joni Oct 24 '12 at 15:25
  • Is there a way to handle "all" platforms mac/win/linux etc? Or should one make sure that users using SFTP must use UTF-8 encoding? – Jojje Oct 24 '12 at 15:48
  • If you use iso-8859-1 for file names it will "work" for every one, but file names will be mangled for a lot of people. Better to make sure everyone uses UTF-8. – Joni Oct 25 '12 at 14:15
1

May be the file source is not exists. check existance before renameTo

BlackJoker
  • 3,099
  • 2
  • 20
  • 27
0

renameTo() is not reliable at all, above all in windows, but also in unix-like system. What I suggest is perform some checks, create some "retry" mechanism and take a look at this post. Can you specify what encoding are you using (by looking at the tag I think UTF-8) and how do you launch your program?

Community
  • 1
  • 1
sataniccrow
  • 372
  • 2
  • 7