简体   繁体   English

如何使用 StreamWriter 和 StreamReader 连接和读取数组值?

[英]How to wire and read an array values by using StreamWriter and StreamReader?

I'm creating programming to write and read values.我正在创建编程来写入和读取值。

But I couldn't find how to write and read "arrays" using StreamWriter and StreamReader.但我找不到如何使用 StreamWriter 和 StreamReader 编写和读取“数组”。

I would appreciate if you give me the answer.如果你能给我答案,我将不胜感激。

using System;
using static System.Console;
using System.IO;
using System.Text;

namespace writeReadArray
{
    public class Record
    {
        public void WriteFile(string filename)
        {
            FileStream fs = new FileStream(filename, FileMode.Create,FileAccess.Write);
            StreamWriter sw = new StreamWriter(fs);
            sw.Close();
            fs.Close();
        }
        public void ReadFile(string filename)
        {
            FileStream infile = new FileStream(filename, FileMode.Open, FileAccess.Read);
            StreamReader reader = new StreamReader(infile);

            reader.Close();
            infile.Close();
        }
    }
}

The array is in another class.该阵列位于另一个 class 中。

using System;
using static System.Console;
namespace writeReadArray
{
public class Arrays

    {
    private char[] AlphabetValues = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J' };

}

} }

You need to consider what you mean by "array".您需要考虑“数组”的含义。 In the trivial sense you can just use the write method that takes a char-array , or convert the char-array to a string using new string(AlphabetValues) and write that.在微不足道的意义上,您可以只使用带有 char-array 的 write 方法,或者使用new string(AlphabetValues)将 char-array 转换为字符串并写入。 Reading would be done by using ReadLine or ReadToEnd.读取将通过使用ReadLine或 ReadToEnd 完成。

But if you want to read and write multiple distinctive values separated from each other and other kinds of content you need some kind of separators to separate each value from each other and from any other content.但是,如果您想读取和写入多个彼此分离的独特值和其他类型的内容,您需要某种分隔符来将每个值彼此分开以及与任何其他内容分开。 This can be problematic when dealing with arrays of strings or characters, since our separator character(s) might occur in the strings we are storing, so we would need to replace these characters by some sequence of other characters, a process known as "escaping".这在处理字符串或字符的 arrays 时可能会出现问题,因为我们的分隔符可能出现在我们正在存储的字符串中,所以我们需要用一些其他字符序列替换这些字符,这个过程称为“转义”。

This is super annoying when dealing with floating point numbers, since using ',' as the separator would be very natural, ie "1.2, 3.4", but ',' is also used as the decimal separator for some cultures, so could result in "1,2, 3,4 if you are not careful.这在处理浮点数时非常烦人,因为使用 ',' 作为分隔符会很自然,即“1.2, 3.4”,但在某些文化中,',' 也用作小数分隔符,因此可能会导致“1,2、3,4,如果你不小心的话。

So for more complex content you might be better of using existing serialization libraries, like json , that takes care of all this for you.因此,对于更复杂的内容,您最好使用现有的序列化库,例如json ,它会为您处理所有这些。 Or use some kind of binary serialization if you do not need the data to be humanly readable.如果您不需要人类可读的数据,或者使用某种二进制序列化。

1-Write 1-写

 char[] AlphabetValues = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J' };

 `FileStream sb = new FileStream("MyFile.txt", FileMode.OpenOrCreate);` `StreamWriter sw = new StreamWriter(sb);` `sw.Write(AlphabetValues);` `sw.Close();`

2-Read 2-读

  string path = @"c:\temp\MyFile.txt";
    
     using (StreamReader sr = new StreamReader(path))
                {
                    while (sr.Peek() >= 0)
                    {
                        Console.WriteLine(sr.ReadLine());
                    }
                }

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

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