C#_File_Assembly
2015-09-24 08:38
302 查看
//--------------------------------------------^ ^-----------------------------------------------------------
static void Main(string[] args)
{
//1.get current assembly location
string currentPath=Assembly.GetExecutingAssembly().Location; //Get Executing Assembly()
string currentDirectoryPath=Path.GetDirectoryName(currentPath);
//2.create a file under current directory path
FileStream fs=File.Create(currentDirectoryPath+@"\test.txt");
fs.Close();
string msg="This is a test string..";
//3.change string to byte[]
byte[] bs=System.Text.Encoding.UTF-8.GetBytes(msg);
File.WriteAllBytes(currentDirectoryPath+@"\test.txt",bs);
//4.change byte[] to string
byte[] rbs=File.ReadAllBytes(currentDirectoryPath+@"\test.txt");
string rString=System.Text.Encoding.UTF-8.GetString(rbs);
Console.WriteLine(rString);
}
//-----------------------------------------------------------------------------------------
编码乱码问题:
ReaderAndWriter
static void Main(string[] args)
{
//1.get current assembly location
string currentPath=Assembly.GetExecutingAssembly().Location; //Get Executing Assembly()
string currentDirectoryPath=Path.GetDirectoryName(currentPath);
//2.create a file under current directory path
FileStream fs=File.Create(currentDirectoryPath+@"\test.txt");
fs.Close();
string msg="This is a test string..";
//3.change string to byte[]
byte[] bs=System.Text.Encoding.UTF-8.GetBytes(msg);
File.WriteAllBytes(currentDirectoryPath+@"\test.txt",bs);
//4.change byte[] to string
byte[] rbs=File.ReadAllBytes(currentDirectoryPath+@"\test.txt");
string rString=System.Text.Encoding.UTF-8.GetString(rbs);
Console.WriteLine(rString);
}
//-----------------------------------------------------------------------------------------
编码乱码问题:
#region 工资读写Exercise using(StreamReader sReader=new StreamReader("工资文件.txt",Encoding.Default)) { using (StreamWriter sWriter = new StreamWriter("new.txt", false)) { while (!sReader.EndOfStream) { string line = sReader.ReadLine(); string[] lineparts = line.Split(new char[] { '|'}); int doubleSalary = Convert.ToInt32(lineparts[1])*2; lineparts[1] = doubleSalary.ToString(); string newline = lineparts[0] + "|" + lineparts[1]; sWriter.WriteLine(newline); } } } #endregion
ReaderAndWriter
相关文章推荐
- C# 打印小票 POS
- c#:int--string
- C#面向对象资料-转
- 一个关于汽车的例子(C#面向对象编程)
- C#中使用ribbon界面
- C# datatable中重复数据去重
- C# datatable中重复数据去重
- C#中数组、ArrayList和List三者的区别
- c# 了解委托
- C#字符串是根据什么比较的?
- C# Mutex对象的使用
- c# wince 下获取当前输入法状态2
- 怎样写一个存储过程类来实现C#中对存储过程的调用
- C# --2007Access链接字符串
- C# 窗体间传值方法大汇总
- 从网址中获取域名,目录名,文件名的C#方法
- C#中如何把时间转换为季度取季度第一天等时间取值问题杂烩
- c# wince多个窗体共用一个主框架
- C#知识体系(一) --- 常用的LInq 与lambda表达式
- C#读取/写入值到前台页面TextBox文本框