简体   繁体   English

是否可以用Java写入隐藏文件?

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

I have figured out how to create a hidden file in Java, now I need to write large amounts of data to the file. 我已经弄清楚了如何用Java创建隐藏文件,现在我需要向该文件中写入大量数据。 I keep getting the following exception: SEVERE: java.io.FileNotFoundException: <filepath>\\tmp (Access is denied) 我不断收到以下异常: SEVERE: java.io.FileNotFoundException: <filepath>\\tmp (Access is denied)

Here are two approaches I took to get try and get a solution, but I get the same exception for both approaches. 这是我尝试并获得解决方案时采用的两种方法,但是两种方法的例外情况相同。 Note: toOverwrite is the hidden file in both cases. 注意:在两种情况下,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;
                }

Approach two using the same file objects: 使用相同的文件对象处理两个:

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;
            }

You can get an Exception when you try to write to a file of type directory. 当您尝试写入目录类型的文件时,您可能会获得异常。 Check what method toOverWrite.isFile() returns; 检查toOverWrite.isFile()返回的方法;

if false you cannot write. 如果为假,则无法编写。

There is no magic in Unix. 在Unix中没有魔术。 Just prepend a . 只需添加一个. to your filename. 到您的文件名。 Under Windows, this cannot be achieved with Java. 在Windows下,这无法使用Java实现。 You need native commands. 您需要本机命令。 May this works with NIO2. 也许这适用于NIO2。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM