Pergunta

Briefly about issue:

I have a file "лл.txt". When I read it's name into String it become "%D0%BB%D0%BB.txt". Then if I want to open and read that file it fails to find it

// filename contains "%D0%BB%D0%BB.txt"
in = new BufferedReader(new FileReader(new File(filename))); // File not found

but it perfectly works if I give the original name

in = new BufferedReader(new FileReader(new File("лл.txt"))); // ok

so the question is ho to get "лл.txt" from "%D0%BB%D0%BB.txt" ? a quick search led me to this

byte[] bytes = str.getBytes( Charset.forName("UTF-8" ));
str = new String( bytes, Charset.forName("UTF-8") );

but it doesn't work for me

Foi útil?

Solução

It looks to be URL encoded with utf-8 charset. You can take cue from the following sample conversion:

System.out.println(URLEncoder.encode("лл.txt", "utf-8")); // Gives %D0%BB%D0%BB.txt
System.out.println(URLDecoder.decode("%D0%BB%D0%BB.txt", "utf-8")); // Gives лл.txt
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top