简体   繁体   English

使用StreamReader从文件读取数据到第1行的数组中

[英]read data from file with StreamReader into array of line 1

 StreamReader text = new StreamReader(@textBox1.Text.ToString());
        String tempArray = text.ReadToEnd();
        char[] charA = tempArray.ToCharArray();
        Console.Write(charA);

output is like that 输出是这样的

****4****
**26*71**
871***694
*6*****4*
2*59*67*8
*8*****2*
658***471
**94*85**
****7****

but ı want to write and save 1d array on 1 row simple ; 但是我想在1行上简单地写和保存1d数组;

****4**** 26*71 871***694 *6*****4* 2*59*67*8 *8*****2* 658***471 94*85 ****7**** **** 4 **** 26 * 71 871 *** 694 * 6 ***** 4 * 2 * 59 * 67 * 8 * 8 ***** 2 * 658 *** 471 94 * 85 **** 7 ****

how can ı split this ? 我该如何拆分?

There's a lot going on in the code you've shared: 您共享的代码中发生了很多事情:

StreamReader text = new StreamReader(@textBox1.Text.ToString());
String tempArray = text.ReadToEnd();
char[] charA = tempArray.ToCharArray();
Console.Write(charA);

textBox1.Text already is a string , so you don't need the .ToString() behind that. textBox1.Text已经一个string ,因此您不需要在其后的.ToString()

By the way you don't need to prefix a field with a @ unless it's the field has the same name as a keyword. 顺便说一句,除非该字段与关键字具有相同的名称,否则您无需在该字段前面加上@ You're free to do it anyway if you like, though). 不过,如果您愿意,也可以自由地这样做。)

StreamReader text = new StreamReader(textBox1.Text);
String tempArray = text.ReadToEnd();
char[] charA = tempArray.ToCharArray();
Console.Write(charA);

Next, if all you want to do with the file is read all its contents as a string , you might want to use File.ReadAllText() as it doesn't keep the file open: 接下来,如果您要对文件进行的所有操作都以string形式读取其所有内容,则可能要使用File.ReadAllText()因为它不会使文件保持打开状态:

String tempArray = File.ReadAllText(textBox1.Text);
char[] charA = tempArray.ToCharArray();
Console.Write(charA);

The console outputs multiple lines but the char[] still really is a 1D array. 控制台输出多行,但char[]仍然实际上是一维数组。 The problem is that there are newline characters in the file, which get processed by the console. 问题在于文件中包含换行符,这些字符由控制台处理。

If you want to remove these newlines, it's easier to do so while you're still working with a string - because there are quite a few built-in features for this. 如果要删除这些换行符,则在仍使用string这样做会更容易-因为此功能有很多内置功能。 Someone already commented using string.Replace("\\r\\n", "") to do this trick: 有人已经使用string.Replace("\\r\\n", "")进行了评论:

String tempArray = File.ReadAllText(textBox1.Text);
tempArray = tempArray.Replace("\r", "");
tempArray = tempArray.Replace("\n", "");
char[] charA = tempArray.ToCharArray();
Console.Write(charA);

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

相关问题 C#使用带有DownloadFileAsync的StreamReader从文件读取行 - C# read line from file with StreamReader with DownloadFileAsync StreamReader从文本文件中读取一行并选择一个符合条件的单词 - StreamReader to read a line from text file and pick a word that matches the criteria 如何使用streamreader从选定的行读取数据 - how to read data using from selected line using streamreader StreamReader读取随机行 - StreamReader Read random line 使用 streamreader 以正确的语法从文件中读取路径 - read a path from file in a correct syntax with streamreader 如何从 streamreader 读取第一行的第一个字符? (例如 txt 文件中的字母 H - How can I read from streamreader the first character from the first line? ( Example the Letter H that is in the txt file 如何使用StreamReader从URI读取文件? - How to read a file from a URI using StreamReader? C#:使用StreamReader从txt文件中读取行,但Peek()返回-1,即使还有很多行 - C#: Using StreamReader to read line from txt file, but Peek() return -1 even there are a lot of lines left 如何使用StreamReader从第x行读取到第y行 - How to read from line x to line y using StreamReader 我想通过 StreamReader 从文本文件中读取数据,编辑特定字符串并将其写入文件中 - I want to read data from a text file via StreamReader, edit a specific string and write it to the file vis StreamWriter
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM