Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if file content is empty?

Tags:

c#

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?

like image 338
HelpNeeder Avatar asked Jan 10 '12 03:01

HelpNeeder


People also ask

How can I tell if a file is empty?

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.

How check if file is empty C++?

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.

How do you check whether a file is empty or not in Java?

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.

How do you check if a file is empty in Python?

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.


2 Answers

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

like image 62
AaA Avatar answered Oct 05 '22 04:10

AaA


Simply check if the file's size is zero bytes: Get size of file on disk.

like image 32
Jon Avatar answered Oct 05 '22 05:10

Jon