如何在txt文件中读取控制台输出和打印数据? [英] how to read console output and print data in txt file?
问题描述
我有以下代码,它成功地从控制台窗口读取输入并创建txt文件,但输入不在文件中打印。这个代码有什么问题。请帮助我。
提前感谢。
<前lang =c#> public static void Main(字符串 [] args)
{
string 行;
for ( int i = 0 ; i < = 10 ; i ++)
{
Console.Write( );
string path = @ C:\\ \\Users\Priya\Desktop\m.txt跨度>;
使用(StreamWriter sw = new StreamWriter(路径))
{
line = Console.ReadLine();
// string str = line.ToString();
sw.WriteLine (线);
}
if (line!= null )
{
Console.WriteLine( dataddd数据传输: + line);
}
else
{
Environment.Exit( 0 跨度>);
}
}
} 尝试更改te行
使用(StreamWriter sw = new StreamWriter(path))
使用(StreamWriter sw = new StreamWriter(path) ,true)
这使得流编写器附加新行,而不是结束用每一行写你的文件。
但更常见的是我将for-loop放在using子句中,所以你只打开一次文件。
I have following code which successfully read input from console window and create txt file but input does not print in file . what wrong with this code .please help me .
thanks in advance.
public static void Main(String[] args)
{
string line;
for (int i = 0; i <= 10; i++)
{
Console.Write("");
string path = @"C:\Users\Priya\Desktop\m.txt";
using (StreamWriter sw = new StreamWriter(path))
{
line = Console.ReadLine();
// string str = line.ToString();
sw.WriteLine(line);
}
if (line != null)
{
Console.WriteLine("data transferd : " + line);
}
else
{
Environment.Exit(0);
}
}
}
Try changing te lineusing (StreamWriter sw = new StreamWriter(path))to
using (StreamWriter sw = new StreamWriter(path), true)
This makes stream writer append the new lines, instead of over writing your file with each line.
But more generally I would put the for - loop inside the using-clause, so you open the file just once.
这篇关于如何在txt文件中读取控制台输出和打印数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!