(1)
StringBuilder stringBuilder = new StringBuilder();
for (int i = 0; i < 10; i++)
{
stringBuilder.Append(i + " ");
}
String[] stringArray = stringBuilder.ToString().Split(' ');
int[] intArray = new int[stringArray.Length];
for (int i = 0; i < stringArray.Length; i++)
{
intArray[i] = Int32.Parse(stringArray[i]);
}
ArrayList arrayList = new ArrayList();
arrayList.Add(1);
arrayList.Add("fefe");
(2)
FileStream test = new FileStream("c:/test.txt", FileMode.Append);
StreamWriter testWrite = new StreamWriter(test, System.Text.Encoding.UTF8);
testWrite.WriteLine("gegege");
StreamReader testRead = new StreamReader(test, System.Text.Encoding.UTF8);
string line = testRead.ReadLine();
StringBuilder sb = new StringBuilder();
while (line != null)
{
sb.Append(line);
line = testRead.ReadLine();
}
(3)批量读入
public static string[] GetDoc(string DestiLocation)//得到文档
{
string[] direc = Directory.GetFiles(DestiLocation);//得到所有的文件名
if(direc.Length==0)
{
Console.WriteLine("READING PROCESS FAILED");//判断是否存在可读入的文件
}
string[] doc=new string[direc.Length];
int i=0;
foreach(string dir in direc)
{
FileStream fs = new FileStream(dir, FileMode.Open);
StreamReader sr=new StreamReader(fs,Encoding.Default);
doc[i]=sr.ReadToEnd();
i++;
}
return doc;
}
以下内容为引用其他网页
源文件:http://developer.51cto.com/art/200909/149698.htm
C# StreamWriter的操作是我们在实际C#写文件开发中会使用的常见方法,那么C# StreamWriter的具体操作是什么呢?这里还向你介绍一个C# StreamWriter写文件的实例,希望对你了解C# StreamWriter写文件的操作有所帮助。
C# StreamWriter写文件的操作实例:
- //实例化一个保存文件对话框
- SaveFileDialog sf = new SaveFileDialog();
- //设置文件保存类型
- sf.Filter = "txt文件|*.txt|所有文件|*.*";
- //如果用户没有输入扩展名,自动追加后缀
- sf.AddExtension = true;
- //设置标题
- sf.Title = "写文件";
- //如果用户点击了保存按钮
- if (sf.ShowDialog() == DialogResult.OK)
- {
- //实例化一个文件流--->与写入文件相关联
- FileStream fs = new FileStream(sf.FileName, FileMode.Create);
- //实例化一个StreamWriter-->与fs相关联
- StreamWriter sw = new StreamWriter(fs);
- //开始写入
- sw.Write(this.textBox1.Text);
- //清空缓冲区
- sw.Flush();
- //关闭流
- sw.Close();
- fs.Close();
- }
C# StreamWriter的操作细节问题基本就向你介绍到这里,希望对你了解和学习C# StreamWriter操作方面有所帮助。