I am using JRuby 1.7.2 and Java 1.7.0. I have a directory with a file
whose name includes multibyte characters. I can’t read that file,
either in JRuby or Java. I’m writing here because I’m hoping someone
might know about this issue, or might be able to tell me I’m using the
wrong Java API for this – something like that.
Note this is not a problem with MRI Ruby 1.9.3. All files read fine in
that environment.
The errors are very similar. Source code for the Ruby program and the
Java program are at the bottom of this message. In both cases, I get a
“No such file or directory” error. In Java, it looks like this:
java.io.FileNotFoundException: heII???A???heII???A???.txt (No such
file or directory)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.(FileInputStream.java:138)
at java.io.FileInputStream.(FileInputStream.java:97)
at java.io.FileReader.(FileReader.java:58)
at ListFiles.main(ListFiles.java:24)
Any idea what might be going on? Is it possible to read files with
multibyte characters in their names using Java 1.7? Are there any
workarounds or other solutions? I would appreciate any help anyone can
offer, because right now I feel completely stuck on this one.
Regards,
steven
JAVA ENVIRONMENT:
[ 01:38 PM (119) asymptotic:stevenarnold ~/Downloads/pyramid/sample ] >
java -version
java version “1.7.0_17”
Java™ SE Runtime Environment (build 1.7.0_17-b02)
Java HotSpot™ 64-Bit Server VM (build 23.7-b01, mixed mode)
RUBY DIRECTORY READER:
Dir[’’].each do |file|
puts "** #{file}"
puts File.open(file).read
end
JAVA DIRECTORY READER:
import java.io.File;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
public class ListFiles
{
public static void main(String[] args)
{
BufferedReader br = null;
String aCurrentLine;
// Directory path here
String path = ".";
String files;
File folder = new File(path);
File[] listOfFiles = folder.listFiles();
for (int i = 0; i < listOfFiles.length; i++)
{
try {
files = listOfFiles[i].getName();
br = new BufferedReader(new FileReader(files));
System.out.println(files);
while ((aCurrentLine = br.readLine()) != null) {
System.out.println(aCurrentLine);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}