簡體   English   中英

是否可以用Java寫入隱藏文件?

[英]Is it possible to write to a hidden file in Java?

我已經弄清楚了如何用Java創建隱藏文件,現在我需要向該文件中寫入大量數據。 我不斷收到以下異常: SEVERE: java.io.FileNotFoundException: <filepath>\\tmp (Access is denied)

這是我嘗試並獲得解決方案時采用的兩種方法,但是兩種方法的例外情況相同。 注意:在兩種情況下,toOverwrite都是隱藏文件。

File fileByteText = new File("./testFile.txt");
File toOverwrite = new File("./tmp");
//Assume toOverwrite is hidden

boolean toReturn = true;
    try {
        byte[] fileByteText = FileUtils.readFileToByteArray(toGetTextFrom);
                    FileUtils.writeByteArrayToFile(toOverwrite, fileByteText,    false);
                    toReturn = false;
                } catch (IOException e) {
                    bam.severe(e);
                    toReturn = true;
                }

使用相同的文件對象處理兩個:

try {
                String fileText = FileUtils.readFileToString(toGetTextFrom);
                FileWriter fw = new FileWriter(toOverwrite.getAbsoluteFile());
                BufferedWriter bw = new BufferedWriter(fw);
                bw.write(fileText);
                bw.close();
                toReturn = false;
            } catch (IOException e1) {
                bam.severe(e1);
                toReturn = true;
            }

當您嘗試寫入目錄類型的文件時,您可能會獲得異常。 檢查toOverWrite.isFile()返回的方法;

如果為假,則無法編寫。

在Unix中沒有魔術。 只需添加一個. 到您的文件名。 在Windows下,這無法使用Java實現。 您需要本機命令。 也許這適用於NIO2。

暫無
暫無

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

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