Search This Blog

Showing posts with label error. Show all posts
Showing posts with label error. Show all posts

Java FileNotFoundException While File is Existing There

Sometimes you my encounter a FileNotFoundException in java while you can see that the specified file is there. This can happen for several reasons.The java code may throw a  FileNotFoundException even when the file exists. This can be for different reasons.

Reason 1

The file does not exist really. Although you can see the file, the path you may have entered may be incorrect. For example, the following code may throw exception in some operating systems:

File f=new File("D:\myfile.txt");

This is because the file separator character is '\' in windows os. since \ is used to display non graphical characters like '\n' you should use its corresponding escape sequence. So "D:\\myfile.txt" would work on windows. If your java application is a cross platform program, you should consider using File.separator instead of '\\' because not all operating systems use '\\' as file separator character. So, the best approach is ;

File f=new File("D:"+File.separator+"myfile.txt");

File.separator is the system-dependent default name-separator character, represented as a string for convenience. This string contains a single character, namely File.separatorChar. If you want to get this character as a Character itself, you can use File.separatorChar. There can be other mistakes in the path.

Reason 2

The name you entered as a file may be actually a directory. So an exception can be thrown in java when you try to read from or write to such File object.

Reason 3

You cannot open it for reading or writing for some other reasons. You can check the status of file by calling the methods like f.exists(), f.canRead(), f.canWrite(), f.isFile() and f.isDirectory(). All these methods return a boolean. I hope these java method names describe well what they are intended for.

What Does Modal and Modeless (Non Modal) Mean in Java

In java, the dialogue boxes in JOptionPane class are very popular. Now when we use dialogue boxes, whether it is message dialogue box, input dialogue or any other,  we may sometimes need to make the parent container (often JFrame or JWindow) not focusable. In another words, we may have to prevent a (parent) component taking input from the user while one of its member component is active. (You may have noticed that when error message boxes are displayed, you cannot click its parent window on many software until you dismiss it by clicking 'OK' or 'close'). Such a property of a member or child component, is called modality. In java, you can use JDialog with or without modality. A JDialog can be either modal or modeless.
A Modal dialog box is a dialog box that blocks input to some other top-level windows in the application. The modal dialog box captures the window focus until it is closed, usually in response to a button press.
A Modeless dialog box is a dialog box that does not block input to any other top level window while it is shown.
See following java code example:

Why FileOutputStream or BufferedWriter Does Not Write New Line to File

The new-line character is system dependent. It depends on operating system. It can be \n, \r, \r\n or something else depending on the operating system. So, when we are writing a cross platform java program, it is important to take care when we use a line separator or new line character. It is always safe to use system specific line separator provided by JVM. You can get the system specific newline character in different ways.

Method 1:

System.lineSeparator() instead of new line character. The java method Sytem.lineSeparator() returns the newline character (line separator) as a strong. If you are appending a string on another with a line separator in between, it can be done as follows.

line1+System.lineSeparator()+line2

Method 2:

You can also get the line separator using the getProperty() method with property key line.separator.

System.getProperty("line.separator");

Method 3:

If your are using a BufferedReader object to write text into a file, you can use the newLine() method to write a newline into the file. If br is a BufferedReader object, br.newLine() will write a newline (line separator) into the file.

Method 4:

If you want to get the line separator as a character, you may use the static member Character.LINE_SEPARATOR. It returns the Unicode newline character.