Search This Blog

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.

How to Filter Filetypes using File Extensions in JFileChooser - Java File Chooser

how to filter files in Jfilechooser by file types or file formats using extensions in filename java codefile name file type filefilter file filter filtering files
Screenshot of following example java program
In java, to browse files or folders from the file system, we use the JFileChooser class. You can use  the JFileChooser object in two modes, save mode or open mode. Here we discuss about filtering file type by file extensions for opening the file. By default, the JFileChooser object will have an AcceptAllFileFilter. This file filter will allow the user to select file of any file type. The file filter will display text All files. If you want to restrict the file chooser to open only one type of file, you should first remove the AcceptAllFileFilter. To do this, call the following method:

myfilechooser.removeChoosableFileFilter(createoropenchooser.getAcceptAllFileFilter());


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:

How to Get User Related Informations in Java

In java, there are some methods that will help you to know some user related informations. Here also,

System.getProperty() 
method can give some informations about the user like:

User account name
User Working directory
User Home directory

To get the username of current user on the computer just call

System.getProperty("user.name")

It will return the current username as a string.


To get the working directory of current user on the computer call

System.getProperty("user.dir")

It will return the path of working directory of current user as a string.


To get the home directory (user's home folder) of current user on the computer call

System.getProperty("user.home")

It will return the path to home directory (folder) of current user as a string.

How to Get Operating System Information in Java

Using java, you can gather some information about the operating system of the computer system on which the code is running. That us java provides some API classes that will help you to know about the operating system of the computer on which your application program is running. You can use System.getProperty() method for this purpose. There are some system property keys (strings) which can be used to get the respective information. The parameter to the System.getProperty() method is a string, the property key. The following values for the parameter string will give operating system related informations.

"os.arch"
Operating system architecture

"os.name"
Name of the operating system.

"os.version"
Version of Operating system.

Usage examples:

The following java program displays the Operating system related informations using getProperty() method.
System.out.println("Operating system architecture:"+System.getProperty("os.arch") ); System.out.println("Operating system Name:"+System.getProperty("os.name") ); System.out.println("Operating system version:"+System.getProperty("os.version") );

How to Get JRE installation Directory in a Java Application

To get the java runtime environment (JRE) installation directory of a computer, we can use the getProperty() method in System class. The key to be used for System.getProperty() method to get the java JRE installation directory is "java.home". System.getProperty("java.home") will return the java runtime environment(JRE) installation directory path as a string. the following code will display the jre installation directory path:


System.out.println(System.getProperty("java.home"));

How to Get Class Path in a Java Application


Class Path is the Path used to find directories and JAR archives containing class files. Elements of the class path are separated by platform-specific characters. To get class path from a running java application, you may use the getProperty() method in System class. The following java code will show you how to get class path in java.

String classpath=System.getProperty("java.class.path");
System.out.println("Class path="+ classpath);

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.

How to Make a Timer Controlled Progress Bar in Java

Java progress bar value updated using timer. update JProgressBar by thread, set progress of progress bar using timer thread
timer controlled progress bar in java
For some applications we require to use timer and progress-bars. Java has a swing class called
JProgressBar to create progress bars. It can have horizontal or vertical orientation. We can set the maximum and minimum values for JProgressBar. It can also have an indeterminate state in which the progressbar does not show a definite value and plays an animation to show that the process is in progress. JProgressBar can be used in indeterminate mode when neither can you determine how long a process would take to complete nor can you track its progress. In some other cases, you may have to use timers. Java provides a Timer class (Java.util.Timer). But you can create a very simple and flexible timer if you want. If the timer does not have crucial importance, you can use a thread that sleeps for every one second as a timer. I have been using such threads as timers for a long time and never failed. Such timers may fail (although very rarely) when the processor has very heavy workload.

Whatever, in a simple example, we will see how to create a timer using thread and how to use JProgressBar. In this example we control the JProgressBar using the timer. The progress in JProgressBar is set calling the method setValue(). In this example we are using a 1 minute timer. The progress bar should show the progress of timer. The java code for timer controlled JProgressBar is as follows:

Problem With JProgressBar setValue Method? JProgressBar Value Not Updating - Solution

I had a problem with a JProgressBar. I called setValue() method of the java swing class JProgressBar for an object but the JProgressBar is still sowing zero progress.This problem may occur for many people. People use the words:

Jprogressbar setvalue not upadting
jprogressbar.setvalue not working
jprogressbar setvalue problem

for googling this problem. All these keyword combinations have a presumption that the problem is with java not on our part, Actually, the problem is on our part but it is a silly mistake. I know two reasons for such problem.