使用streamreader将文本文件写入wpf中的两个字符串数组
Text file to two string arrays in wpf using streamreader
我正在尝试将一个文本文件读入两个字符串数组。 Array1是全奇数行,array2全是偶数行。然后我将 array1 的所有项目添加到一个组合框,当它被选中时,或者当它被输入时,将 array2 输出到文本框。
到目前为止,我已经尝试了这里的一些方法,但最大的问题似乎是创建数组。我之前试图在这里获得帮助,但答案实际上并没有回答我的问题。它们必须是数组,而不是列表(我尝试过并且效果很好)。我真的对这整件事感到困惑,我尝试的代码现在是垃圾:
private void ReadFile(string filePath, string customerPhone, string customerName)
{
string line = string.Empty;
var fileSR = new StreamReader(filePath);
bool number = true;
while((line = fileSR.ReadLine()) != null)
{
if (number)
{
customerPhone(line);
number = false;
}
else
{
customerName(line);
number = true;
}
}
fileSR.Close();
}
我对整个过程失去了信心,但我需要找到一种方法让它发挥作用,然后我才能了解它为什么会起作用。
你快到了,只需使用 List<string>
。
private void ReadFile(string filePath, string customerPhone, string customerName)
{
string line = string.Empty;
using (var fileSR = new StreamReader(filePath))
{
bool number = true;
List<string> customerPhone = new List<string>();
List<string> customerName = new List<string>();
while((line = fileSR.ReadLine()) != null)
{
if (number)
{
customerPhone.Add(line);
number = false;
}
else
{
customerName.Add(line);
number = true;
}
}
fileSR.Close();
}
}
如果您只对 Arrays
感兴趣,您可以简单地调用 customerName.ToArray()
将其转换为数组。
Linq 解决方案
或者您可以使用 Linq
并执行此操作。
var bothArrays = File.ReadLines("filepath") // Read All lines
.Select((line,index) => new {line, index+1}) // index each line
.GroupBy(x=> x/2) // Convert into two groups
.SelectMany(x=> x.Select(s=>s.line).ToArray()) // Convert it to array
.ToArray();
您应该使用 集合 到 return 数据,比如 IList<String>
:
private static void ReadFile(String filePath,
IList<String> oddLines,
IList<String> evenLines) {
oddLines.Clear();
evenLines.Clear();
int index = 1; //TODO: start with 0 or with 1
foreach (String line in File.ReadLines(filePath)) {
if (index % 2 == 0)
evenLines.Add(line);
else
oddLines.Add(line);
index += 1;
}
}
使用
List<String> names = new List<String>();
List<String> phones = new List<String>();
ReadFile(@"C:\MyDate.txt", names, phones);
// If you want array representation
String[] myNames = names.ToArray();
String[] myPhones = phones.ToArray();
// Let's print out names
Console.Write(String.Join(Envrironment.NewLine, names));
请注意,使用 File.ReadLines
通常比 StreamReader
更方便,后者应包含在 using
:
中
// foreach (String line in File.ReadLines(filePath)) equals to
using (var fileSR = new StreamReader(filePath)) {
while ((line = fileSR.ReadLine()) != null) {
...
}
}
这成功了!我有这些 class 级字符串:
string cFileName = "customer.txt";
string[] cName = new string[0];
string[] cPhone = new string[0];
然后在 Window Loaded 事件中,但可以在它自己的方法中使用:
private void Window_Loaded_1(object sender, RoutedEventArgs e)
{
//read file on start
int counter = 0;
string line;
StreamReader custSR = new StreamReader(cFileName);
line = custSR.ReadLine();
while (custSR.Peek() != -1)
{
Array.Resize(ref cPhone, cPhone.Length + 1);
cPhone[cPhone.Length - 1] = line;
counter++;
line = custSR.ReadLine();
Array.Resize(ref cName, cName.Length + 1);
cName[cName.Length - 1] = line;
counter++;
line = custSR.ReadLine();
phoneComboBox.Items.Add(cPhone[cPhone.Length - 1]);
}
custSR.Close();
//focus when program starts
phoneComboBox.Focus();
}
我正在尝试将一个文本文件读入两个字符串数组。 Array1是全奇数行,array2全是偶数行。然后我将 array1 的所有项目添加到一个组合框,当它被选中时,或者当它被输入时,将 array2 输出到文本框。
到目前为止,我已经尝试了这里的一些方法,但最大的问题似乎是创建数组。我之前试图在这里获得帮助,但答案实际上并没有回答我的问题。它们必须是数组,而不是列表(我尝试过并且效果很好)。我真的对这整件事感到困惑,我尝试的代码现在是垃圾:
private void ReadFile(string filePath, string customerPhone, string customerName)
{
string line = string.Empty;
var fileSR = new StreamReader(filePath);
bool number = true;
while((line = fileSR.ReadLine()) != null)
{
if (number)
{
customerPhone(line);
number = false;
}
else
{
customerName(line);
number = true;
}
}
fileSR.Close();
}
我对整个过程失去了信心,但我需要找到一种方法让它发挥作用,然后我才能了解它为什么会起作用。
你快到了,只需使用 List<string>
。
private void ReadFile(string filePath, string customerPhone, string customerName)
{
string line = string.Empty;
using (var fileSR = new StreamReader(filePath))
{
bool number = true;
List<string> customerPhone = new List<string>();
List<string> customerName = new List<string>();
while((line = fileSR.ReadLine()) != null)
{
if (number)
{
customerPhone.Add(line);
number = false;
}
else
{
customerName.Add(line);
number = true;
}
}
fileSR.Close();
}
}
如果您只对 Arrays
感兴趣,您可以简单地调用 customerName.ToArray()
将其转换为数组。
Linq 解决方案
或者您可以使用 Linq
并执行此操作。
var bothArrays = File.ReadLines("filepath") // Read All lines
.Select((line,index) => new {line, index+1}) // index each line
.GroupBy(x=> x/2) // Convert into two groups
.SelectMany(x=> x.Select(s=>s.line).ToArray()) // Convert it to array
.ToArray();
您应该使用 集合 到 return 数据,比如 IList<String>
:
private static void ReadFile(String filePath,
IList<String> oddLines,
IList<String> evenLines) {
oddLines.Clear();
evenLines.Clear();
int index = 1; //TODO: start with 0 or with 1
foreach (String line in File.ReadLines(filePath)) {
if (index % 2 == 0)
evenLines.Add(line);
else
oddLines.Add(line);
index += 1;
}
}
使用
List<String> names = new List<String>();
List<String> phones = new List<String>();
ReadFile(@"C:\MyDate.txt", names, phones);
// If you want array representation
String[] myNames = names.ToArray();
String[] myPhones = phones.ToArray();
// Let's print out names
Console.Write(String.Join(Envrironment.NewLine, names));
请注意,使用 File.ReadLines
通常比 StreamReader
更方便,后者应包含在 using
:
// foreach (String line in File.ReadLines(filePath)) equals to
using (var fileSR = new StreamReader(filePath)) {
while ((line = fileSR.ReadLine()) != null) {
...
}
}
这成功了!我有这些 class 级字符串:
string cFileName = "customer.txt";
string[] cName = new string[0];
string[] cPhone = new string[0];
然后在 Window Loaded 事件中,但可以在它自己的方法中使用:
private void Window_Loaded_1(object sender, RoutedEventArgs e)
{
//read file on start
int counter = 0;
string line;
StreamReader custSR = new StreamReader(cFileName);
line = custSR.ReadLine();
while (custSR.Peek() != -1)
{
Array.Resize(ref cPhone, cPhone.Length + 1);
cPhone[cPhone.Length - 1] = line;
counter++;
line = custSR.ReadLine();
Array.Resize(ref cName, cName.Length + 1);
cName[cName.Length - 1] = line;
counter++;
line = custSR.ReadLine();
phoneComboBox.Items.Add(cPhone[cPhone.Length - 1]);
}
custSR.Close();
//focus when program starts
phoneComboBox.Focus();
}