G G*_* Gr 2 c# io openfiledialog
所以我有一个我正在尝试实施的通用数字检查:
public static bool isNumberValid(string Number)
{
}
Run Code Online (Sandbox Code Playgroud)
我想读取文本文件的内容(仅包含数字)并检查每一行的数字并使用isNumberValid. 然后我想将结果输出到一个新的文本文件,我得到了这么远:
private void button2_Click(object sender, EventArgs e)
{
int size = -1;
DialogResult result = openFileDialog1.ShowDialog(); // Show the dialog.
if (result == DialogResult.OK) // Test result.
{
string file = openFileDialog1.FileName;
try
{
string text = File.ReadAllText(file);
size = text.Length;
using (StringReader reader = new StringReader(text))
{
foreach (int number in text)
{
// check against isNumberValid
// write the results to a new textfile
}
}
}
catch (IOException)
{
}
}
}
Run Code Online (Sandbox Code Playgroud)
如果有人可以提供帮助,那么从这里卡住吗?
文本文件在列表中包含多个数字:
4564
4565
4455
等等。
我想写的新文本文件只是末尾附加了 true 或 false 的数字:
第4564章
您不需要一次将整个文件读入内存。你可以写:
using (var writer = new StreamWriter(outputPath))
{
foreach (var line in File.ReadLines(filename)
{
foreach (var num in line.Split(','))
{
writer.Write(num + " ");
writer.WriteLine(IsNumberValid(num));
}
}
}
Run Code Online (Sandbox Code Playgroud)
这里的主要优点是内存占用要小得多,因为它一次只加载文件的一小部分。
| 归档时间: |
|
| 查看次数: |
6458 次 |
| 最近记录: |