Mailing Lists: Apple Mailing Lists

Image of Mac OS face in stamp
 
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: MacOS 10.4.11 Character problem



zhangzehao wrote:

File file = new File("/Users/imageworks/Desktop/表示履歴.jpg");
String[] command = {"ditto", "-ck", "--sequesterRsrc",filename, "/ Users/imageworks/Desktop/zhangh.zip"};

When a String contains literal non-ASCII characters in the Java source file, then the Java compiler (javac) will read those characters using its default text-encoding at compile-time. If javac's default encoding isn't the same as the encoding of the source file, then javac will compile a string literal that is not what you think it is.


You need to treat the Java source-file with the same attention to text-encodings as you treat text files that your program reads. You have to take text-encoding into account at the point in time where the file is used. In this case, the file is a Java source file, and its point of use is when it's compiled. If the jpg filename isn't being compiled into the Java program correctly, then there's no way that the rest of the code will work, because it's not getting the filename that you think it is.

The standardized way to encode all non-ASCII characters in Java String literals is to use Unicode escapes. In short, use \uxxxx, where xxxx is 4 hex characters of a UTF-16 Unicode code-point.

The above is true for all languages and writing systems. I've seen it happen with accented characters in the Latin alphabet, as well as for characters in the Russian and Greek alphabets.

To test what filename is actually being compiled, add code to print the pathname to System.out with every non-ASCII char as "\uxxxx" where xxxx is the 4-character hex form of the Java char. You really need to see exactly what the code is using for its filenames.


PrintStream ps= new PrintStream(new FileOutputStream("/Users/ imageworks/Desktop/zhang.sh"));
ps.write(sb.toString().getBytes("UTF8"));

This is a poor use of a PrintStream. You should probably be using an OutputStreamWriter that uses the UTF8 encoding, on a FileOutputStream that's writing to the file. Then you can just write the text Strings to it and have it take care of the encoding.


Or if you insist on doing the encoding with getBytes(), then write the bytes directly to the FileOutputStream, eliminating the PrintStream.

Or you can eliminate the temporary zhang.sh file by writing bytes to the OutputStream of the shell Process, which the shell will then interpret as commands. See Process.getOutputStream().

  -- GG _______________________________________________
Do not post admin requests to the list. They will be ignored.
Java-dev mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/java-dev/email@hidden

This email sent to email@hidden


Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2007 Apple Inc. All rights reserved.