This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes how to check if file is empty Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "how to check if file is empty" Watch "how to check if file is empty" New topic
Author

how to check if file is empty

kristian jarvi
Greenhorn

Joined: Jan 19, 2002
Posts: 9
I want to check if the file I'm trying to
read is empty and exit program if it's empty.
I've tried
if(bis.readLine().length()==0)
System.out.print("File is empty!");
}
else...
and
if(bis.readLine()==null)
System.out.print("File is empty!");
}
else...
but it's not working the way I want.
If i try to read an empty file it doesn't
print out the message i like.

File inputFile = new File ("test.txt");
FileReader fis =new FileReader(inputFile);
BufferedReader bis = new BufferedReader(fis);
for(String line=bis.readLine().trim();line != null;line=bis.readLine()) {
Jigar Gosar
Greenhorn

Joined: Jan 11, 2002
Posts: 10
well you can use the function of File class called Length
-------------------------------------------
public long length()
Returns the length of the file denoted by this abstract pathname.
Returns:
The length, in bytes, of the file denoted by this abstract pathname, or 0L if the file does not exist
Throws:
SecurityException - If a security manager exists and its
SecurityManager.checkRead(java.io.FileDescriptor) method denies read access to the file
-----------------------------------------------
now
if(myfile.Length()==0)
//print file empty
let me know if this works
[ February 19, 2002: Message edited by: Jigar Gosar ]
kristian jarvi
Greenhorn

Joined: Jan 19, 2002
Posts: 9
Originally posted by Jigar Gosar:
well you can use the function of File class called Length
-------------------------------------------
public long length()
Returns the length of the file denoted by this abstract pathname.
Returns:
The length, in bytes, of the file denoted by this abstract pathname, or 0L if the file does not exist
Throws:
SecurityException - If a security manager exists and its
SecurityManager.checkRead(java.io.FileDescriptor) method denies read access to the file
-----------------------------------------------
now
if(myfile.Length()==0)
//print file empty
let me know if this works
[ February 19, 2002: Message edited by: Jigar Gosar ]

Hi, Jigar
I can't get it to work.it doesn't
print the message i want, just an empty line
in command promt!!
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: how to check if file is empty
 
Similar Threads
Reading every line from a file to string
Readin a text file and cuttin the text
I Need help with file handling
Reading txt file where tab is used?
Problems using/making method!