Write state into file