怎么把一个String 变量保存到一个.txt文档中去?
怎么把一个String 变量保存到一个.txt文档中去?
各位高手,请问一下怎么实现?
[解决办法]
用StreamWriter打开文件,writeline就行了撒
[解决办法]
string a="a";
FileStream fs = new FileStream("C:\a.txt",FileMode.Append);
StreamWriter sr=new StreamWriter(fs);
sr.WriteLine(a);
sr.close();
fs.close();
[解决办法]
IOusing System.IO;String csdn="回帖是一种美德!每天回帖即可获得 10 分可用分!";StreamWriter mysw=new StreamWriter(path);mysw.Write(csdn);mysw.Close();
[解决办法]
换个变量名不行吗比如sw :)
[解决办法]
private void Form1_Load(object sender, EventArgs e)
{
SaveFile("123123123",@"C:\1.txt");
}
public static void SaveFile(string p_Text, string p_Path)
{
System.IO.StreamWriter _StreamWriter = new System.IO.StreamWriter(p_Path);
_StreamWriter.Write(p_Text);
_StreamWriter.Close();
}
[解决办法]
using System.IO;namespace 将string变量保存到txt文档中{ class Program { static void Main(string[] args) { string csdn="回帖是一种美德!每天回帖即可获得 10 分可用分!"; //使用Unicode编码,将csdn变量中的内容写到temp.txt文件中 StreamWriter sw = new StreamWriter("temp.txt", false, Encoding.Unicode); sw.Write(csdn); sw.Close(); Console.WriteLine("文件已经成功写入."); Console.ReadLine(); } }}
[解决办法]
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.IO;namespace ConsoleApplication1{ class Program { static void Main(string[] args) { using (StreamWriter sw = new StreamWriter(@"c:\1.txt", true)) //存在C:\1.txt里面 { string a = "i belive i can fly"; sw.Write(a); sw.Flush(); } } }}
[解决办法]
sr.close();
[解决办法]
haha
[解决办法]
System.IO.File.WriteAllText("c:\\1.txt", "Hello World!", Encoding.UTF8);
[解决办法]
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace File
{
public class Program
{
public static void Write(string filename, string content)
{
FileStream fs = new FileStream(filename, FileMode.Append);
StreamWriter sw = new StreamWriter(fs);
sw.WriteLine(content);
sw.Flush();
sw.Close();
}
static void Main(string[] args)
{
string str = "hello word";
Write(@"E:\string.txt", str);
}
}
}
看看这个吧
[解决办法]