溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

C#?List如何生成Txt文檔且讀取Txt文檔封裝List

發布時間:2022-08-10 10:47:40 來源:億速云 閱讀:239 作者:iii 欄目:開發技術

C# List如何生成Txt文檔且讀取Txt文檔封裝List

在C#編程中,List<T>是一個非常常用的集合類型,它提供了靈活的數據存儲和操作方式。然而,在實際應用中,我們經常需要將List<T>中的數據持久化到文件中,或者從文件中讀取數據并重新封裝為List<T>。本文將詳細介紹如何使用C#將List<T>生成Txt文檔,以及如何從Txt文檔中讀取數據并封裝為List<T>。

目錄

  1. List簡介
  2. 將List生成Txt文檔
  3. 從Txt文檔讀取數據并封裝為List
  4. 封裝為通用方法
  5. 總結

List簡介

List<T>是C#中一個非常常用的泛型集合類,它允許我們存儲和操作一組類型為T的對象。List<T>提供了豐富的方法和屬性,如Add、Remove、Find、Sort等,使得我們可以方便地對集合中的元素進行操作。

將List生成Txt文檔

在實際應用中,我們經常需要將List<T>中的數據保存到文件中,以便后續使用或分享。Txt文檔是一種常見的文件格式,下面我們將介紹兩種將List<T>生成Txt文檔的方法。

2.1 使用StreamWriter寫入Txt文檔

StreamWriter是C#中用于寫入文本文件的類。我們可以使用StreamWriterList<T>中的每一行數據寫入Txt文檔。

using System;
using System.Collections.Generic;
using System.IO;

class Program
{
    static void Main()
    {
        List<string> dataList = new List<string>
        {
            "Line 1",
            "Line 2",
            "Line 3"
        };

        string filePath = "output.txt";

        using (StreamWriter writer = new StreamWriter(filePath))
        {
            foreach (string line in dataList)
            {
                writer.WriteLine(line);
            }
        }

        Console.WriteLine("Txt文檔生成成功!");
    }
}

在上面的代碼中,我們首先創建了一個List<string>對象dataList,然后使用StreamWriterdataList中的每一行數據寫入到output.txt文件中。

2.2 使用File.WriteAllLines寫入Txt文檔

File.WriteAllLines是C#中一個非常方便的方法,它可以將一個字符串數組或List<string>直接寫入到文件中。

using System;
using System.Collections.Generic;
using System.IO;

class Program
{
    static void Main()
    {
        List<string> dataList = new List<string>
        {
            "Line 1",
            "Line 2",
            "Line 3"
        };

        string filePath = "output.txt";

        File.WriteAllLines(filePath, dataList);

        Console.WriteLine("Txt文檔生成成功!");
    }
}

在這個例子中,我們直接使用File.WriteAllLines方法將dataList中的數據寫入到output.txt文件中。這種方法比使用StreamWriter更加簡潔。

從Txt文檔讀取數據并封裝為List

除了將List<T>生成Txt文檔,我們還經常需要從Txt文檔中讀取數據并重新封裝為List<T>。下面我們將介紹兩種從Txt文檔讀取數據并封裝為List<T>的方法。

3.1 使用StreamReader讀取Txt文檔

StreamReader是C#中用于讀取文本文件的類。我們可以使用StreamReader逐行讀取Txt文檔中的數據,并將其添加到List<T>中。

using System;
using System.Collections.Generic;
using System.IO;

class Program
{
    static void Main()
    {
        string filePath = "output.txt";
        List<string> dataList = new List<string>();

        using (StreamReader reader = new StreamReader(filePath))
        {
            string line;
            while ((line = reader.ReadLine()) != null)
            {
                dataList.Add(line);
            }
        }

        Console.WriteLine("從Txt文檔讀取數據并封裝為List<T>成功!");
        foreach (string line in dataList)
        {
            Console.WriteLine(line);
        }
    }
}

在這個例子中,我們使用StreamReader逐行讀取output.txt文件中的數據,并將其添加到dataList中。

3.2 使用File.ReadAllLines讀取Txt文檔

File.ReadAllLines是C#中一個非常方便的方法,它可以將Txt文檔中的所有行讀取到一個字符串數組中。我們可以直接將這個字符串數組轉換為List<T>。

using System;
using System.Collections.Generic;
using System.IO;

class Program
{
    static void Main()
    {
        string filePath = "output.txt";

        List<string> dataList = new List<string>(File.ReadAllLines(filePath));

        Console.WriteLine("從Txt文檔讀取數據并封裝為List<T>成功!");
        foreach (string line in dataList)
        {
            Console.WriteLine(line);
        }
    }
}

在這個例子中,我們使用File.ReadAllLines方法將output.txt文件中的所有行讀取到一個字符串數組中,然后將其轉換為List<string>。

封裝為通用方法

為了提高代碼的復用性,我們可以將上述功能封裝為通用的方法。下面是一個將List<T>生成Txt文檔和從Txt文檔讀取數據并封裝為List<T>的通用方法示例。

using System;
using System.Collections.Generic;
using System.IO;

class FileHelper
{
    public static void SaveListToFile<T>(List<T> list, string filePath)
    {
        List<string> stringList = list.ConvertAll(x => x.ToString());
        File.WriteAllLines(filePath, stringList);
    }

    public static List<T> LoadListFromFile<T>(string filePath, Func<string, T> converter)
    {
        List<T> list = new List<T>();
        string[] lines = File.ReadAllLines(filePath);

        foreach (string line in lines)
        {
            list.Add(converter(line));
        }

        return list;
    }
}

class Program
{
    static void Main()
    {
        List<int> dataList = new List<int> { 1, 2, 3, 4, 5 };
        string filePath = "output.txt";

        // 保存List<T>到Txt文檔
        FileHelper.SaveListToFile(dataList, filePath);
        Console.WriteLine("List<T>已保存到Txt文檔!");

        // 從Txt文檔讀取數據并封裝為List<T>
        List<int> loadedList = FileHelper.LoadListFromFile(filePath, int.Parse);
        Console.WriteLine("從Txt文檔讀取數據并封裝為List<T>成功!");

        foreach (int item in loadedList)
        {
            Console.WriteLine(item);
        }
    }
}

在這個例子中,我們定義了一個FileHelper類,其中包含兩個靜態方法:SaveListToFileLoadListFromFile。SaveListToFile方法將List<T>保存到Txt文檔中,LoadListFromFile方法從Txt文檔中讀取數據并封裝為List<T>。通過這種方式,我們可以輕松地在不同的項目中復用這些功能。

總結

本文詳細介紹了如何使用C#將List<T>生成Txt文檔,以及如何從Txt文檔中讀取數據并封裝為List<T>。我們探討了使用StreamWriterFile.WriteAllLinesList<T>寫入Txt文檔的方法,以及使用StreamReaderFile.ReadAllLines從Txt文檔中讀取數據的方法。最后,我們將這些功能封裝為通用的方法,以提高代碼的復用性。

通過掌握這些技巧,您可以輕松地在C#項目中實現數據的持久化和讀取,從而提高開發效率和代碼的可維護性。希望本文對您有所幫助!

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女