您的位置:首页 > 编程语言 > C#

c#文件操作

2013-08-08 13:24 162 查看
一 文件操作

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace 读取整理
{
class Program
{
static string desiteFile = "G:\\任务\\整理1\\";//分类存取的文件
// static string desiteFile = "";
static string path2 = "G:\\任务\\整理1\\result.txt";//结果文件
static string path_config = "G:\\任务\\整理\\audioConfig.txt";//映射文件

static System.IO.StreamWriter sw = new System.IO.StreamWriter(path2, false);

static int count = 1;
static int countfile = 0;

public class Map
{
public  int num ;
public  string[] name;// new string[10];
public Map()
{
num = 0;
name = new string[10];
}

};
static Map[] map = new Map[254];
//读入映射文件,为声音文件做对应 做准备
public static void readData()
{
for (int i = 0; i < 254; i++)
map[i] = new Map();

System.IO.FileStream fs = new System.IO.FileStream(path_config, System.IO.FileMode.Open);
System.IO.StreamReader m_streamReader = new System.IO.StreamReader(fs);
m_streamReader.BaseStream.Seek(0, System.IO.SeekOrigin.Begin);
//string arry = "";
string strLine = m_streamReader.ReadLine();
int tmp1 = 1;
int tmp2 = 0;
do {
string[] split = strLine.Split('=');
string a = split[0];
tmp1 = System.Int32.Parse(a);

if (tmp1 == tmp2)
{
map[tmp1].num ++;
}
map[tmp1].name[map[tmp1].num] = split[1];
tmp2 = tmp1;

strLine = m_streamReader.ReadLine();
} while (strLine != null && strLine != "");
m_streamReader.Close();
m_streamReader.Dispose();
fs.Close();
fs.Dispose();
}
//递归 有问题,出现重复复制,重复访问
public static void read(string path)
{

//目录下的文件夹集合
string[] diArr = System.IO.Directory.GetDirectories(path, "*", System.IO.SearchOption.AllDirectories);
//目录下的文件
string[] rootfileArr = System.IO.Directory.GetFiles(path);

//文件夹递归
for (int i = 0; i < diArr.Length; i++)
{
//增加父节点
string fileName = System.IO.Path.GetFileNameWithoutExtension(diArr[i]);
Console.WriteLine(fileName);
//node.Nodes.Add(fileName);
read(diArr[i]);
}
//文件直接添加
foreach (string var in rootfileArr)
{
string str ="";

//标注
string extension = System.IO.Path.GetExtension(var);//扩展名 “.aspx”
if(extension == ".wav")
{
//     Console.WriteLine(var);
if (count % 10000 == 0 || count == 1)
{
desiteFile = "G:\\任务\\整理1\\";
desiteFile += countfile.ToString()+"\\";
countfile++;
}

string fileNameWithoutExtension = System.IO.Path.GetFileNameWithoutExtension(var);// 没有扩展名的文件名 “Default”
int now = System.Int32.Parse(fileNameWithoutExtension);//获取标号
for (int i = 0; i <= map[now].num;i++ )
{
string strr = count.ToString() + "="+ map[now].name[i];
sw.WriteLine(strr);
}

str = desiteFile + count.ToString() + extension;
System.IO.File.Copy(var, str,true);
count++;
//    Console.WriteLine(count);

}

}
}
//非递归,直接获得所有文件名
public static void read_new(string path)
{

//目录下的文件夹集合
string[] diArr = System.IO.Directory.GetDirectories(path, "*", System.IO.SearchOption.AllDirectories);
//目录下的文件

//文件夹递归
for (int i = 0; i < diArr.Length; i++)
{
//增加父节点
string fileName = System.IO.Path.GetFileNameWithoutExtension(diArr[i]);
Console.WriteLine(fileName);
//node.Nodes.Add(fileName);
// read(diArr[i]);
string[] rootfileArr = System.IO.Directory.GetFiles(diArr[i]);
//文件直接添加
foreach (string var in rootfileArr)
{
string str = "";

//标注
string extension = System.IO.Path.GetExtension(var);//扩展名 “.aspx”
if (extension == ".wav")
{
//     Console.WriteLine(var);
if (count % 10000 == 0 || count == 1)
{
desiteFile = "G:\\任务\\整理1\\";
desiteFile += countfile.ToString() + "\\";
countfile++;
}

string fileNameWithoutExtension = System.IO.Path.GetFileNameWithoutExtension(var);// 没有扩展名的文件名 “Default”
int now = System.Int32.Parse(fileNameWithoutExtension);//获取标号

str = desiteFile +"bjx"+ count.ToString("00000000") + extension;
for (int k = 0; k <= map[now].num; k++)
{
string strr = "bjx" + count.ToString("00000000") + extension + "=" + map[now].name[k];
sw.WriteLine(strr);
}

System.IO.File.Copy(var, str, true);
count++;
//    Console.WriteLine(count);

}

}
}

}

static void Main(string[] args)
{
string path = "G:\\任务\\重命名加标注\\";//读取的目录文件
readData();
read_new(path);
sw.Close();//
}
}
}


二 二进制文件操作

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace read
{
class Program
{
static void read_correct(string file1)
{
/*
* 函数:
* 功能:校正文件数据值
* 时间:2013、8、8
* */
//读取文件流
System.IO.FileStream fs1 = new System.IO.FileStream(file1, System.IO.FileMode.Open);
System.IO.BinaryReader m_streamReader = new System.IO.BinaryReader(fs1);

Byte[] head = new Byte[44];
int count = m_streamReader.Read(head,0,44);//读取头部

Int16[] data = new Int16[1000000];
Int16 temp;
int countnum =0;
int sum=0;
fs1.Position = 44;
//循环读取并求和
while (fs1.Length > fs1.Position)
{
temp = m_streamReader.ReadInt16();
sum+=temp;
data[countnum] = temp;
countnum++;
}
//关闭读取文件
m_streamReader.Close();
m_streamReader.Dispose();
fs1.Close();

//打开写入文件
System.IO.FileStream fs2 = new System.IO.FileStream(file1, System.IO.FileMode.Open);
System.IO.BinaryWriter sw = new System.IO.BinaryWriter(fs2);
sw.Write(head);//写入头部
fs2.Position = 44;

int mean = sum / countnum;
//数据校正,每个值减去均值
for(int i=0; i < countnum;i++)
{
data[i] -= (Int16)mean;
sw.Write(data[i]);
}

//关闭文件及流
sw.Close();
fs2.Dispose();

}
static int count  = 0;
/*
* 函数:
* 功能:循环读取各个文件夹,遍历wav文件
* 时间:2013、8、8
* */
public static void read_new(string path1)
{

//目录下的文件夹集合
string[] diArr = System.IO.Directory.GetDirectories(path1, "*", System.IO.SearchOption.AllDirectories);
//目录下的文件

string desiteFile = "G:\\任务\\整理2\\";
//文件夹递归
for (int i = 0; i < diArr.Length; i++)
{
//增加父节点
string fileName = System.IO.Path.GetFileNameWithoutExtension(diArr[i]);
Console.WriteLine(fileName);

string[] rootfileArr = System.IO.Directory.GetFiles(diArr[i]);
//文件直接添加
foreach (string var in rootfileArr)
{

string extension = System.IO.Path.GetExtension(var);//扩展名 “.aspx”
if (extension == ".wav")
{
read_correct(var);
}

}
}

}
static void Main(string[] args)
{
string path1 = "G:\\任务\\整理1";
read_new(path1);
}
}
}


 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: