簡體   English   中英

Java中的UTF-8轉換

[英]UTF-8 convertion in Java

關於問題的簡要說明:

我有一個文件“лл.txt”。 當我將其名稱讀入String時,它變成“%D0%BB%D0%BB.txt”。 然后,如果我想打開並讀取該文件,它將找不到它

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

但是,如果我給我原來的名字,它就可以正常工作

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

所以問題是如何從“%D0%BB%D0%BB.txt”中獲取“лл.txt”? 快速搜索使我想到了這一點

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

但這對我不起作用

它似乎是使用utf-8字符集進行URL編碼的。 您可以從以下示例轉換中獲取提示:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM