数据访问之文件系统数据
来源:互联网 发布:淘宝看自己的五星评价 编辑:程序博客网 时间:2024/06/05 05:03
一:读文件(利用FileStream对象)
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
namespace ReadFile
{
class Program
{
static void Main(string[] args)
{
byte[] byteData = new byte[200];
char[] charData = new char[200];
try
{
string fileName = "D:\\2.txt";
FileStream aFile = new FileStream(fileName, FileMode.Open);
aFile.Seek(113, SeekOrigin.Begin);
aFile.Read(byteData, 0, 200);
}
catch (IOException e)
{
Console.WriteLine("A IO Exception has been throw!");
Console.WriteLine("{0}",e.ToString());
Console.ReadKey();
return;
}
Decoder decoder = Encoding.UTF8.GetDecoder();
decoder.GetChars(byteData,0,byteData.Length,charData,0);
Console.WriteLine("{0}",charData);
Console.ReadKey();
}
}
}
二:写文件(利用FileStream对象)
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
namespace WriteFile
{
class Program
{
static void Main(string[] args)
{
byte[] byteData;
char[] charData;
try
{
string fileName = "D:\\3.txt";
FileStream aFile = new FileStream(fileName, FileMode.Create);
charData = "HelloWorld".ToCharArray();
byteData = new byte[charData.Length];
//将字符数组转换为字节数组
Encoder e = Encoding.UTF8.GetEncoder();
e.GetBytes(charData, 0, charData.Length, byteData, 0, true);
aFile.Seek(0, SeekOrigin.Begin);
aFile.Write(byteData, 0, byteData.Length);
}
catch (IOException e)
{
Console.WriteLine("A IO Exception has been throw!");
Console.WriteLine("{0}", e.ToString());
Console.ReadKey();
return;
}
}
三:利用StreamWriter对象写文件
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Data;
namespace StreamWrite
{
class Program
{
static void Main(string[] args)
{
try
{
FileStream aFile = new FileStream("D:\\Log.txt", FileMode.OpenOrCreate);
StreamWriter sw = new StreamWriter(aFile);
bool truth = true;
sw.WriteLine("Hello!Log!"); 利用WriteLine()写一行字符串
sw.WriteLine("It is now {0} and things are longking good.", DateTime.Now.ToLongDateString());
sw.WriteLine("More than that,");
sw.Write("it's {0} that c# is fun.", truth);
sw.Close();
}
catch (IOException e)
{
Console.WriteLine("A IO Exception has been throw!");
Console.WriteLine("{0}", e.ToString());
Console.ReadKey();
return;
}
}
}
}
}
}
四:读文件(通过streamreader对象)
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
namespace StreamRead
{
class Program
{
static void Main(string[] args)
{
try
{
String fileName = "D:\\Log.txt";
FileStream fs = new FileStream(fileName, FileMode.Open);
StreamReader sr = new StreamReader(fs);
int a = sr.Read();//Read()是读出int型
Console.WriteLine("{0}", a);
string b =sr.ReadLine();//ReadLine()来读出字符串型
while (b != null)
{
Console.WriteLine("{0}", b);
b = sr.ReadLine();
}
sr.Close();
}
catch (IOException e)
{
Console.WriteLine("A error !");
Console.WriteLine(e.ToString());
}
Console.ReadKey();
}
}
}
- 数据访问之文件系统数据
- Linux文件系统之元数据
- SpringBoot之数据访问
- FAT12文件系统之数据存储方式详解
- FAT12文件系统之数据存储方式详解
- 大数据存储之分布式文件系统
- DataNode之文件系统数据集FsVolumeList
- 通用数据访问之我见
- 数据访问速度之差别
- Spring 数据访问之事务管理
- Spring 之数据访问 JdbcTemplate
- 微信小程序之数据访问
- SpringBoot之数据访问操作
- Linux0.12-文件系统-文件中数据的访问操作
- 数据访问
- 数据访问
- 数据访问
- 数据访问
- 对症下药 硬盘坏道检测与修复全攻略(组图)
- ios 加载资源中的Html
- EasyUI 1.3.3 火狐下弹出window框无法居中的解决办法
- 我的首个电子书软件--嘎嘎读书 的开发(二)
- C++箴言:谨慎使用多继承
- 数据访问之文件系统数据
- stream_component_open的主要步骤
- 计算机硬件术语
- 6种摆脱百度竞价恶意点击的技巧
- LeetCode 之 Add Binary
- Qt Create中文乱码
- 【CSDN常见问题解答】Java多种数据库连接
- Bundle savedInstanceState的作用
- 关于ListView的 addHeaderView(...) 方法