filename with characters other than english cant read

T

tomerbd1

Hi

I have a file /temp/1/תומר

i'm passing to my main(..) method the file as argument and then i tried
to read it i get a FileNotFoundException

If i print the filename in args[0] into a text file then i see that the
path of the file is fine its the same its: /temp/1/תומר

the exception is happening on this line:

InputStream is = new FileInputStream(args[0]);

anyone knows what to do?
 
S

Steve W. Jackson

Hi

I have a file /temp/1/™ÂÓ¯

i'm passing to my main(..) method the file as argument and then i tried
to read it i get a FileNotFoundException

If i print the filename in args[0] into a text file then i see that the
path of the file is fine its the same its: /temp/1/™ÂÓ¯

the exception is happening on this line:

InputStream is = new FileInputStream(args[0]);

anyone knows what to do?

According to the API, the FileNotFoundException can occur when the file
doesn't exist (seems not to apply here), the file is a directory (have
you checked to be certain?) or for *any* other reason they file cannot
be opened for reading.

So if it's positively present and positively a "normal" file, I would
think the stack trace would provide something at least mildly helpful.
You might also try creating a File object around that path and
confirming whether you're able to do the usual operations on it, like
confirming that it's a regular file, readable, etc.

= Steve =
 
O

Oliver Wong

Steve W. Jackson said:
I have a file /temp/1/תומר
[...]

So if it's positively present and positively a "normal" file, I would
think the stack trace would provide something at least mildly helpful.
You might also try creating a File object around that path and
confirming whether you're able to do the usual operations on it, like
confirming that it's a regular file, readable, etc.

You should check whether the OS or command line shell might be
interfering in anyway (e.g. perhaps it's trying to be "helpful" and doing
some sort of encoding which Java isn't expecting?)

- Oliver
 
S

Steve W. Jackson

Oliver Wong said:
You should check whether the OS or command line shell might be
interfering in anyway (e.g. perhaps it's trying to be "helpful" and doing
some sort of encoding which Java isn't expecting?)

- Oliver

I agree. That's kind of the idea I was thinking of when I suggested
trying to make a File object of it, looking to see what kinds of values
were there for its name, path, etc. While it wasn't connected to a
command line parameter, I've encountered issues with some special
characters before that were related to encoding, so this is definitely a
good area to look into.

= Steve =
 
C

Chris Uppal

If i print the filename in args[0] into a text file then i see that the
path of the file is fine its the same its: /temp/1/????

When you are being puzzled by character encoding issues (or where it seems as
if that /might/ be the problem), it is rarely profitable to "print" the strings
as strings.

When you dumped the string to file, what character encoding did you use for
that operation (or did you rely on the "system default") ? When you viewed the
resulting file in your editor, what character encoding did the editor decide to
use to decode the contents of the file ? Was the editor correctly set up so
when it printed the text to its window (or terminal, or whatever), the text was
rendered how it thought it should be ? There are many variables, and it is
easy to get lost. Worse, two errors can cancel each other out.

So, when you get this kind of problem, don't just print the strings, print the
values of the characters they contain.

-- chris
 
S

sendtomymailg1

Hi

I was doing some tests as you suggested and here is the outcome (I
still coudlnt resolve it...)

1. Trying to see if the file exists with File.exists() results with
false
2. I verified manually that the file is a file and not a folder
3. I tried both to hardcode the file path inside my java code and to
check if the file exists - resulted in false, and to read the file path
from another file (which also resulted in false)
here are the tests


when i do:

C:\>dir C:\temp\1\עברית
Volume in drive C has no label.
Volume Serial Number is F0C1-B8A2

Directory of C:\temp\1

03/29/2006 17:50 18 עברית
1 File(s) 18 bytes
0 Dir(s) 50,586,972,160 bytes free

C:\>

Code:
public static void main(String[] args) throws Exception {
File file = new File("/temp/1/תומר");
System.out.println(file.exists());
InputStream is = new FileInputStream(file);
}
Result:
java TDFileUtils
false
Exception in thread "main" java.io.FileNotFoundException: \temp\1\????
(The syst
em cannot find the file specified)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(Unknown Source)
at TDFileUtils.main(TDFileUtils.java:235)


Code:

public static void main(String[] args) throws Exception {
File file = new
File(TDFileUtils.getInstance().fileToString("/temp/1.txt")); // read
the file path from a different file that holds it
System.out.println(file.exists());
InputStream is = new FileInputStream(file);
}

public String fileToString(String filePath) throws
FileNotFoundException, IOException {
return new String(fileToBytes(filePath));
}

/** Returns the contents of the file in a byte array. */
public byte[] fileToBytes(File file) throws IOException {
InputStream is = new FileInputStream(file);

// Get the size of the file
long length = file.length();

// You cannot create an array using a long type.
// It needs to be an int type.
// Before converting to an int type, check
// to ensure that file is not larger than Integer.MAX_VALUE.
if (length > Integer.MAX_VALUE) {
// File is too large
}

// Create the byte array to hold the data
byte[] bytes = new byte[(int)length];

// Read in the bytes
int offset = 0;
int numRead = 0;
while (offset < bytes.length
&& (numRead=is.read(bytes, offset, bytes.length-offset))
offset += numRead;
}

// Ensure all the bytes have been read in
if (offset < bytes.length) {
throw new IOException("Could not completely read file
"+file.getName());
}

// Close the input stream and return bytes
is.close();
return bytes;
}

public byte[] fileToBytes(String filePath) throws IOException {
return fileToBytes(new File(filePath));
}

Result:
java TDFileUtils
false
Exception in thread "main" java.io.FileNotFoundException:
C:\temp\1\=ß°T· (The
ystem cannot find the file specified)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(Unknown Source)
at TDFileUtils.main(TDFileUtils.java:238)

And when i'm updating the method fileToString to be:
public String fileToString(String filePath) throws
FileNotFoundException, IOException {
return new String(fileToBytes(filePath),
System.getProperty("file.encoding"));
}

Then the result is:
java TDFileUtils
false
Exception in thread "main" java.io.FileNotFoundException:
C:\temp\1\=ß°T· (The s
ystem cannot find the file specified)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(Unknown Source)
at TDFileUtils.main(TDFileUtils.java:238)


The content of the file /temp/1.txt is: C:\temp\1\עברית which is a
file that exists... according to the dos
when i do:

C:\>dir C:\temp\1\עברית
Volume in drive C has no label.
Volume Serial Number is F0C1-B8A2

Directory of C:\temp\1

03/29/2006 17:50 18 עברית
1 File(s) 18 bytes
0 Dir(s) 50,586,972,160 bytes free

C:\>
 
T

tomerbd1

Running this code:

public static void main(String[] args) throws Exception {
File dir = new File("/temp/1");
File[] files = dir.listFiles();
System.out.println(files[0].getAbsolutePath());
System.out.println(files[0].exists());
}

do results in good results:

C:\>java org.tomdog.gmailer.util.TDFileUtils
C:\temp\1\?????
true

But then... why couldnt i get this file when i hardcoded its filepath
in my main? (i just want to say that my original problem, but i didnt
reach that yet since i'm still trying to overcome this problem is that
i right click a file and the file name is passed from windows as first
argument to my java app to args[0] and then i try to make an instance
of the File object with that file and the java app cant find it...but i
suspect that if i'll be able to overcome this problem of hardcoded
filepath or a filepath that comes from a file i'll be able to overcome
the right click issue).

Thanks

Tomer
 
T

tomerbd1

I'm working on a windows machine but anyway I copied the file to a
linux machine, and ran this command which resulted in:

[root@1]# ls | od -c
0000000 362 341 370 351 372 \n
0000006
[root@1]#
 
T

tomerbd1

I tried this:

public static void main(String[] args) throws Exception {
File dir = new File("/temp/1");
File[] files = dir.listFiles();
System.out.println(files[0].getAbsolutePath());
System.out.println(files[0].exists());
TDFileUtils.getInstance().stringToFile(files[0].getAbsolutePath(),
"/temp/realfilename.txt");
}

public void stringToFile(String str, String filePath) throws
FileNotFoundException, IOException {
BufferedWriter out = new BufferedWriter(new FileWriter(filePath));
out.write(str);
out.close();
}

And the result inside /temp/realfilename.txt is:

C:\temp\1\?????

I hope i didnt do a stupid mistake here... becuase it looks pretty
obvious to me that if it did get the real file since it says it
exists() -> true then it should have written the correct file path
realfilename.txt and not just with question marks...
 
G

Gordon Beaton

I was doing some tests as you suggested and here is the outcome (I
still coudlnt resolve it...)

Try getting a list of files like this:

File dir = new File("/temp/1");
File[] files = dir.listFiles();

Find your file in the resulting list and you should be able to open it
or rename it. Or see what the filename really is.

/gordon
 
C

Chris Uppal

But then... why couldnt i get this file when i hardcoded its filepath
in my main?

How do you know what the filename hardcoded into your test application was ?
There's no reason to think it was what it /looked/ as if it was, unless you
have been careful to tell your Java editor what character encoding to use,
/and/ have told your compiler what character encoding was in use in the source
file. As far as I know there is no easy way to tell javac that, so the chances
are that /it/ didn't think those characters meant what /you/ thought they meant
(even though they looked right in your editor).

When specifying characters that are not part of the 7-bit US-ASCII character
set, it is wise always to use the Unicode escape sequences. You /can/ use
other characters if:
- You are using the Sun standard tools and your character
encoding is Latin-1. (Not the case here since the characters you
mentioned are not in Latin-1).
- You are using a different Java compiler which is aware of
other character encodings.
Otherwise you must use Unicode escapes. The tool native2ascii (one of the
command-line tools that comes with the JDK) will help with that.

-- chris
 
G

Gordon Beaton

And the result inside /temp/realfilename.txt is:

C:\temp\1\?????

I hope i didnt do a stupid mistake here... becuase it looks pretty
obvious to me that if it did get the real file since it says it
exists() -> true then it should have written the correct file path
realfilename.txt and not just with question marks...

You need to specify a character encoding when you open the output
file. That means you can't use FileWriter, you need to use
FileOutputStream and an OutputStreamWriter instead. Try ISO-8859-1.

Based on the od output (in octal) you posted, the real name of the
file is spelled: ograve aacute oslash eacute uacute.

You see question marks because the mechanism you're using to display
the filename can't display those characters (and realize that the same
problem may occur depending how you try to display the contents of
realfilename.txt)

/gordon
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top