System.IO 应用一

摘要:
//sw.Write;//sw.Close();//必须关闭才会把字符串写入文件。////下面是为了测试FileInfo类而加进来的代码//FileInfofi=newFileInfo;//Console.WriteLine;//Console.WriteLine;//.txt//Console.WriteLine;//file.txt//Console.WriteLine;//3741//Console.WriteLine;//c:ile.txt//Console.WriteLine;//c:ile.txt//Console.WriteLine;//true//Console.ReadLine();////和上面的作用一样//File.WriteAllText;//创建新文件,如果文件存在则改写文件////File.Encrypt;//加密文件,加密后文件成绿色////File.Decrypt;//加密文件,解密后文件恢复原来的颜色//Console.WriteLine;//Console.ReadLine();//下面的示例在指定路径中创建一个文件,将一些信息写入该文件,再从文件中读取。//File.CreateText//创建或打开一个文件用于写入UTF-8编码的文本,此方法的返回类型是StreamWriter.如果由path指定的文件不存在,则创建该文件。

using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.IO;

namespace TestWebClientClass
{
class Program
{
static void Main(string[] args)
{
WebClient client = new WebClient();
client.BaseAddress = "http://www.microsoft.com/:";
string data = client.DownloadString("office");
Console.WriteLine(data);

//创建一个文本文件,把返回的字符串data写入文本文件
string m_path = @"c: ile.txt";
//File.Create(m_path); //此方法的返回类型是FileStream File.Create方法在这里会产生异常:线程有异常
File.CreateText(m_path); //File.CreateText在这里不会产生异常
StreamWriter sw = new StreamWriter(m_path, false);
//StreamWriter sw = File.CreateText(m_path);这一条语句和上面2条语句等价
sw.Write(data);
sw.Close();//必须关闭才会把字符串写入文件。
Console.WriteLine("写入成功");
Console.ReadLine();

////和上面的作用一样
//StreamWriter sw = new StreamWriter( @"c: ile.txt", false);//为false则改写,为true则追加在文件后面,true和false只决定是否改写或追加,与文件存不存在没有关系。
//sw.Write(data);
//sw.Close();//必须关闭才会把字符串写入文件。
////下面是为了测试FileInfo类而加进来的代码
//FileInfo fi = new FileInfo(@"c: ile.txt");
//Console.WriteLine("写入成功");
//Console.WriteLine(fi.Extension);//.txt
//Console.WriteLine (fi.Name);// file.txt
//Console.WriteLine (fi.Length );// 3741
//Console.WriteLine (fi.ToString());//c: ile.txt
//Console .WriteLine (fi.FullName );//c: ile.txt
//Console.WriteLine(fi.Exists);//true
//Console.ReadLine();

////和上面的作用一样
//File.WriteAllText(@"c: ile01.txt", data, Encoding.UTF8);//创建新文件,如果文件存在则改写文件
////File.Encrypt(@"c: ile01.txt");//加密文件,加密后文件成绿色
////File.Decrypt(@"c: ile01.txt");//加密文件,解密后文件恢复原来的颜色
//Console.WriteLine("写入成功");
//Console.ReadLine();

//下面的示例在指定路径中创建一个文件,将一些信息写入该文件,再从文件中读取。
string path = @"c: empMyTest.txt";
try
{
// Delete the file if it exists.
if (File.Exists(path))
{
// Note that no lock is put on the
// file and the possibliity exists
// that another process could do
// something with it between
// the calls to Exists and Delete.
File.Delete(path);
}
// Create the file.
using (FileStream fs = File.Create(path))
{
Byte[] info = new UTF8Encoding(true).GetBytes("This is some text in the file.");
// Add some information to the file.
fs.Write(info, 0, info.Length);

}
// Open the stream and read it back.
using (StreamReader sr = File.OpenText(path))
{
string s = "";
while ((s = sr.ReadLine()) != null)
{
Console.WriteLine(s);
}
}
}
catch (Exception Ex)
{
Console.WriteLine(Ex.ToString());
}

/*---------------------知识扩充---------------------------------*/
//File.Create(string Path )//创建文件,此方法的返回类型是FileStreamv,如果指定的文件不存在,则创建该文件;如果存在并且不是只读的,则将改写其内容。
//File.CreateText(string Path ) //创建或打开一个文件用于写入UTF-8编码的文本 ,此方法的返回类型是StreamWriter.如果由 path 指定的文件不存在,则创建该文件。如果该文件确实存在,则改写其内容。允许其他线程在文件打开后读取该文件。
//File.CreateText方法,相当于调用StreamWriter类的构造函数,所以指定路径的文件可以存在也可以没有存在
//File.AppendAllText(string Path ,string contents);//将指定的字符串追加到文件中,如果文件还不存在则创建该文件
//File.AppendAllText(string Path,string contents,Encoding encoding );
//File.Open (string Path ,FileMode mode) //此方法的返回类型是FileStream
//File.Open (string Path ,FileMode mode,FileAccess access)
//File.Copy (这里面有两个参数或三个参数)//实现文件复制
//File.Encrypt(string Path)//将某个文件加密,使得只有加密该文件的帐户才能将其解密
//File.Decrypt (string Path)//解密由当前帐户用Systen.IO.File.Encrtpt(System.String)方法加密的文件
//File.Delete (string Path )//删除指定的文件。如果指定的文件不存在,则不引发异常
//File.Exists (string Path )//确定指定的文件是否存在,返回值为bool
//File.Move(string sourceFileName,string destFileName)//移动文件夹,并可为文件指定新名字
//File.OpenRead(string Path) //打开现有文件以进行读取 此方法的返回类型是FileStream
//File.OpenWrite(string Path)//打开现有文件以进行写入 此方法的返回类型是FileStream
//File.OpenText (string Path)//返回类型为SteamReader,打开现有UTF-8编码文本文件以进行读取,此方法相当于掉用StreamReader的构造函数,所以要求指定路径的文件必须已经存在,否则产生异常
//File.ReadAllText (string Path ) //返回值为string,打开一个文件,使用指定的 编码读取文件的所有行,然后关闭文件
//File.ReadAllText (string Path,Encoding encoding )

}
}
}

本文来自CSDN博客:http://blog.csdn.net/terry001/archive/2007/08/28/1762478.aspx

免责声明:文章转载自《System.IO 应用一》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇头文件重复包含问题oracle之数据限定与排序下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

动态创建Fastreport

动态创建Fastreport 动态创建Fastreport分以下几个步骤: 1.首先清空Fastreport,定义全局变量,并加载数据集frReport.Clear;frReport.DataSets.Add(frxDBDataset1);DataHeight :=28;DataWidth :=80;FirstTop := 50;FirstLeft :=...

hive函数之~字符串函数

1、字符串长度函数:length 语法: length(string A)返回值: int说明:返回字符串A的长度 hive> selectlength('abcedfg') fromtableName; 7 2、字符串反转函数:reverse 语法: reverse(string A)返回值: string说明:返回字符串A的反转结果 h...

024. asp.net中第一次使用GridView (设置鼠标经过时更换背景色)

1. 前端HTML代码 <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Index.aspx.cs" Inherits="Index" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "ht...

使用replaceAll实现字符串替换

使用replaceAll实现字符串替换,具体要求为将字符串“abc123bcd45ef6g7890”中的数字替换成汉字“数字”,如果是连续的数字那么替换为一个汉字“数字”。 在Java api中的String类提供了replaceAll方法,实现将字符串中匹配正则表达式的字符串替换成其它字符串,replaceAll方法的声明如下所示: String rep...

springboot + @ControllerAdvice + @ExceptionHandler 实现全局异常拦截

1.目的:实现全局异常拦截,不用在controller在写try catch,使用代码看起来更加整洁 2.不啰嗦直接上代码 2.1首先我们创建一个异常捕获处理类(@RestControllerAdvice = @ControllerAdvice + @ResponseBody) @RestControllerAdvice@Slf4jpublic cla...

Debian 7 Wheezy 配置: 中文字体美化

参考的是这篇文章 http://blog.csdn.net/qq280948982/article/details/6782603 首先通过sudo apt-get install 直接安装 ttf-wqy-zenhei. 另外我还安装了 xfonts-wqy.这两个字体安装完后, 不需要额外设置, Chrome的中文字体方块乱码问题就自动修复了, Ice...