using System;
using System.Collections;
using System.IO;
using System.Text;
namespace Zhaizj.Framework.IO {
///
/// 封装了文件常用操作方法
///
public class File {
///
/// 读取文件的内容(采用UTF8编码)
///
/// 文件的绝对路径
/// 文件的内容
public static String Read( String absolutePath ) {
return Read( absolutePath, Encoding.UTF8 );
}
///
/// 以某种编码方式,读取文件的内容
///
/// 文件的绝对路径
/// 编码方式
/// 文件的内容
public static String Read( String absolutePath, Encoding encoding ) {
using (StreamReader reader = new StreamReader( absolutePath, encoding )) {
return reader.ReadToEnd();
}
}
///
/// 读取文件各行内容(采用UTF8编码),以数组形式返回
///
/// 文件的绝对路径
/// 文件各行内容
public static String[] ReadAllLines( String absolutePath ) {
return ReadAllLines( absolutePath, Encoding.UTF8 );
}
///
/// 以某种编码方式,读取文件各行内容(采用UTF8编码),以数组形式返回
///
/// 文件的绝对路径
/// 编码方式
/// 文件各行内容
public static String[] ReadAllLines( String absolutePath, Encoding encoding ) {
ArrayList list = new ArrayList();
using (StreamReader reader = new StreamReader( absolutePath, encoding )) {
String str;
while ((str = reader.ReadLine()) != null) {
list.Add( str );
}
}
return (String[])list.ToArray( typeof( String ) );
}
///
/// 将字符串写入某个文件中(采用UTF8编码)
///
/// 文件的绝对路径
/// 需要写入文件的字符串
public static void Write( String absolutePath, String fileContent ) {
Write( absolutePath, fileContent, Encoding.UTF8 );
}
///
/// 将字符串写入某个文件中(需要指定文件编码方式)
///
/// 文件的绝对路径
/// 需要写入文件的字符串
/// 编码方式
public static void Write( String absolutePath, String fileContent, Encoding encoding ) {
using (StreamWriter writer = new StreamWriter( absolutePath, false, encoding )) {
writer.Write( fileContent );
}
}
///
/// 删除文件
///
/// 文件的绝对路径
public static void Delete( String absolutePath ) {
System.IO.File.Delete( absolutePath );
}
///
/// 判断文件是否存在
///
/// 文件的绝对路径
///
public static Boolean Exists( String absolutePath ) {
return System.IO.File.Exists( absolutePath );
}
///
/// 移动文件
///
/// 原来的路径
/// 需要挪到的新路径
public static void Move( String sourceFileName, String destFileName ) {
System.IO.File.Move( sourceFileName, destFileName );
}
///
/// 拷贝文件(如果目标存在,不覆盖)
///
/// 原来的路径
/// 需要挪到的新路径
public static void Copy( String sourceFileName, String destFileName ) {
System.IO.File.Copy( sourceFileName, destFileName, false );
}
///
/// 拷贝文件
///
/// 原来的路径
/// 需要挪到的新路径
/// 如果目标存在,是否覆盖
public static void Copy( String sourceFileName, String destFileName, Boolean overwrite ) {
System.IO.File.Copy( sourceFileName, destFileName, overwrite );
}
///
/// 将内容追加到文件中(采用UTF8编码)
///
/// 文件的绝对路径
/// 需要追加的内容
public static void Append( String absolutePath, String fileContent ) {
Append( absolutePath, fileContent, Encoding.UTF8 );
}
///
/// 将内容追加到文件中
///
/// 文件的绝对路径
/// 需要追加的内容
/// 编码方式
public static void Append( String absolutePath, String fileContent, Encoding encoding ) {
using (StreamWriter writer = new StreamWriter( absolutePath, true, encoding )) {
writer.Write( fileContent );
}
}
//public static void Zip( String sourceFileName ) {
// Zip( sourceFileName, sourceFileName + ".zip" );
//}
//public static void Zip( String sourceFileName, String destFileName ) {
// throw new Exception( "Zip 方法未实现" );
//}
//public static void UnZip( String sourceFileName ) {
// throw new Exception( "UnZip 方法未实现" );
//}
//public static void UnZip( String sourceFileName, String destFilePath ) {
// throw new Exception( "UnZip 方法未实现" );
//}
}
}