Android 上文件名中的外来字符
File outputFile = new File(path, clickedKey+".txt");
OutputStream fos = new FileOutputStream(outputFile);
fos.write(data.getBytes());
fos.close();
当路径不包含“ąóźżę”(波兰语中的特殊字符)之类的字符时,这段代码将起作用。 如果路径包含其中任何一个,fos.write
可以工作,但没有效果(新文件不是使用“外部”路径名创建的,但路径存在)。 我的问题是:我能做什么来解决它?
文件管理器应用程序(如“Astro”、“文件管理器”等)可以毫无问题地使用这些字符。
我也尝试这样做:
Charset charset = Charset.forName("UTF-8");
CharsetEncoder encoder = charset.newEncoder();
CharsetDecoder decoder = charset.newDecoder();
ByteBuffer bbuf = encoder.encode(CharBuffer.wrap(clickedKey+".txt"));
CharBuffer cbuf = decoder.decode(bbuf);
String s = cbuf.toString();
bbuf = encoder.encode(CharBuffer.wrap(path));
cbuf = decoder.decode(bbuf);
String path_s = cbuf.toString();
File outputFile = new File(path_s, s);
OutputStream fos = new FileOutputStream(outputFile);
fos.write(tab.getBytes());
fos.close();
但效果不佳。
File outputFile = new File(path, clickedKey+".txt");
OutputStream fos = new FileOutputStream(outputFile);
fos.write(data.getBytes());
fos.close();
This piece of code works when a path doesn't contain chars like: "ąóźżę" (special characters from polish language).
If the path contains any of them fos.write
works but there's no effect (new file isn't created with "foreign" path name, but the path exists).
My question is: what can I do to fix it?
File managers apps like "Astro", "File Manager" etc. work without any trouble with such characters.
I try also this:
Charset charset = Charset.forName("UTF-8");
CharsetEncoder encoder = charset.newEncoder();
CharsetDecoder decoder = charset.newDecoder();
ByteBuffer bbuf = encoder.encode(CharBuffer.wrap(clickedKey+".txt"));
CharBuffer cbuf = decoder.decode(bbuf);
String s = cbuf.toString();
bbuf = encoder.encode(CharBuffer.wrap(path));
cbuf = decoder.decode(bbuf);
String path_s = cbuf.toString();
File outputFile = new File(path_s, s);
OutputStream fos = new FileOutputStream(outputFile);
fos.write(tab.getBytes());
fos.close();
but it doesn't work as well.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
当我的手机通过 ADB 连接并且我正在使用 DDMS 检查新文件时,“外部”路径上没有任何文件。但事实上,文件就在手机里!
我检查过,直接在手机中使用文件管理器:)
很奇怪。但我解决了问题。
When my phone was connected by ADB and I was checking new files with DDMS, there wasn't any file on "foreign" path. But in fact, files were in phone!
I checked it, with file manager direct in my phone :)
It strange. But I solved the problem.
你检查过编码吗?在 Eclipse 中:编辑 >设置编码> UTF-8 或 16
Did you check the encoding? in eclipse: Edit > set Encoding > utf-8 or 16