如何将文件的一部分读取到对象中
本文关键字:读取 对象 一部分 文件 | 更新日期: 2023-09-27 18:21:31
我有一个文件,我想扫描它,并将部分放入自定义对象中。
文件如下所示:
[Student]
Name=John
LastName=Doe
Username=user1
Password=pass1
[Teacher]
Name=Jane
LastName=Doe
Username=user2
Password=pass2
[Manager]
Name=Jason
LastName=Doe
Username=user3
Password=pass3
我有一个Profile
类,看起来是这样的:
public class Profile
{
public string studentName { get; set; }
public string studentLastName { get; set; }
public string studentUsername { get; set; }
public string studentPassword { get; set; }
public string teacherName { get; set; }
public string teacherLastName { get; set; }
public string teacherUsername { get; set; }
public string teacherPassword { get; set; }
}
所以,我只需要让学生和老师进入配置文件对象。以下是我开始的方式:
public Profile readFile(string filename)
{
var profile = new Profile();
using (StreamReader sr = new StreamReader(filename))
{
while (!sr.EndOfStream)
{
String line = sr.ReadLine();
if (line != null && line.Contains("Student"))
{
// and I am stuck here, not sure how to find
// the next lines, so i can take the values
// and put them in the profile
}
if (line != null && line.Contains("Teacher"))
{
// and I am stuck here, not sure how to find
// the next lines, so i can take the values
// and put them in the profile
}
}
}
}
您可以这样做:
var p=new Profile();
using (StreamReader sr = new StreamReader(filename))
{
while (!sr.EndOfStream)
{
String line = sr.ReadLine();
if (line != null && line.Contains("Student"))
{
p.studentName=GetLineValue(sr.ReadLine());
p.studentLastName=GetLineValue(sr.ReadLine());
p.studentUsername=GetLineValue(sr.ReadLine());
p.studentPassword=GetLineValue(sr.ReadLine());
}
if (line != null && line.Contains("Teacher"))
{
p.teacherName=GetLineValue(sr.ReadLine());
p.teacherLastName=GetLineValue(sr.ReadLine());
p.teacherUsername=GetLineValue(sr.ReadLine());
p.teacherPassword=GetLineValue(sr.ReadLine());
}
}
}
private string GetLineValue(string line)
{
return line.Substring(line.IndexOf('=')+1);
}
应该开始的伪代码位:
if (line != null && line.Contains("Student"))
{
var temp = new Student();
do {
string temp_line = String line = sr.ReadLine();
if //temp_line is empty
{
// you got the line. split on "="
// assign the student property to the value
// you'll have to use a bit of reflection here, but shouldn't be too hard
}
} while // not empty string (which means you're ready for the next object)
}
我可能会把它们分解成简单地填充每个对象(学生/老师等)的方法
好处是你不依赖订单,如果你想的话,你可以添加"哦,那个属性不存在,跳过它"的代码。。。
您可以使用反射。像这个
public static Profile readFile(string filename)
{
var profile = new Profile();
var properties = typeof(Profile).GetProperties().ToDictionary(q => q.Name, q => q);
using (StreamReader sr = new StreamReader(filename))
{
String mode = "";
while (!sr.EndOfStream)
{
String line = sr.ReadLine();
if (line == "[Student]")
{
mode = "student";
// and I am stuck here, not sure how to find
// the next lines, so i can take the values
// and put them in the profile
}
else if (line == "[Teacher]")
{
mode = "teacher";
}
else if (!string.IsNullOrEmpty(line))
{
var nameValues = line.Split(new char[] { '=' }, 2);
if (nameValues.Length < 2)
continue;
var key = mode + nameValues[0];
if (properties.ContainsKey(key))
{
var value = nameValues[1];
properties[key].SetValue(profile, value);
}
}
}
}
return profile;
}
您可以找到大括号,如下所示:
class memberLocation
{
public int start, end;
}
class Program
{
static void Main(string[] args)
{
memberLocation student, teacher, manager;
student = new memberLocation();
teacher = new memberLocation();
manager = new memberLocation();
String filePath = "data.txt";
StreamReader sr = new StreamReader(filePath);
String fileData = sr.ReadToEnd();
student.start = fileData.IndexOf("[Student]");
teacher.start = fileData.IndexOf("[Teacher]");
manager.start = fileData.IndexOf("[Manager]");
student.end = fileData.IndexOf(']', student.start + 9) - 9; // [Student] length = 9 - [Teacher] length
teacher.end = fileData.IndexOf(']', teacher.start + 9) - 9; // [Teacher] length = 9
manager.end = fileData.IndexOf(']', manager.start + 9) - 9; // [Manager] length = 9
String studentStr, teacherStr, managerStr;
if (student.end > 0)
studentStr = fileData.Substring(student.start, student.end - student.start);
else
studentStr = fileData.Substring(student.start);
if (teacher.end > 0)
teacherStr = fileData.Substring(teacher.start, teacher.end - teacher.start);
else
teacherStr = fileData.Substring(teacher.start);
if (manager.end > 0)
managerStr = fileData.Substring(manager.start, manager.end - manager.start);
else
managerStr = fileData.Substring(manager.start);
}
}