txt文档里的内容的格式如下:
标题1
aaaaaaaa
bbbbbbbb
cccccccc标题2
dddddddd
eeeeeeee
ffffffff标题3
gggggggg
hhhhhhhh
。。
用readline可以按行读出
现在文档内容其实是很多有标题的小段落组成,如何把每一段文字标题和内容分别读出存在不同数据库字段里
谢谢
标题1
aaaaaaaa
bbbbbbbb
cccccccc标题2
dddddddd
eeeeeeee
ffffffff标题3
gggggggg
hhhhhhhh
。。
用readline可以按行读出
现在文档内容其实是很多有标题的小段落组成,如何把每一段文字标题和内容分别读出存在不同数据库字段里
谢谢
解决方案 »
- C#读取Excel文件显示到Datagird控件中 ,读取Execl过程有问题,请教
- 请教Windows Phone 7 项目方向
- WPF 画拓扑
- [ComponentArt:Grid]誰使用過ComponentArt公司的grid控件,想知道在這個控件中如何在ClientTemplate中使用Js!
- 关于比较的问题,达人请进
- DataGrid绑定图片!
- vs2005文本框上哪个属性是关于正则表达式的
- 在线等Socket.Receive异常问题
- 求助:PROCESSENTRY32的应用...在线等,谢谢.
- 如何获取IP,GetIp()的代码?
- 请问各位高手
- 請問,我把一個TEXTBOX綁定到一個DATATABLE中,。。。。。。
bool isEnd = true;
while (isEnd)
{
string text = reader.ReadLine();
if (text == "")
{
//isnew
//标题
//e.g
//value="";
}
else
{
//TODO;
//e.g
//value=value+text;
//Array.add(value);
}
if (reader.EndOfStream)
isEnd = false;
}
file.Close();如果按照你说的格式的话,这样应该没什么问题。剩下的就看你怎么做了。
using System.IO;public class TextFromFile
{
private const string FILE_NAME = "MyFile.txt"; public static void Main(String[] args)
{
if (!File.Exists(FILE_NAME))
{
Console.WriteLine("{0} does not exist.", FILE_NAME);
return;
}
using (StreamReader sr = File.OpenText(FILE_NAME))
{
bool isTitle = true;
String input;
while ((input = sr.ReadLine()) != null)
{
if (string.IsNullOrEmpty(input))
{
isTitle = true;
continue;
}
if (isTitle)
{
// WriteTitleToDB(input); // 是标题。
isTitle = false;
}
else
{
// WriteContentToDB(input); // 是内容。
}
}
}
}
}
StreamReader sr = new StreamReader(FILE_NAME,System.Text.Encoding.Default)
StreamReader sr = new StreamReader(FILE_Path,System.Text.Encoding.Default)
string file=sr.ReadLine();然后可以把file可以读出来了
System.Text.Encoding.Default这个是避免读出的是乱码
这样可以按照格式读出中英文
试试看吧
最好是 string file = sr.ReadToEnd();
{
StreamReader sr;
try
{
path = new string[5];
int i = 0;
string sLine = "";
sr = File.OpenText(Path.GetFileName("Path.ini"));
while (sLine != null)
{
sLine = sr.ReadLine();
if (sLine == null)
break;
path[i] = sLine;
i++;
}
}
catch (Exception e)
{
MessageBox.Show(e.Message, "提示", MessageBoxButtons.OK,
MessageBoxIcon.Error);
return path;
}
sr.Close();
return path;
}