C# txt文件读写
//读取文件内容
(1).使用FileStream类进行文件的读取,并将它转换成char数组,然后输出。
byte[] byData = new byte[];
char[] charData = new char[];
public void Read()
{
try
{
FileStream file = new FileStream("E:\\test.txt", FileMode.Open);
file.Seek(, SeekOrigin.Begin);
file.Read(byData, , ); //byData传进来的字节数组,用以接受FileStream对象中的数据,第2个参数是字节数组中开始写入数据的位置,它通常是0,表示从数组的开端文件中向数组写数据,最后一个参数规定从文件读多少字符.
Decoder d = Encoding.Default.GetDecoder();
d.GetChars(byData, , byData.Length, charData, );
Console.WriteLine(charData);
file.Close();
}
catch (IOException e)
{
Console.WriteLine(e.ToString());
}
}
(2).使用StreamReader读取文件,然后一行一行的输出。
public void Read(string path)
{
StreamReader sr = new StreamReader(path,Encoding.Default);
String line;
while ((line = sr.ReadLine()) != null)
{
Console.WriteLine(line.ToString());
}
}
//写文件
//写日志
public void fileWriter(string info)
{ //获取当前服务器的路径
string file = HttpContext.Current.Server.MapPath("~/logs/" + System.DateTime.Now.Year + System.DateTime.Now.Month + "log.log");
if (File.Exists(file) == true)
{
StreamWriter mySw = new StreamWriter(file, true);
mySw.WriteLine("");
mySw.WriteLine(System.DateTime.Now);
mySw.WriteLine(info);
mySw.Close();
}
else
{
using (File.Create(file)) ;
StreamWriter mySw = new StreamWriter(file, true);
mySw.WriteLine("");
mySw.WriteLine(System.DateTime.Now);
mySw.WriteLine(info);
mySw.Close();
} }
C# txt文件读写的更多相关文章
- python 简单的txt文件读写
1 读取txt文件.跟c相比,python的文件读写简直是方便的可怕 首先是读取文件 首先获得文件名称,然后通过 open函数打开文件,通过for循环逐行读出文件内容 #!python file by ...
- java 对txt文件读写(已经封装好)
读文件: public static String readTxt(String txtPath) { File file = new File(txtPath); if(file.isFile() ...
- C#txt文件读写基本操作
string strFileName=@"C:\Users\Administrator\Desktop\记事2.txt"; //判断是否存在 if (File.Exists(str ...
- python txt文件读写(追加、覆盖)
(1)在lucky.txt中新增内容(覆盖:每次运行都会重新写入内容) f = "lucky.txt" a =8 with open(f,"w") as fil ...
- Android TXT文件读写
package com.wirelessqa.helper; import java.io.FileInputStream; import java.io.FileOutputStream; impo ...
- Java逐行读写TXT文件
package help; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; imp ...
- cv.Mat 与 .txt 文件数据的读写操作
1.按OpenCV格式实现的 .txt 文件读写 可以用 cvSave 和 cvLoad 实现,格式和 .xml/.yml 的差不多,不过如果专用与 OpenCV 的数据读写,还是用 .xml/.y ...
- python应用:TXT文件的读写
python读写TXT文件不需要导入包 python中常用的读写方式: 文件打开模式 描述 r 以只读模式打开文件,并将文件指针指向文件头:如果文件不存在会报错 w 以只写模式打开文件,并将文件指针指 ...
- Python自动化--语言基础4--模块、文件读写、异常
模块1.什么是模块?可以理解为一个py文件其实就是一个模块.比如xiami.py就是一个模块,想引入使用就在代码里写import xiami即可2.模块首先从当前目录查询,如果没有再按path顺序逐一 ...
随机推荐
- C#实现窗体间的通信
以下将窗体间的几种通信实现方式做一下罗列:首先新建一个窗体Form1,在其中放置一个Textbox.Button控件.再新建一个窗体Form2,其上放置一个Button控件.具体代码示例如下: //F ...
- C语言初学 简单定义圆的面积计算问题
#include<stdio.h> #define PI 3.14159 main() { double a; scanf("%lf",&a); printf( ...
- DataSet排序
//排序 if (ds != null && ds.Tables.Count > 0) { DataVi ...
- Struts2学习笔记--Struts例子及开发流程
参考资料:http://blog.csdn.net/hntyzgn2010/article/details/5547753 http://chenlh.iteye.com/blog/464341 入门 ...
- Thinking in Java——笔记(21)
Concurrency However, becoming adept at concurrent programming theory and techniques is a step up fro ...
- SharedPreferences数据、openFileOutput文件、SQLite数据库文件存储位置
在模拟器中: SharedPreferences将XML文件保存在/data/data/<package name>/shared_prefs目录下, openFileOutput方法将文 ...
- RazorPad中的ModelProvider
在RazorPad的右侧 我们可以提供模型的结构,Json数据结构体 当提供多个的时候 是Json中的数组 [{ Name: "NI" }, { Name: &qu ...
- 递归转手工栈处理的一般式[C语言]
是任意形式的递归,是化解的一般式. 主题所谓的“递归调用化解为栈处理”,意思是,将递归函数调用化解为“一个由stack_push stack_pop stack_top等函数调用组成的循环式子”.这里 ...
- SQL使用单引号
SQL> select 'xxxx'oooo' from dual; ERROR: ORA-01756: quoted string not properly terminated SQL> ...
- PHP常用魔术方法(__call魔术方法:)
魔术方法 __call <?php //文件名:index.php define('a',__DIR__); include '/IMooc/Loader.php'; spl_autoload ...