I have a puzzling situation and I would need an expert opinion as to the cause of the phenomenon explained below. A couple of weeks ago, I have conducted a session titled "An overview .NET for Java developers" and as a part of it I wrote a quick class C# (3.5 framework) to read from a file and write to another file line by line (in an iteration). As my audience were java developers, I had the same code in a java class for side by side comparison. However, when I ran these classes on the same machine, to my surprise the java code consistently ran twice as fast than C# code. I have tried many optimizations in C# code to narrow the gap but could not succeed. There has to be an explanation and I am looking for somebody that can explain the cause. I am attaching the source code from both the classes for your reference.
Java class
public class ReadWriteTextFile {
static public String getContents(File aFile, String OutPutFileName) {
StringBuilder contents = new StringBuilder();
try {
BufferedReader input = new BufferedReader(new FileReader(aFile));
FileReader x = new FileReader(aFile);
try {
String line = null;
while (( line = input.readLine()) != null){
setContents(OutPutFileName, line + System.getProperty("line.separator"));
}
}
finally {
input.close();
}
}
catch (IOException ex){
ex.printStackTrace();
}
return contents.toString();
}
static public void setContents(String FileName, String aContents)
throws FileNotFoundException, IOException {
try {
FileWriter fstream = new FileWriter(FileName, true);
BufferedWriter out = new BufferedWriter(fstream);
out.write(aContents);
out.close();
} catch (Exception xe) {
xe.printStackTrace();
}
}
public static void main (String[] aArguments) throws IOException {
System.out.println(getDateTime() + ": Started");
File testFile = new File("C:\\temp\\blah.txt");
String testFile2 = "C:\\temp\\blahblah.txt";
for(int i=0; i<100; i++){
getContents(testFile, testFile2);
}
System.out.println(getDateTime() + ": Ended");
}
private synchronized static String getDateTime() {
DateFormat dateFormat = new SimpleDateFormat(
"yyyy/MM/dd HH:mm:ss");
Date date = new Date();
return dateFormat.format(date);
}
}
C# class
class ReadWriteTextFile
{
static void Main(string[] args)
{
System.Diagnostics.Trace.WriteLine(getDateTime() + ": Started");
String testFile = "C:\\temp\\blah.txt";
String testFile2 = "C:\\temp\\blahblah.txt";
for(int i=0; i<100; i++){
getContents(testFile, testFile2);
}
System.Diagnostics.Trace.WriteLine(getDateTime() + ": Ended");
}
static public void getContents(String sourceFile, String targetFile) {
try {
using (StreamReader r = File.OpenText(sourceFile))
{
String line;
while ((line = r.ReadLine()) != null)
{
setContents(targetFile, line);
}
r.Close();
}
}
catch (IOException ex){
Console.WriteLine(ex.StackTrace);
}
}
static public void setContents(String targetFile, String aContents)
{
try {
//FileStream fsO = new FileStream(targetFile, FileMode.Append);
//StreamWriter w = new StreamWriter(fsO);
FileStream fs = new FileStream(targetFile, FileMode.Append,
FileAccess.Write, FileShare.None);
using (StreamWriter w = new StreamWriter(fs))
{
w.WriteLine(aContents + "\n");
}
} catch (Exception xe) {
Console.WriteLine(xe.StackTrace);
}
}
private static String getDateTime() {
DateTime dt = DateTime.Now;
return dt.ToString("yyyy/MM/dd HH:mm:ss");
}
}