簡體   English   中英

使用 Java 重命名文件后,在 linux 系統上或通過 Java 無法訪問某些文件

[英]After renaming files with Java some not accessible on linux system or via 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 等系統上都沒有遇到過這個問題。

嘗試在 java start 命令中聲明-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

(順便說一句,在該列表中是 Apple 的 HFS+,但有趣的是,它已被 Apple 文件系統 APFS 取代,該文件系統不允許在文件名中使用 unicode - 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" );

這是一個很好的具有挑戰性的難題,因為這很難調試

我嘗試在帶有 XFS 和 bash(作為 ls 內置)的 Debian 10 上使用 Java 11 使用 java.nio.Files 進行重現,但無法使用名為 é 的文件重現該問題。

請嘗試使用更簡單的復制場景和更多細節,否則我只能猜測這可能與:

問題還在於,這似乎很難調試,因為像strace這樣的簡單工具沒有顯示關於文件名getdents系統調用的字節的足夠信息,以查看較低級別 API 的情況,請參閱:我的意思

也許是時候采取不同的策略了? 嘗試只將歌曲的完整標題寫入播放列表文件 總會有特殊字符在某些設置中會導致問題,如果您不小心,甚至會出現帶有腳本的空格、目錄分隔符(斜杠或反斜杠)等(請參閱: 此相關問題

您的文件系統已損壞 - 這不是應用程序級別的問題,但根據將物理磁盤內容轉換為文件名 + 數據的文件系統驅動程序,物理磁盤上的內容無效。 您需要檢查哪個設備代表您的文件系統(“mount”命令顯示哪個設備掛載到哪個目錄 - 它可能類似於/dev/sda1 。您需要將其重新掛載為只讀(可以是如果這是您的根文件系統,則很棘手)並運行fsck /dev/sda1 (或任何您的設備)來修復它。您不能 100% 確定您可以取回這些文件。

暫無
暫無

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

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