简体   繁体   English

如何逐行、部分地从文本文件中取出数据?

[英]How can I take data out of a text file line by line, part by part?

So I've been hitting my head against the wall trying to fill this list box with ID numbers saved into a text file.所以我一直在碰壁,试图用保存到文本文件中的 ID 号填充这个列表框。 It's simple enough to get the lines out of the file and put them into a list box like that, but that ID number is also shared with more information that I want to display in other text boxes.从文件中取出行并将它们放入这样的列表框中很简单,但是该 ID 号也与我想在其他文本框中显示的更多信息共享。

Edit So this is a comma delimited file and here is an example of a student's stored information:编辑所以这是一个逗号分隔的文件,这里是一个学生存储信息的例子:

1234561, Hubert, Huphrey, 123 Apple, Townsville, Some State, MM/DD/YYYY 1234561,Hubert,Huphrey,123 Apple,Townsville,一些 State,MM/DD/YYYY

To put it plainly, I have this text file that has multiple ID #'s, Names, and Addresses, but I just want to display the ID #'s so they can be clicked on from their list box so the rest of the information can be displayed on screen.说白了,我有这个文本文件,它有多个 ID #'s、Names 和 Addresses,但我只想显示 ID #'s,以便可以从它们的列表框中单击它们,以便信息的 rest可以显示在屏幕上。

I have already taken in a lot of advice, but my thick head can't figure out a few key points.我已经接受了很多建议,但我的脑袋却想不出几个关键点。 Let me start off by sharing the code I have so far:让我从分享我目前拥有的代码开始:

EDIT I have updated the method to work with the new Student Class.编辑我已经更新了使用新Student Class 的方法。 But Now I need to figure out how to work with the collection to get the properties of the class.但现在我需要弄清楚如何使用该集合来获取 class 的属性。

        List<Student> colStudents = new List<Student>();
        public MainWindow()
        {
            InitializeComponent();
        }

        private void btnGetStudents_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                //Define a Student object variable with the name StudentInfo
                string strStudentList;
                // Declare a StreamReader variable for Student Object
                StreamReader srStudent = new StreamReader("StudentData.txt");
                // intIndex is the index of the contact chosen
                int intIndex = lstStudentsID.SelectedIndex;

                //Clear list box to avoid having it fill up too much
                lstStudentsID.Items.Clear();

                // Read the disk file structure
                while (srStudent.EndOfStream == false)
                {
                    // Read disk file into a string variable using the ReadLine method.
                    strStudentList = srStudent.ReadLine();
                    // Tokenize the string read from Student object
                    string[] tokenize = strStudentList.Split(',');
                    // Create a student object instance from the Student class.
                    Student StudentInfo = new Student();
                    // Set properties of Student object to array element containing the student data
                    StudentInfo.StudentID = int.Parse(tokenize[0]);
                    StudentInfo.StudentFirstName = tokenize[1];
                    StudentInfo.StudentLastName = tokenize[2];
                    StudentInfo.StudentAddressCity = tokenize[3];
                    StudentInfo.StudnetBirthDate = tokenize[4];
                    // Add student object to collection
                    colStudents.Add(StudentInfo);
                    // Add ID's to the listbox
                    lstStudentsID.Items.Add(StudentInfo.StudentID);
                }
                //Close disk file after all records have been read in
                srStudent.Close();
            }
            catch(Exception ex)
            {
                MessageBox.Show("Experiencing the following disk problems: " + Environment.NewLine + ex.Message, "Disk File", MessageBoxButton.OK, MessageBoxImage.Exclamation);
            }
        }

EDIT : Now that I have the while loop working well enough, I need to either figure out how to utilize the collection or just find a way to use a second and third method to print out the selected student's full name and address编辑:现在我的 while 循环运行得很好,我需要弄清楚如何利用该集合,或者只是找到一种方法来使用第二种和第三种方法来打印出所选学生的全名和地址

Here's my new class:这是我的新 class:

public class Student
    {
        // Properties
        private double _ID;  // Student's ID, double just in case int is too small
        private string _StudentFirstName;  // Student's Name
        private string _StudentLastName;
        private string _StudentAddressStreet;
        private string _StudentAddressCity;
        private string _StudentBirthDate;

        // Constructor
        public Student()
        {
            _ID = 0;
            _StudentFirstName = "";
            _StudentLastName = "";
            _StudentAddressStreet = "";
            _StudentAddressCity = "";
            _StudentBirthDate = "";
        }

        // ID Property
        public double StudentID
        {
            get { return _ID; }
            set { _ID = value; }
        }

        // First Name Property
        public string StudentFirstName
        {
            get { return _StudentFirstName; }
            set { _StudentFirstName = value; }
        }

        // Last name Property
        public string StudentLastName
        {
            get { return _StudentLastName; }
            set { _StudentLastName = value; }
        }

        // Address Property
        public string StudentAddressStreet
        {
            get { return _StudentAddressStreet; }
            set { _StudentAddressStreet = value; }
        }

        // Address Property
        public string StudentAddressCity
        {
            get { return _StudentAddressCity; }
            set { _StudentAddressCity = value; }
        }

        // Address Property
        public string StudnetBirthDate
        {
            get { return _StudentBirthDate; }
            set { _StudentBirthDate = value; }
        }
    }

You create dictionary with Key as studentid and value as file line then using dictionary you can populate the list box when user select particular list item retrieve the data from dictionary do not perform IO operation.您创建字典,键作为 studentid,值作为文件行,然后使用字典,当用户 select 特定列表项从字典中检索数据不执行 IO 操作时,您可以填充列表框。

Dictionary studentInfo = new Dictionary();字典 studentInfo = new Dictionary();

    static void Main(string[] args)
    {


        StreamReader Student = new StreamReader("StudentData.txt");
        // intIndex is the index of the contact chosen
        intIndex = lstStudentsID.SelectedIndex;
        // Add student to colStudents list
        colStudents.Add(Student.ToString());
        // Read the disk file structure
        while (Student.EndOfStream == false)
        {
            // Read disk file into a string variable using the ReadLine method.
            strStudentInfo = Student.ReadLine();
            // Tokenize the string read from Student object
            string[] tokenize = strStudentInfo.Split(',');
            // Create a student object instance from the Student class.
            string temp = tokenize[0];

            studentInfo.Add("Key", strStudentInfo);

        }

    }
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.stream.Stream;

public class TestReadFile {

    public static void main(String args[]) {

        String fileName = "StudentData.txt";

        //read file into stream, try-with-resources
        try (Stream<String> stream = Files.lines(Paths.get(fileName))) {

            stream.forEach(System.out::println);

        } catch (IOException e) {
            e.printStackTrace();
        }

    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM