File Writing:
The following code shows you how write data in file or how to append data in file in both languages (C#.net and Java)
Code
In C#.net:
Way One:
using System.IO;
try
{
TextWriter tw = File.CreateText("Billy.txt");
tw.WriteLine("I am osama mursleen");
tw.Flush(); // To send data from stream to file
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
Way Two:
try
{
FileStream fs = new FileStream("abc1.txt", FileMode.Create, FileAccess.Write);
StreamWriter sw = new StreamWriter(fs);
sw.WriteLine("I am osama mursleen");
sw.Flush();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
In Java:
try {
FileWriter fw = new FileWriter(new File("NewFile.txt"));
fw.write("I Am Dangerous");
fw.close();
} catch (IOException e) {
e.printStackTrace();
}
Appending Data In File While Writing:
In C#.net:
FileStream fs = new FileStream("abc1.txt", FileMode.Append, FileAccess.Write);
StreamWriter sw = new StreamWriter(fs);
sw.WriteLine("I am osama mursleen");
sw.Flush();
In Java:
FileWriter fw = new FileWriter(new File("NewFile.txt"),true); // true means to append data in file, if you don’t write true then every time you write in that particular file it will overwrite the existing data.
fw.write("I Am Dangerous");
fw.close();
0 comments:
Post a Comment