用Java重命名文件后,有些文件在linux系统上或通过Java无法访问



我在使用Java的linux系统上读取(也许还写入(文件时遇到了问题。我的应用程序抱怨它无法读取一些音频文件,当我查看系统时,我注意到ls -l在这些文件上也失败了,所有的问题文件都是包含带引号的字符等的文件,如é,没有这些字符的文件也可以。

[root@N1-0247 Georges Bizet- Suites from Carmen & L'arlésienne]# pwd
/mnt/disk1/share/import/all/MusicUnmatched/WAV/Yan Pascal Tortelier/Georges Bizet- Suites from Carmen & L'arlésienne
[root@N1-0247 Georges Bizet- Suites from Carmen & L'arlésienne]# ls -l
ls: cannot access 20 - L' Arlésienne, suite for orchestra No. 1, from the incidental music- Farandole.WAV: No such file or directory
ls: cannot access 19 - L' Arlésienne, suite for orchestra No. 1, from the incidental music- Minuetto.WAV: No such file or directory
ls: cannot access 18 - L' Arlésienne, suite for orchestra No. 1, from the incidental music- Intermezzo.WAV: No such file or directory
ls: cannot access 17 - L' Arlésienne, suite for orchestra No. 1, from the incidental music- Pastorale.WAV: No such file or directory
ls: cannot access 16 - L' Arlésienne, suite for orchestra No. 1, from the incidental music- Carillon.WAV: No such file or directory
ls: cannot access 15 - L' Arlésienne, suite for orchestra No. 1, from the incidental music- Adagietto.WAV: No such file or directory
ls: cannot access 14 - L' Arlésienne, suite for orchestra No. 1, from the incidental music- Minuetto.WAV: No such file or directory
ls: cannot access 13 - L' Arlésienne, suite for orchestra No. 1, from the incidental music- Prélude.WAV: No such file or directory
ls: cannot access 08 - Carmen Suites for orchestra Nos. 1 & 2 (assembled by Ernest Guirard)- Chanson Du Toréador (Act II).WAV: No such file or directory
ls: cannot access 07 - Carmen Suites for orchestra Nos. 1 & 2 (assembled by Ernest Guirard)- Dans Bohème (Gypsy Song, Act II).WAV: No such file or directory
ls: cannot access 05 - Carmen Suites for orchestra Nos. 1 & 2 (assembled by Ernest Guirard)- Seguédille (Act I).WAV: No such file or directory
ls: cannot access 04 - Carmen Suites for orchestra Nos. 1 & 2 (assembled by Ernest Guirard)- Habeñera (Act I).WAV: No such file or directory
ls: cannot access 02 - Carmen Suites for orchestra Nos. 1 & 2 (assembled by Ernest Guirard)- Prélude (Prelude To Act I).WAV: No such file or directory
ls: cannot access 01 - Carmen Suites for orchestra Nos. 1 & 2 (assembled by Ernest Guirard)- Les Toréadors (Introduction To Act I).WAV: No such file or directory
total 192148
?????????? ? ?    ?           ?            ? 01 - Carmen Suites for orchestra Nos. 1 & 2 (assembled by Ernest Guirard)- Les Toréadors (Introduction To Act I).WAV
?????????? ? ?    ?           ?            ? 02 - Carmen Suites for orchestra Nos. 1 & 2 (assembled by Ernest Guirard)- Prélude (Prelude To Act I).WAV
-rw-rw-rw- 1 root root 36681194 Feb 21  2017 03 - Carmen Suites for orchestra Nos. 1 & 2 (assembled by Ernest Guirard)- La Grade Montante (Street Urchins' Chorus, Act I).WAV
?????????? ? ?    ?           ?            ? 04 - Carmen Suites for orchestra Nos. 1 & 2 (assembled by Ernest Guirard)- Habeñera (Act I).WAV
?????????? ? ?    ?           ?            ? 05 - Carmen Suites for orchestra Nos. 1 & 2 (assembled by Ernest Guirard)- Seguédille (Act I).WAV
-rw-rw-rw- 1 root root 16455464 Feb 21  2017 06 - Carmen Suites for orchestra Nos. 1 & 2 (assembled by Ernest Guirard)- Les Dragons D'Alcala (Entr'acte, Act II).WAV
?????????? ? ?    ?           ?            ? 07 - Carmen Suites for orchestra Nos. 1 & 2 (assembled by Ernest Guirard)- Dans Bohème (Gypsy Song, Act II).WAV
?????????? ? ?    ?           ?            ? 08 - Carmen Suites for orchestra Nos. 1 & 2 (assembled by Ernest Guirard)- Chanson Du Toréador (Act II).WAV
-rw-rw-rw- 1 root root 27743402 Feb 21  2017 09 - Carmen Suites for orchestra Nos. 1 & 2 (assembled by Ernest Guirard)- Intermezzo (Entr'acte, Act III).WAV
-rw-rw-rw- 1 root root 39886886 Feb 21  2017 10 - Carmen Suites for orchestra Nos. 1 & 2 (assembled by Ernest Guirard)- Marche Des Contrebandiers (Introduction To Act III).WAV
-rw-rw-rw- 1 root root 52822606 Feb 21  2017 11 - Carmen Suites for orchestra Nos. 1 & 2 (assembled by Ernest Guirard)- Nocturne (Micaela's Aria, Act III).WAV
-rw-rw-rw- 1 root root 23100378 Feb 21  2017 12 - Carmen Suites for orchestra Nos. 1 & 2 (assembled by Ernest Guirard)- Argonaise (Entr'acte, Act IV).WAV
?????????? ? ?    ?           ?            ? 13 - L' Arlésienne, suite for orchestra No. 1, from the incidental music- Prélude.WAV
?????????? ? ?    ?           ?            ? 14 - L' Arlésienne, suite for orchestra No. 1, from the incidental music- Minuetto.WAV
?????????? ? ?    ?           ?            ? 15 - L' Arlésienne, suite for orchestra No. 1, from the incidental music- Adagietto.WAV
?????????? ? ?    ?           ?            ? 16 - L' Arlésienne, suite for orchestra No. 1, from the incidental music- Carillon.WAV
?????????? ? ?    ?           ?            ? 17 - L' Arlésienne, suite for orchestra No. 1, from the incidental music- Pastorale.WAV
?????????? ? ?    ?           ?            ? 18 - L' Arlésienne, suite for orchestra No. 1, from the incidental music- Intermezzo.WAV
?????????? ? ?    ?           ?            ? 19 - L' Arlésienne, suite for orchestra No. 1, from the incidental music- Minuetto.WAV
?????????? ? ?    ?           ?            ? 20 - L' Arlésienne, suite for orchestra No. 1, from the incidental music- Farandole.WAV

我认为文件系统是UTF8,至少如果我设置了

export LANG=en_US.UTF-8

在我的配置文件中,文件名显示为正确的名称。

早些时候,这些文件被Java应用程序重命名为新名称,所以尽管报告了错误,但似乎是Java应用程序出现了一些问题,但我不知道是什么。

在我的Java启动脚本中,我有一行

export LC_ALL=en_US.UTF-8

我在其他linux系统或Windows、MacOS ecetera上没有遇到过这个问题。

尝试在java启动命令中声明-Dfile.encoding=UTF-8

首先,两点:

1(ls出现错误的事实表明,问题是文件名和文件系统之间的问题,而不是Java本身的问题。无论你的程序是用什么语言编写的,你都会遇到同样的问题——事实上,如果你试图直接在命令行上复制或重命名文件。

2( 问题不在于引号字符,正如引号字符出现在正确命名的文件中所示,例如:

-rw-rw-rw- 1 root root 52822606 Feb 21  2017 11 - Carmen Suites for orchestra Nos. 1 & 2 (assembled by Ernest Guirard)- Nocturne (Micaela's Aria, Act III).WAV

因此,问题出在unicode字符é上。

这个角色是这样的:https://www.compart.com/en/unicode/U+00E9,因此它由一个后跟E9的空字节组成。

问题是像xfs这样的POSIX文件系统不允许在文件名中有空字节(请参阅xfs文件系统中的所有非法字符是什么?(

问题是,在那个文件系统中不能有那个字符的文件名。

因此,您必须更改文件名或文件系统。

例如,此页面列出了文件系统,指明了那些允许在文件名中使用unicode的文件系统:

https://en.wikipedia.org/wiki/Filename#Comparison_of_filename_limitations

(顺便说一句,该列表中有苹果的HFS+,但有趣的是,它已被不允许在文件名中使用unicode的苹果文件系统APFS所取代。(https://developer.apple.com/library/archive/documentation/FileManagement/Conceptual/APFS_Guide/FAQ/FAQ.html(

另一种选择是更改Java程序以修改文件名,将é替换为e:

String safeFilename = filename.replaceAll("é", "e");

或者如果您喜欢:

String safeFilename = filename.replaceAll( "u00e9", "e" );

这是一个很有挑战性的难题,因为很难调试

我试着用java.nio.Files和java 11在Debian 10上用XFS和bash(作为ls内置(复制,但无法用名为é的文件复制这个问题。

请尝试获得更简单的复制场景和更多细节,否则我只能猜测这可能与有关

  • Unicode规范化的兼容性问题,请参阅:https://docs.oracle.com/javase/tutorial/i18n/text/normalizerapi.html
  • 还没有像Java.nio那样使用现代Java API。像这个问题中的文件Java.io.File:访问文件名编码无效的文件

问题还在于,这似乎很难调试,因为像strace这样的简单工具没有显示足够的文件名getdents系统调用字节信息,无法查看较低级别API的情况,请参阅:我的意思是

也许是时候采取不同的策略了?是否只将歌曲的完整标题写入播放列表文件?总有一些特殊的字符会在某些设置中引起问题,甚至是脚本中的空格(如果你是uncarefl(、目录分隔符(斜杠或反斜杠(等。(参见:这个相关问题(

您的文件系统已损坏-这不是应用程序级别的问题,但根据将物理磁盘内容转换为文件名+数据的文件系统驱动程序,物理磁盘上的内容无效。您需要检查哪个设备代表您的文件系统("mount"命令显示哪个设备被安装到哪个目录中,它可能类似于/dev/sda1。您需要将其重新安装为只读(如果这是您的根文件系统,这可能很棘手(,并运行fsck /dev/sda1(或您的任何设备(来修复它。不能100%确定您是否能取回这些文件。

最新更新