File Reading:
The following code shows you how to read a data from file, reading all file data in one string in both languages (C#.net and Java)
Code
In C#.net:
Way One:
using System.IO;
TextReader tw = File.OpenText(@"d:\Osama_Stuff\Billy.txt");
try
{
//Reading all file data without loop
Console.WriteLine(tw.ReadToEnd());
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
Way Two:
using System.IO;
try
{
FileStream fs = new FileStream("abc1.txt", FileMode.Open, FileAccess.Read);
StreamReader sw = new StreamReader(fs);
while (sw.ReadLine() != null)
{
Console.WriteLine(sw.ReadLine());
}
//Or you can use without loop like:
Console.WriteLine(sw.ReadToEnd());
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
In Java:
import java.io.*;
try {
FileReader fr = new FileReader(new File("NewFile.txt"));
BufferedReader br = new BufferedReader(fr);
String line = br.readLine();
// Java doesnot have ReadToEnd() function, so reading a file line by line
while(line != null) {
System.out.println(line);
line = br.readLine();
}
} catch (IOException ioEx) {
ioEx.printStackTrace();
}
0 comments:
Post a Comment