0

I have figured out how to create a hidden file in Java, now I need to write large amounts of data to the file. I keep getting the following exception: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.

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;
            }
Reid Mac
  • 2,411
  • 6
  • 37
  • 64
  • http://stackoverflow.com/questions/13215716/ioerror-errno-13-permission-denied-when-trying-to-open-hidden-file-in-w-mod – stark Nov 28 '12 at 16:14

2 Answers2

0

You can get an Exception when you try to write to a file of type directory. Check what method toOverWrite.isFile() returns;

if false you cannot write.

AlexWien
  • 28,470
  • 6
  • 53
  • 83
  • What? Lol I'm assuming you are saying that I cannot write to a directory. I am not writing to a directory, it is a file. I added an extension to the filename, it still doesn't work. – Reid Mac Nov 28 '12 at 16:40
  • So isFile() returns true?, Then check what file.canWrite() returns – AlexWien Nov 28 '12 at 16:44
  • Yes, isFile() returns true, canWrite() also returns true. – Reid Mac Nov 28 '12 at 16:45
  • Then attach the jdk source to your projekt and debug into, then you see where, and hopefully why the exception is raised. – AlexWien Nov 28 '12 at 16:48
-1

There is no magic in Unix. Just prepend a . to your filename. Under Windows, this cannot be achieved with Java. You need native commands. May this works with NIO2.

Michael-O
  • 18,123
  • 6
  • 55
  • 121
  • Yes, I know how to create a hidden file in unix and windows. I am more concerned with writing to the file once it is hidden. – Reid Mac Nov 28 '12 at 16:38