Android 上文件名中的外来字符

发布于 2024-12-02 02:21:38 字数 938 浏览 2 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

淡淡的优雅 2024-12-09 02:21:38

当我的手机通过 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.

棒棒糖 2024-12-09 02:21:38

你检查过编码吗?在 Eclipse 中:编辑 >设置编码> UTF-8 或 16

Did you check the encoding? in eclipse: Edit > set Encoding > utf-8 or 16

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文