I wrote a java program to print the length of files in Java
import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.file.Files;
public class FTPTest {
public static void main(String args[]) throws IOException {
File dirFile = new File("/home/chandakv/Desktop/test/vishal");
File[] ab = dirFile.listFiles();
for (int i = 0; i < ab.length; i++) {
System.out.println(ab[i].getName() + "\t" + ab[i].length());
}
}
}
It works fine with normal files but with one file having the file name:
bjliretimessitb_u.'192.168.200.38' ?ϵ? ?ڲ????? (Y).lnk
It is unable to read that file. If I am renaming that file (even removing any valid characters) it is working as expected. Till now, I am able to figure out that .lnk is not the problem and when renaming the file, the Linux explorer converts the encoding to ASCII so the file is being processed.
But, how to process the file normally without modifying its name.
FYI, on running the above program,
file.isFile() -> Returns false
file.isReadable() -> Returns false
file.length() -> Returns 0
And on linux "ls -l" shows file size as 207 bytes.
you should write file name character as unicode encoded \\uxxxx
in your code all unicode characters converted to question mark then the file does not exists
Try creating a new PrintStream
(don't use the default System.out
PrintStream
) using UTF8 encoding.
PrintStream out = new PrintStream(System.out, true, "UTF-8");
out.println(unicodeText);
Your code would look like this:
import java.io.PrintStream;
File dirFile = new File("/home/chandakv/Desktop/test/vishal");
File[] ab = dirFile.listFiles();
PrintStream out = new PrintStream(System.out, true, "UTF-8");
for (int i = 0; i < ab.length; i++) {
out.println(ab[i].getName() + "\t" + ab[i].length());
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.