A Uncomplicated Illustration To Cheque If File Is Empty Inwards Java
One of the readers of my weblog Javarevisited emailed me yesterday close how to banking enterprise jibe if a file is empty inwards Java as well as that's why I am writing this post to demo that using an example. The mutual approach for checking if a file is empty or non is to start banking enterprise jibe if the file exists or non as well as so banking enterprise jibe if it contains whatever content, but Java has done that difficult operate for you. Well, it's pretty slow to banking enterprise jibe emptiness for a file inwards Java past times using length() method of the java.io.File class. This method returns null if the file is empty, but the skillful affair is it also returns null if the file doesn't exist. Which way y'all don't ask to banking enterprise jibe if the file exists or not.
Btw, when it comes to deciding the emptiness of file, unopen to application mightiness move unlike than others. For example, unopen to application may reckon a file amongst precisely whitespace equally empty but unopen to may not.
The length() method volition non reckon them empty, which way if y'all precisely type infinite bar inwards your file it looks empty but it won't move considered empty past times your Java program.
If y'all convey such requirement so y'all ask to pose a exceptional logic which involves reading the file as well as analyzing content.
Anyway, inwards this article, I'll explicate to y'all how to banking enterprise jibe if given file is empty inwards Java using a sample program. First, we'll without creating a file.
If y'all remember, File course of written report is used to stand upwards for both file as well as directory inwards Java but it's nix but a path. When y'all nation new File("C:\\myfile.txt") it doesn't exercise a file but a handgrip to that path. That's why nosotros tin exercise a File object to given path as well as telephone telephone its length() method to banking enterprise jibe if the file is empty or not.
After that, we'll exercise a novel file as well as verify again, this fourth dimension also our code should nation the file is empty because fifty-fifty though the file exists, at that topographic point is nix inwards it.
In the concluding step, we'll write something inwards the file as well as repeat the process. This fourth dimension our plan should impress the file is non empty.
You tin clearly run across that when nosotros start checked past times creating a File object, nosotros got the File is empty because the file didn't fifty-fifty exist. Remember, a file is non created inwards Java until y'all telephone telephone the createNewFile()method.
After that, nosotros checked i time again but this fourth dimension also nosotros got the File is empty output because fifty-fifty though the file exists it was truly empty, nix was written on it.
In the tertiary case, nosotros writing i work "Java is great" on file using FileWriter as well as so checked again. Boom!!!, this fourth dimension the file wasn't empty which way our code is working fine equally expected.
You tin also re-create glue this plan inwards your Eclipse IDE as well as run as well as it should impress the same output really start time.
But, afterwards that, the output volition alter as well as it volition e'er print:
File is non empty ...
File is non empty afterwards creation...
File is non empty afterwards writing something ...
Can y'all gauge why?
Well, because afterwards the start run of this program, y'all should already convey a cookbook.txt file inwards your Eclipse's projection directory as well as it is non empty so it prints non empty. The createNewFile() method volition non override if a file amongst the same bring upwards already exists.
Another thing, which y'all should proceed inwards hear is calling the flush() or close() method afterwards writing something on file. For example, if y'all take the flush() method as well as banking enterprise jibe if the file is empty or not, y'all may have wrong effect i.e. file is empty, fifty-fifty afterwards y'all convey written something on it.
Can y'all gauge why?
Well, because the content may non move flushed to the file when y'all checked, that's why calling flush() is of import earlier checking.
That's all close how to banking enterprise jibe if a file is empty inwards Java. You tin only telephone telephone the length() method without worrying close whether the file exists or not. This method returns null if the file doesn't be or file is truly empty i.e. doesn't comprise anything.
Further Learning
The Complete Java MasterClass on Udemy
Java Fundamentals Part 1 as well as ii on Pluarlsight
Extreme Java - Advanced Topics past times Heinz Kabutz
Core Java SE ix for the Impatient (2nd Edition)
10 Things Java Developers Should Learn inwards 2018
10 Books Java Developers Can Read inwards 2018
Thanks for reading this article so far. If y'all similar this article so delight portion amongst your friends as well as colleagues. If y'all convey whatever questions or feedback so delight driblet a note.
Btw, when it comes to deciding the emptiness of file, unopen to application mightiness move unlike than others. For example, unopen to application may reckon a file amongst precisely whitespace equally empty but unopen to may not.
The length() method volition non reckon them empty, which way if y'all precisely type infinite bar inwards your file it looks empty but it won't move considered empty past times your Java program.
If y'all convey such requirement so y'all ask to pose a exceptional logic which involves reading the file as well as analyzing content.
Anyway, inwards this article, I'll explicate to y'all how to banking enterprise jibe if given file is empty inwards Java using a sample program. First, we'll without creating a file.
If y'all remember, File course of written report is used to stand upwards for both file as well as directory inwards Java but it's nix but a path. When y'all nation new File("C:\\myfile.txt") it doesn't exercise a file but a handgrip to that path. That's why nosotros tin exercise a File object to given path as well as telephone telephone its length() method to banking enterprise jibe if the file is empty or not.
After that, we'll exercise a novel file as well as verify again, this fourth dimension also our code should nation the file is empty because fifty-fifty though the file exists, at that topographic point is nix inwards it.
In the concluding step, we'll write something inwards the file as well as repeat the process. This fourth dimension our plan should impress the file is non empty.
How to banking enterprise jibe if a file is empty inwards Java? Example
Without whatever farther ado, hither is our sample Java plan to demonstrate how to banking enterprise jibe if a given file is empty or not.
package tool; import java.io.File; import java.io.FileWriter; import java.io.IOException; /** * * Influenza A virus subtype H5N1 uncomplicated illustration to banking enterprise jibe if file is empty inwards Java */ public class Hello { public static void main(String args[]) throws IOException { // checking if file is empty // if file doesn't exits so also length() method volition consider // it empty. File newFile = new File("cookbook.txt"); if (newFile.length() == 0) { System.out.println("File is empty ..."); } else { System.out.println("File is non empty ..."); } // Now let's exercise the file newFile.createNewFile(); // let's elbow grease i time again as well as banking enterprise jibe if file is nonetheless empty if (newFile.length() == 0) { System.out.println("File is empty afterwards creating it..."); } else { System.out.println("File is non empty afterwards creation..."); } // Now let's write something on the file FileWriter author = new FileWriter(newFile); writer.write("Java is best"); writer.flush(); // don't forget to unopen the FileWriter writer.close(); // Now let's banking enterprise jibe i time again as well as verify if file is nonetheless empty if (newFile.length() == 0) { System.out.println("File is nonetheless empty afterwards writing into it..."); } else { System.out.println("File is non empty afterwards writing something ..."); } } } Output File is empty ... File is empty afterwards creating it... File is not empty afterwards writing something ...
You tin clearly run across that when nosotros start checked past times creating a File object, nosotros got the File is empty because the file didn't fifty-fifty exist. Remember, a file is non created inwards Java until y'all telephone telephone the createNewFile()method.
After that, nosotros checked i time again but this fourth dimension also nosotros got the File is empty output because fifty-fifty though the file exists it was truly empty, nix was written on it.
In the tertiary case, nosotros writing i work "Java is great" on file using FileWriter as well as so checked again. Boom!!!, this fourth dimension the file wasn't empty which way our code is working fine equally expected.
You tin also re-create glue this plan inwards your Eclipse IDE as well as run as well as it should impress the same output really start time.
But, afterwards that, the output volition alter as well as it volition e'er print:
File is non empty ...
File is non empty afterwards creation...
File is non empty afterwards writing something ...
Can y'all gauge why?
Well, because afterwards the start run of this program, y'all should already convey a cookbook.txt file inwards your Eclipse's projection directory as well as it is non empty so it prints non empty. The createNewFile() method volition non override if a file amongst the same bring upwards already exists.
Another thing, which y'all should proceed inwards hear is calling the flush() or close() method afterwards writing something on file. For example, if y'all take the flush() method as well as banking enterprise jibe if the file is empty or not, y'all may have wrong effect i.e. file is empty, fifty-fifty afterwards y'all convey written something on it.
Can y'all gauge why?
Well, because the content may non move flushed to the file when y'all checked, that's why calling flush() is of import earlier checking.
That's all close how to banking enterprise jibe if a file is empty inwards Java. You tin only telephone telephone the length() method without worrying close whether the file exists or not. This method returns null if the file doesn't be or file is truly empty i.e. doesn't comprise anything.
Further Learning
The Complete Java MasterClass on Udemy
Java Fundamentals Part 1 as well as ii on Pluarlsight
Extreme Java - Advanced Topics past times Heinz Kabutz
Core Java SE ix for the Impatient (2nd Edition)
10 Things Java Developers Should Learn inwards 2018
10 Books Java Developers Can Read inwards 2018
Thanks for reading this article so far. If y'all similar this article so delight portion amongst your friends as well as colleagues. If y'all convey whatever questions or feedback so delight driblet a note.
0 Response to "A Uncomplicated Illustration To Cheque If File Is Empty Inwards Java"
Post a Comment