Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

how to check if file is empty

 
kristian jarvi
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic