aspose file tools*
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 Spring in Action this week in the Spring 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!!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: how to check if file is empty