Yevgeny Simkin
Yevgeny Simkin

Reputation: 28349

Is there a way to manually corrupt a file?

I have a process in which very sporadically I'm creating files which for reasons that I can't yet figure out I get a "java.io.FileNotFoundException: File is not a normal file." exception even though the file appears (in the file manager and text editor) to be entirely healthy. So, I'm trying to recreate this error and see how I can deal with it but to do that I need to manually create a file that would raise this exception, and I have no clue what I could manually do to a file to have it suddenly become "broken" in this way.

I realize that whatever I will do to the file is not the same thing that is wrong with the files that are throwing my exception, but for the purpose of writing testable code to verify that my safeguarding code works, that won't matter... I just need a broken file. I guess I could just throw the exception manually in some random cycle, but I was hoping I could get this to be more "realistic".

All help appreciated.

Upvotes: 3

Views: 3289

Answers (3)

Majid Laissi
Majid Laissi

Reputation: 19788

A corrupted file is still a file. A corrupted Word document for example is still a file but cannot be opened using a word processor because it's content is not readable.

The exception is thrown here :

public FilePartSource(File file) throws FileNotFoundException {
66          this.file = file;
67          if (file != null) {
68              if (!file.isFile()) {
69                  throw new FileNotFoundException("File is not a normal file.");
70              }
71              if (!file.canRead()) {
72                  throw new FileNotFoundException("File is not readable.");
73              }
74              this.fileName = file.getName();       
75          }
76      }

and occurs when your file is not a file, but rather something else (a directory? a link to an inexistant file?)

Upvotes: 2

Cratylus
Cratylus

Reputation: 54074

I think that you may be looking the wrong way, especially if the file is actually not corrupted according to text editors.
You should have posted the entire stack trace. I suspect that the issue is not with the file but with the path name.
Have you tried the result of File#isfile() at the point you get the exception? From javadoc (my emphasis)

public boolean isFile()

Tests whether the file denoted by this abstract pathname is a normal file. A file is normal if it is not a directory and, in addition, satisfies other system-dependent criteria. Any non-directory file created by a Java application is guaranteed to be a normal file.

I suspect that File#isfile() would return false prior to the exception and you should look into this API instead of trying to corrupt the file

Upvotes: 2

user1623834
user1623834

Reputation:

If you want to corrupt an existing file just write a function that opens the file and scrambles the bytes. You can also do this with a hex editor. If you want to generate garbage files you can just create a new file and write a random.nextBytes() in to it.

Upvotes: 1

Related Questions