Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Force IOException during file reading

I have a piece of code that reads data from a file. I want to force IOException in this code for testing purposes (I want to check if the code throws a correct custom exception in this case).

Is there any way to create a file which is protected from being read, for example? Maybe dealing with some security checks can help?

Please, note that passing the name of a non-existent file cannot help, because FileNotFoundException has a separate catch clause.

Here is the piece of code for better understanding of the question:

    BufferedReader reader = null;     try {          reader = new BufferedReader(new FileReader(csvFile));          String rawLine;         while ((rawLine = reader.readLine()) != null) {             // some work is done here         }      } catch (FileNotFoundException e) {         throw new SomeCustomException();     } catch (IOException e) {         throw new SomeCustomException();     } finally {         // close the input stream         if (reader != null) {             try {                 reader.close();             } catch (IOException e) {                 // ignore             }         }     } 
like image 322
DixonD Avatar asked Apr 02 '10 13:04

DixonD


People also ask

How do you cause IOException?

It can throw an IOException when the either the stream itself is corrupted or some error occurred during reading the data i.e. Security Exceptions, Permission Denied etc and/or a set of Exceptions which are derived from IOEXception .

What is the difference between IOException and exception?

Error , Exception , and RuntimeException all have several subclasses. For example, IOException is a subclass of Exception and NullPointerException is a subclass of RuntimeException . You may have noticed that Java differentiates errors from exceptions.

Which method will throw an IOException?

The Machine class has a public method called run(). This method declares that it throws an IOException. IOException (input-output exception) is part of the Java standard library.

How do you handle IOException?

IOException is a Java exception that occurs when an IO operation fails. Develop can explicitly handle the exception in a try-catch-finally block and print out the root cause of the failure. The developer can take the correct actions to solve this situation by having additional code in the catch and finally blocks.


1 Answers

Disclaimer: I have not tested this on a non-Windows platform, so it may have different results on a platform with different file locking characteristics.

If you lock the file beforehand, you can trigger an IOException when something attempts to read from it:

java.io.IOException: The process cannot access the file because another process has locked a portion of the file 

This works even if you are in the same thread.

Here's some sample code:

final RandomAccessFile raFile = new RandomAccessFile(csvFile, "rw"); raFile.getChannel().lock(); 
like image 147
Weston B Avatar answered Oct 04 '22 11:10

Weston B