I am trying to check if file doesn't have anything in it.
This is what I have which checks/create/write to file:
class LastUsed
{
private static string dir = Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles) + @"\Folder\";
private static string file = dir + @"\Settings.txt";
private string text;
public void CheckFileStatus()
{
if (!Directory.Exists(dir))
{
DirectoryInfo directory = Directory.CreateDirectory(dir);
}
if (!File.Exists(file))
{
using (FileStream fileStream = File.Create(file))
{
}
}
}
private void SetFileText(string writeText)
{
using (StreamWriter streamWriter = new StreamWriter(file))
{
streamWriter.Write(writeText);
}
}
private string GetFileText()
{
string readText;
using (StreamReader streamReader = File.OpenText(file))
{
readText = streamReader.ReadLine();
}
return readText;
}
public string Text
{
set
{
text = value;
SetFileText(text);
}
get
{
return GetFileText();
}
}
As we can see I can read/write file by using properties. So I have tried to check the Text property for null value but it doesn't seem to work.
How should I do this?
You can use the find command and other options as follows. The -s option to the test builtin check to see if FILE exists and has a size greater than zero. It returns true and false values to indicate that file is empty or has some data.
The above code works in a simple manner: peek() will peek at the stream and return, without removing, the next character. If it reaches the end of file, it returns eof() . Ergo, we just peek() at the stream and see if it's eof() , since an empty file has nothing to peek at.
Well, it's pretty easy to check emptiness for a file in Java by using the length() method of the java. io. File class. This method returns zero if the file is empty, but the good thing is it also returns zero if the file doesn't exist.
If your file was truly empty, with ls -l (or dir on windows) reporting a size of 0, os. path. getsize() should also return 0.
This code should do it
if (new FileInfo(fileName).Length ==0){
// file is empty
} else {
// there is something in it
}
fileName is the file path that you want to look for its size
Simply check if the file's size is zero bytes: Get size of file on disk.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With