Reputation: 71
I am trying to figure out how to work with files and I got confused due to the amount of different methods.
I have used current way of writting to the file. So far it seems to work fine, but I wonder if I have to add a file closing when the user exits the game? Another questions is why the file gets created without using File.Create()?
if (!File.Exists(path))
{
File.WriteAllText(path, "Date\t\t| ROM \t\t| Left Hand | Right Hand |\n");
}
The whole code is attached:
public class SavingData : MonoBehaviour
{
public static string path = @"C:\users\Desktop\Game1.txt";
void Start()
{
CreateFile();
}
void CreateFile()
{
if (!File.Exists(path))
{
File.WriteAllText(path, "Date\t\t| ROM \t\t| Left Hand | Right Hand |\n");
}
else
{
string date = "Login date: " + System.DateTime.Now + "\n";
File.AppendAllText(path, date);
}
}
public static void WriteToFile()
{
File.AppendAllText(path, "hellow\n");
}
public static void CloseFile()
{
}
}
Upvotes: 0
Views: 2831
Reputation: 1709
Actually, if you want more control you need to use FileStream. It gives you more control while writing into files. It allows you to keep the file handle open and just write data without any additional control.
But FileStream also has some type of disadvantages.
When a FileStream object does not have an exclusive hold on its handle, another thread could access the filehandle concurrently and change the position of the operating system's file pointer that is associated with the filehandle. In this case, the cached position in the FileStream object and the cached data in the buffer could be compromised. The FileStream object routinely performs checks on methods that access the cached buffer to ensure that the operating system's handle position is the same as the cached position used by the FileStream object.
On the other hands :
System.IO.File contains wrappers around file operations for basic actions such as saving a file, reading a file to lines, etc. It's simply an abstraction over FileStream.
So WriteAllText is the abstraction for over the Create, Save and Close and automatically doing it and you don't need to know each of the implementations.
So the basic answer to your question is: NO, you don't need to manually close file, it will do it automatically.
Upvotes: 1
Reputation: 270980
The documentation of File.WriteAllText
states:
Creates a new file, writes the specified string to the file, and then closes the file. If the target file already exists, it is overwritten.
So this single method does all the things that you thought you didn't do - creating the file and closing it.
If you would like to close the file manually, don't use File.WriteAllText
and File.AppendAllText
. Use another way of writing to a file, like a StreamWriter
.
This is what I would write if I had to use StreamWriter
.
public class SavingData : MonoBehaviour
{
public string path = @"C:\users\Desktop\Game1.txt";
private StreamWriter writer;
void Start()
{
CreateFile();
}
void CreateFile()
{
writer = new StreamWriter(path, true);
if (!File.Exists(path))
{
writer.WriteLine("Date\t\t| ROM \t\t| Left Hand | Right Hand |\n");
}
else
{
string date = "Login date: " + System.DateTime.Now + "\n";
writer.WriteLine(date);
}
}
public void WriteToFile()
{
writer.WriteLine("hellow\n");
}
public void CloseFile()
{
writer.Close();
}
}
Upvotes: 2