本文整理汇总了C#中System.IO.StreamReader.GetSplittedLines方法的典型用法代码示例。如果您正苦于以下问题:C# StreamReader.GetSplittedLines方法的具体用法?C# StreamReader.GetSplittedLines怎么用?C# StreamReader.GetSplittedLines使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.IO.StreamReader
的用法示例。
在下文中一共展示了StreamReader.GetSplittedLines方法的11个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: ReadCities
/// <summary>
/// Read cities from text file
/// One entry per line, seperated with a tab (name, country, population, latitude, longitude)
/// </summary>
/// <param name="filename"></param>
/// <returns></returns>
public int ReadCities(string filename)
{
_logger.TraceEvent(TraceEventType.Information, 1, "ReadCities started");
int oldCount = _cities.Count;
try
{
using (TextReader tr = new StreamReader(filename))
{
_cities.AddRange(from c in tr.GetSplittedLines('\t')
select new City(c[0].Trim(),
c[1].Trim(),
int.Parse(c[2]),
double.Parse(c[3], CultureInfo.InvariantCulture),
double.Parse(c[4], CultureInfo.InvariantCulture)));
}
return _cities.Count - oldCount;
}
catch (Exception e)
{
Console.WriteLine("The file \"" + filename + "\" could not be read!");
Console.WriteLine(e.Message);
_logger.TraceEvent(TraceEventType.Critical, 1, e.StackTrace);
return -1;
}
finally
{
_logger.TraceEvent(TraceEventType.Information, 1, "ReadCities ended");
}
}
示例2: ReadCities
public int ReadCities(string filename)
{
logger.TraceEvent(TraceEventType.Information, 1, "ReadCities started");
var numberOfCitiesOld = _cities.Count;
var numberOfCities = 0;
try
{
using (TextReader reader = new StreamReader(filename))
{
var citiesAsStrings = reader.GetSplittedLines('\t');
// Old Version Lab 4
foreach (var cs in citiesAsStrings)
{
_cities.Add(new City(cs[0].Trim(), cs[1].Trim(), int.Parse(cs[2], CultureInfo.InvariantCulture), double.Parse(cs[3], CultureInfo.InvariantCulture), double.Parse(cs[4], CultureInfo.InvariantCulture)));
numberOfCities++;
}
return numberOfCities;
_cities.AddRange(from cs in citiesAsStrings
select new City(cs[0].Trim(),
cs[1].Trim(),
int.Parse(cs[2], CultureInfo.InvariantCulture),
double.Parse(cs[3], CultureInfo.InvariantCulture),
double.Parse(cs[4], CultureInfo.InvariantCulture)));
numberOfCities = (citiesAsStrings.Count() - numberOfCitiesOld);
}
return numberOfCities;
}
catch (Exception e)
{
if (isCritical(e))
{
throw;
}
Console.WriteLine("File: \"" + filename + "\" could not be found");
Console.WriteLine(e.Message);
logger.TraceEvent(TraceEventType.Critical, 1, e.StackTrace);
return -1;
}
finally
{
logger.TraceEvent(TraceEventType.Information, 1, "ReadCities ended");
}
}
示例3: ReadCities
public int ReadCities(string filename)
{
logger.TraceInformation("ReadCities started");
if (CityList == null)
{
CityList = new List<City>();
logger.TraceEvent(TraceEventType.Verbose, 0, "CityList initialized");
}
logger.TraceEvent(TraceEventType.Information, 0, "Reading cities form file {0}", filename);
CultureInfo info = CultureInfo.GetCultureInfo("en-GB");
try
{
using (TextReader reader = new StreamReader(filename))
{
int oldCount = CityList.Count();
IEnumerable<string[]> citiesAsStrings = reader.GetSplittedLines('\t');
CityList.AddRange(
citiesAsStrings
.Select(cs => new City(
cs[0].Trim(), // City Name
cs[1].Trim(), // Country
int.Parse(cs[2]), // population
double.Parse(cs[3].Trim(), info), // latitude
double.Parse(cs[4].Trim(), info) // longitued
)
));
int difference = CityList.Count - oldCount;
logger.TraceEvent(TraceEventType.Verbose, 0, "Added {0} cities to the actual list of cities", difference);
logger.TraceInformation("ReadCities ended");
return difference;
}
}
catch (IOException ex)
{
// Bad, ugly code. You should not catch an exception only
// because you want to log it!!!!
logger.TraceEvent(TraceEventType.Critical, 0, ex.StackTrace);
throw;
}
}
示例4: ReadRoutes
/// <summary>
/// Reads a list of links from the given file.
/// Reads only links where the cities exist.
/// </summary>
/// <param name="filename">name of links file</param>
/// <returns>number of read route</returns>
public int ReadRoutes(string filename)
{
int routesRead = 0;
// write log
traceSource.TraceEvent(TraceEventType.Information, 1, "ReadRoutes started");
try
{
using (TextReader reader = new StreamReader(filename))
{
foreach (string[] linkAsString in reader.GetSplittedLines('\t'))
{
City city1 = cities.FindCity(linkAsString[0]);
City city2 = cities.FindCity(linkAsString[1]);
// only add links, where the cities are found
if ((city1 != null) && (city2 != null))
{
routes.Add(new Link(city1, city2, city1.Location.Distance(city2.Location),
TransportModes.Rail));
routesRead++;
}
}
// write log
traceSource.TraceEvent(TraceEventType.Information, 1, "ReadRoutes ended");
}
}
catch (Exception e)
{
traceSource.TraceEvent(TraceEventType.Critical, 2, "FileNotFound: " + e.StackTrace.ToString());
}
return routesRead;
}
示例5: ReadCities
public int ReadCities(string _filename)
{
citiesTraceSource.TraceEvent(TraceEventType.Information, 1, "ReadCities started");
int counter = 0;
try
{
using (TextReader reader = new StreamReader(_filename))
{
IEnumerable<string[]> citiesAsStrings = reader.GetSplittedLines('\t').ToArray();
var newCities = citiesAsStrings.Select(line => new City(line[0].Trim(), line[1].Trim(), int.Parse(line[2]), double.Parse(line[3]), double.Parse(line[4])));
counter = newCities.Count();
cities.AddRange(newCities);
}
} catch (FileNotFoundException ex)
{
citiesTraceSource.TraceEvent(TraceEventType.Critical, 2, String.Format("Exception is catched: {0}", ex.StackTrace));
throw new FileNotFoundException("File does not exist");
}
citiesTraceSource.TraceEvent(TraceEventType.Information, 1, "ReadCities ended");
return counter;
}
示例6: ReadRoutes
/// <summary>
/// Reads a list of links from the given file.
/// Reads only links where the cities exist.
/// </summary>
/// <param name="filename">name of links file</param>
/// <returns>number of read route</returns>
public int ReadRoutes(string filename)
{
logger.TraceInformation("ReadRoutes started");
using (TextReader reader = new StreamReader(filename))
{
List<string[]> existingCities = reader.GetSplittedLines('\t')
.Where(s => cities.FindCity(s[0]) != null && cities.FindCity(s[1]) != null)
.ToList();
List<City[]> cits = existingCities.Select(str =>
str.Select(
s => cities.FindCity(s))
.ToArray()
).ToList();
List<Link> links = cits.Select(
la => new Link(la[0], la[1], la[0].Location.Distance(la[1].Location), TransportModes.Rail))
.ToList();
routes.AddRange(links);
}
logger.TraceInformation("ReadRoutes ended");
return Count;
}
示例7: ReadRoutes
//Kann abstract bleiben
/// <summary>
/// Reads a list of links from the given file.
/// Reads only links where the cities exist.
/// </summary>
/// <param name="filename">name of links file</param>
/// <returns>number of read route</returns>
public int ReadRoutes(string filename)
{
_logger.TraceEvent(TraceEventType.Information, 1, "ReadRoutes started");
var count = 0;
using (TextReader reader = new StreamReader(filename))
{
reader.GetSplittedLines('\t').ToList().ForEach(s =>
{
City c1 = _cities.FindCity(s[0]);
City c2 = _cities.FindCity(s[1]);
if (c1 != null && c2 != null)
{
_routes.Add(new Link(c1, c2, c1.Location.Distance(c2.Location), TransportModes.Rail));
count++;
}
});
}
_logger.TraceEvent(TraceEventType.Information, 1, "ReadRoutes ended");
return count;
}
示例8: ReadCities
public int ReadCities(string _filename)
{
traceSource.TraceInformation("ReadCities started");
traceSource.Flush();
try
{
using (TextReader reader = new StreamReader(_filename))
{
var citiesAsStrings = reader.GetSplittedLines('\t');
var c = citiesAsStrings.Select(city => new City(city[0].ToString(), city[1].ToString(), int.Parse(city[2]),
double.Parse(city[3], CultureInfo.InvariantCulture), double.Parse(city[4], CultureInfo.InvariantCulture))).ToArray();
cities.AddRange(c);
traceSource.TraceInformation("ReadCities ended");
traceSource.Flush();
return c.Count();
}
}
catch (FileNotFoundException e)
{
traceSourceErrors.TraceData(TraceEventType.Critical, 1, e.StackTrace);
traceSource.Flush();
throw(e);
}
}
示例9: ReadCities
//old version
//public int ReadCities(string filename)
//{
// string line;
// char[] delimiters = new char[] { '\t' };
// int countNewCities = 0;
// using (var reader = new StreamReader(filename))
// {
// while ((line = reader.ReadLine()) != null)
// {
// string[] parts = line.Split(delimiters);
// // Make name lowercase and then...
// string cityNameTemp1 = parts[0].ToLower();
// // ...make first letter uppercase
// string cityNameTemp2 = cityNameTemp1.First().ToString().ToUpper() + cityNameTemp1.Substring(1);
// City newCity = new City(cityNameTemp2, parts[1], Int32.Parse(parts[2]), Double.Parse(parts[3]), Double.Parse(parts[4]));
// cities.Add(newCity);
// countNewCities++;
// }
// }
// this.Count += countNewCities;
// return countNewCities;
//}
// Lab 4 Version
//public int ReadCities(string filename)
//{
// int countNewCities = 0;
// using (TextReader reader = new StreamReader(filename))
// {
// IEnumerable<string[]> citiesAsStrings = reader.GetSplittedLines('\t');
// foreach (string[] cs in citiesAsStrings)
// {
// cities.Add(new City(cs[0].Trim(), cs[1].Trim(),
// int.Parse(cs[2]),
// double.Parse(cs[3]),
// double.Parse(cs[4])));
// }
// this.Count += countNewCities;
// return countNewCities;
// }
//}
//Lab 6 Version
public int ReadCities(string filename)
{
int nofCitiesBefore = cities.Count;
using (TextReader reader = new StreamReader(filename))
{
var x = from cs in reader.GetSplittedLines('\t')
select new City(cs[0].Trim(), cs[1].Trim(),
int.Parse(cs[2]),
double.Parse(cs[3]),
double.Parse(cs[4]));
cities.AddRange(x);
}
Count = cities.Count();
return cities.Count - nofCitiesBefore;
}
示例10: ReadCities
public int ReadCities(string filename)
{
logger.TraceEvent(TraceEventType.Information, 1, "ReadCities started");
int countOld = cities.Count;
try
{
using (TextReader reader = new StreamReader(filename))
{
var citiesAsStrings = reader.GetSplittedLines('\t').ToList();
cities.AddRange(citiesAsStrings.Select(c => new City(c[0].Trim(),c[1].Trim(),int.Parse(c[2]),double.Parse(c[3], CultureInfo.InvariantCulture), double.Parse(c[4], CultureInfo.InvariantCulture))));
}
}
catch (Exception e)
{
logger.TraceEvent(TraceEventType.Critical, 1, e.StackTrace);
}
logger.TraceEvent(TraceEventType.Information, 1, "ReadCities ended");
return cities.Count-countOld;
}
示例11: ReadCities
public int ReadCities(string fileName)
{
int oldCount = Count;
citiesTrace.TraceInformation("ReadCities started");
try
{
using (TextReader reader = new StreamReader(fileName))
{
CityList.AddRange(from cs in reader.GetSplittedLines('\t')
select new City(cs[0], cs[1], int.Parse(cs[2]), double.Parse(cs[3]), double.Parse(cs[4])));
}
}
catch (FileNotFoundException e)
{
SourceLevels oldLevel = citiesTrace.Switch.Level;
citiesTrace.Switch.Level = SourceLevels.Critical;
citiesTrace.TraceEvent(TraceEventType.Critical, 1, e.StackTrace);
citiesTrace.Switch.Level = oldLevel;
}
citiesTrace.TraceInformation("Read Cities ended");
citiesTrace.Close();
return Count - oldCount;
}