Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create File If File Does Not Exist

I need to get my code to read if file doesnt exist create else append. Right now it is reading if it does exist create and append. Here is the code:

if (File.Exists(path)) {     using (StreamWriter sw = File.CreateText(path))     { 

Would I do this?

if (! File.Exists(path)) {     using (StreamWriter sw = File.CreateText(path))     { 

Edit:

string path = txtFilePath.Text;  if (!File.Exists(path)) {     using (StreamWriter sw = File.CreateText(path))     {         foreach (var line in employeeList.Items)         {             sw.WriteLine(((Employee)line).FirstName);             sw.WriteLine(((Employee)line).LastName);             sw.WriteLine(((Employee)line).JobTitle);         }     } } else {     StreamWriter sw = File.AppendText(path);      foreach (var line in employeeList.Items)     {         sw.WriteLine(((Employee)line).FirstName);         sw.WriteLine(((Employee)line).LastName);         sw.WriteLine(((Employee)line).JobTitle);     }     sw.Close(); } 

}

like image 457
shan Avatar asked Apr 30 '12 11:04

shan


People also ask

Which mode create a new file if file does not exist?

"w" - Write - will create a file if the specified file does not exist.

Does append create a file if it doesn't exist?

Create file if it does not exist by Using append modeIf the file does not exist, it creates a new file for reading and writing. It will not truncate the file and append the content at end of the file as shown in the example below.

How do you create a file if file does not exist Java?

Java creating file with FileThe File's createNewFile method creates a new, empty file named by the pathname if a file with this name does not yet exist. The createNewFile returns true if the named file does not exist and was successfully created; false if the named file already exists.

Does StreamWriter create file if not exist?

C# StreamWriter append text This constructor initializes a new instance of the StreamWriter class for the specified file by using the default encoding and buffer size. If the file exists, it can be either overwritten or appended to. If the file does not exist, the constructor creates a new file.


2 Answers

You can simply call

using (StreamWriter w = File.AppendText("log.txt")) 

It will create the file if it doesn't exist and open the file for appending.

Edit:

This is sufficient:

string path = txtFilePath.Text;                using(StreamWriter sw = File.AppendText(path)) {   foreach (var line in employeeList.Items)                    {                         Employee e = (Employee)line; // unbox once     sw.WriteLine(e.FirstName);                          sw.WriteLine(e.LastName);                          sw.WriteLine(e.JobTitle);    }                 }      

But if you insist on checking first, you can do something like this, but I don't see the point.

string path = txtFilePath.Text;                  using (StreamWriter sw = (File.Exists(path)) ? File.AppendText(path) : File.CreateText(path))                  {                           foreach (var line in employeeList.Items)                          {                                sw.WriteLine(((Employee)line).FirstName);                                sw.WriteLine(((Employee)line).LastName);                                sw.WriteLine(((Employee)line).JobTitle);                          }                   }  

Also, one thing to point out with your code is that you're doing a lot of unnecessary unboxing. If you have to use a plain (non-generic) collection like ArrayList, then unbox the object once and use the reference.

However, I perfer to use List<> for my collections:

public class EmployeeList : List<Employee> 
like image 197
Chris Gessler Avatar answered Oct 03 '22 06:10

Chris Gessler


or:

using FileStream fileStream = File.Open(path, FileMode.Append); using StreamWriter file = new StreamWriter(fileStream); // ... 
like image 26
Mitja Bonca Avatar answered Oct 03 '22 06:10

Mitja Bonca