/// <summary>
/// 文件读写操作/// </summary>
public partial class TestIO : DevComponents.DotNetBar.Office2007Form
{
public TestIO()
{
InitializeComponent();
} /// <summary>
/// 创建文件
/// </summary>
private void btnCreateFile_Click(object sender, EventArgs e)
{
string path = Application.StartupPath + @"\Test.txt";
FileStream fs = new FileStream(path, FileMode.Create);
StreamWriter sw = new StreamWriter(fs);
sw.WriteLine("This is a test file.");
sw.WriteLine("This is second line.");
sw.Close();
fs.Close(); // 也可以这样创建 StreamWriter
// StreamWriter sw = File.CreateText(path);
} /// <summary>
/// 读取文件
/// </summary>
private void btnReadFile_Click(object sender, EventArgs e)
{
string path = Application.StartupPath + "\\Test.txt";
textBoxX1.Text = string.Empty;
if (File.Exists(path))
{
FileStream fs = new FileStream(path, FileMode.Open);
StreamReader sr = new StreamReader(fs);
// 也可以这样创建 StreamReader
// File.OpenText(path);
string str = string.Empty;
while (true)
{
str = sr.ReadLine();
if (!string.IsNullOrEmpty(str))
{
textBoxX1.Text += str + "\r\n";
}
else
{
sr.Close();
fs.Close();
break;
}
}
}
else
{
MessageBox.Show("指定的路径下不存在此文件!");
}
} /// <summary>
/// 追加文件内容
/// </summary>
private void btnAppendFile_Click(object sender, EventArgs e)
{
string path = Application.StartupPath + "\\Test.txt";
FileStream fs = new FileStream(path, FileMode.Append);
StreamWriter sw = new StreamWriter(fs);
// 也可以这样创建 StreamReader
// StreamWriter sw = File.AppendText(path);
sw.WriteLine("This is three line.");
sw.Close();
fs.Close();
} /// <summary>
/// 复制文件
/// </summary>
private void btnCopyFile_Click(object sender, EventArgs e)
{
string oldPath = Application.StartupPath + "\\Test.txt";
string newPath = Application.StartupPath + "\\TestClone.txt";
File.Copy(oldPath, newPath);
} /// <summary>
/// 删除文件
/// </summary>
private void btnDeleteFile_Click(object sender, EventArgs e)
{
string path = Application.StartupPath + "\\TestClone.txt";
File.Delete(path);
} /// <summary>
/// 移动文件
/// </summary>
private void btnMoveFile_Click(object sender, EventArgs e)
{
string oldPath = Application.StartupPath + "\\Test.txt";
// 移动文件的同时也可以使用新的文件名
string newPath = "d:\\NewTest.txt";
File.Move(oldPath, newPath);
} /// <summary>
/// 创建目录
/// </summary>
private void btnCreateDirectory_Click(object sender, EventArgs e)
{
string path1 = "d:\\Jason1";
// 创建目录 Jason1
DirectoryInfo dDepth1 = Directory.CreateDirectory(path1);
// dDepth2 指向 dDepth1 创建的子目录 Jason2
DirectoryInfo dDepth2 = dDepth1.CreateSubdirectory("Jason2");
// 设置应用程序当前的工作目录为 dDepth2 指向的目录
Directory.SetCurrentDirectory(dDepth2.FullName);
// 在当前目录创建目录 Jason3
Directory.CreateDirectory("Jason3");
} private void btnDeleteDirectory_Click(object sender, EventArgs e)
{
string path = "d:\\Jason1";
DeleteDirectory(path);
} /// <summary>
/// 删除目录及其所有子目录,文件
/// </summary>
private static void DeleteDirectory(string path)
{
if (Directory.Exists(path))
{
foreach (string str in Directory.GetFileSystemEntries(path))
{
if (File.Exists(str))
{
File.Delete(str);
}
else
{
DeleteDirectory(str);
}
}
Directory.Delete(path);
}
else
{
MessageBox.Show("该目录不存在!");
}
} private void btnCopyDirectory_Click(object sender, EventArgs e)
{
string sourcePath = "d:\\Jason1";
string targetPath = "d:\\Jason2";
CopyDirectory(sourcePath, targetPath);
} /// <summary>
/// 复制目录及其所有内容
/// </summary>
/// <param name="sourcePath">源目录</param>
/// <param name="targetPath">目标目录</param>
private void CopyDirectory(string sourcePath, string targetPath)
{
// 该字符用于在反映分层文件系统组织的路径字符串中分隔目录级别(msdn)
// 在windows系统下实质上是为目录路径加上"\\"
if (targetPath[targetPath.Length - ] != Path.DirectorySeparatorChar)
{
targetPath += Path.DirectorySeparatorChar;
}
// 目标目录不存在则创建之
if (!Directory.Exists(targetPath))
{
Directory.CreateDirectory(targetPath);
}
// 获取文件系统(含目录和文件)
string[] fileList = Directory.GetFileSystemEntries(sourcePath);
foreach (string fileName in fileList)
{
if (Directory.Exists(fileName))
{
// Path.GetFileName(fileName) 可取出最后一个分级目录名或文件名
CopyDirectory(fileName, targetPath + Path.GetFileName(fileName));
}
else
{
// true 为可覆盖
File.Copy(fileName, targetPath + Path.GetFileName(fileName), true);
}
}
}
}

IO操作的更多相关文章

  1. [.NET] 利用 async & await 进行异步 IO 操作

    利用 async & await 进行异步 IO 操作 [博主]反骨仔 [出处]http://www.cnblogs.com/liqingwen/p/6082673.html  序 上次,博主 ...

  2. 文件IO操作..修改文件的只读属性

    文件的IO操作..很多同行的IO工具类都是直接写..但是如果文件有只读属性的话..则会写入失败..所以附加了一个只读的判断和修改.. 代码如下: /// <summary> /// 创建文 ...

  3. python之协程与IO操作

    协程 协程,又称微线程,纤程.英文名Coroutine. 协程的概念很早就提出来了,但直到最近几年才在某些语言(如Lua)中得到广泛应用. 子程序,或者称为函数,在所有语言中都是层级调用,比如A调用B ...

  4. JAVASE02-Unit08: 文本数据IO操作 、 异常处理

    Unit08: 文本数据IO操作 . 异常处理 * java.io.ObjectOutputStream * 对象输出流,作用是进行对象序列化 package day08; import java.i ...

  5. JAVASE02-Unit07: 基本IO操作 、 文本数据IO操作

    基本IO操作 . 文本数据IO操作 java标准IO(input/output)操作 package day07; import java.io.FileOutputStream; import ja ...

  6. IO操作概念。同步、异步、阻塞、非阻塞

    “一个IO操作其实分成了两个步骤:发起IO请求和实际的IO操作. 同步IO和异步IO的区别就在于第二个步骤是否阻塞,如果实际的IO读写阻塞请求进程,那么就是同步IO. 阻塞IO和非阻塞IO的区别在于第 ...

  7. Java基础复习笔记系列 七 IO操作

    Java基础复习笔记系列之 IO操作 我们说的出入,都是站在程序的角度来说的.FileInputStream是读入数据.?????? 1.流是什么东西? 这章的理解的关键是:形象思维.一个管道插入了一 ...

  8. java中的IO操作总结

    一.InputStream重用技巧(利用ByteArrayOutputStream) 对同一个InputStream对象进行使用多次. 比如,客户端从服务器获取数据 ,利用HttpURLConnect ...

  9. Linux系统编程--文件IO操作

    Linux思想即,Linux系统下一切皆文件. 一.对文件操作的几个函数 1.打开文件open函数 int open(const char *path, int oflags); int open(c ...

  10. Java之IO操作总结

    所谓IO,也就是Input与Output的缩写.在java中,IO涉及的范围比较大,这里主要讨论针对文件内容的读写 其他知识点将放置后续章节 对于文件内容的操作主要分为两大类 分别是: 字符流 字节流 ...

随机推荐

  1. bootstrapCDN和本地化

    因为公司网络环境较差和自己是菜鸟的原因,很简单的事情折腾了不少时间.测试开发的网页时候 更新速度总是很慢,这跟使用bootstrapCDN有关系,因为每次更新,它都要重新访问cdn.bootstrap ...

  2. dotproject 2.1.8 甘特图中文乱码解决

    1.安装中文语言包 下载地址为http://www.dotproject.net/dpDownloads/Language_Packs/Chinese_Simplified_(GBK)/dotproj ...

  3. Android驱动开发前的准备(二)

    搭建android开发环境 2.1 Android底层开发需要哪些工具 2.2 安装 JDK 2.3 搭建Android 应用程序开发环境 2.4安装Android NDK开发环境 2.5安装交叉编译 ...

  4. UIPickerView控件中自定义展示的字体大小及样式

    - (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger) ...

  5. Tomcat 在mac中Operation not permitted

    5.执行/Library/Tomcat/bin下的startup.sh,然后打开http://localhost:8080查看是否Tomcat已经启动,若要停止服务器就运行同目录下的shutdown. ...

  6. js本地图片预览

    相信大家都遇到过上传图片之前预览,网上找了很多,但都不是所有浏览器都支持,不过后来找到一个,但自己没有完全实验. 代码如下: <script> //使用IE条件注释来判断是否IE6,通过判 ...

  7. Ubuntu配置VNC server

    安装vncserver后,默认的配置下只有一个很"朴素"的图形界面(没有抓图,就一个黑白窗口),要支持Ubuntu的桌面,并且支持和windows之前复制粘贴文字,需要修改xsta ...

  8. UVA1103

    题意:输入以16进制的矩阵,先转换成2进制,之后输出形成的图案. 思路:先处理掉无关图案的0,之后一个图案一个图案的遍历,识别图案的方法就是有多少个圈圈.找到一个就全部标记为-1.并且记录圆圈的数目. ...

  9. [llvm] Call the LLVM Jit from c program

    stackoverflow: http://stackoverflow.com/questions/1838304/call-the-llvm-jit-from-c-program Another t ...

  10. CentOS上安装软件错误提示:configure: error: no acceptable C compiler found in $PATH

    CentOS上安装软件错误提示:configure: error: no acceptable C compiler found in $PATH 因为是centos linux,默认可以采用yum方 ...