以下是我一直在使用的内容.虽然它确实有效,但我的程序在尝试计算一个相当大的文件时会锁定,例如10,000行或更多行.较小的文件立即运行.
是否有更好的或者我应该说更快的方式来计算文本文件中的行?
这是我目前正在使用的:
Dim selectedItems = (From i In ListBox1.SelectedItems).ToArray()
For Each selectedItem In selectedItems
ListBox2.Items.Add(selectedItem)
ListBox1.Items.Remove(selectedItem)
Dim FileQty = selectedItem.ToString
'reads the data file and returns the qty
Dim intLines As Integer = 0
'Dim sr As New IO.StreamReader(OpenFileDialog1.FileName)
Dim sr As New IO.StreamReader(TextBox1_Path.Text + "\" + FileQty)
Do While sr.Peek() >= 0
TextBox1.Text += sr.ReadLine() & ControlChars.CrLf
intLines += 1
Loop
ListBox6.Items.Add(intLines)
Next
Run Code Online (Sandbox Code Playgroud) 如何使用stringbuilder并将其转换为流?
所以我的stringbuilder必须转换为:
StreamReader stream = ????
Run Code Online (Sandbox Code Playgroud)
更新
我尝试使用stringreader,如:
StringReader sr = new StringReader(sb.ToString());
StreamReader stream = new StreamReader(sr);
Run Code Online (Sandbox Code Playgroud)
但那不起作用?
我有以下方法从文件GetData创建一个StreamReader.
private void GetData(string file)
{
string filename = Path.GetFileNameWithoutExtension(file);
XmlDocument xmldoc = new XmlDocument();
using (StreamReader sr = new StreamReader(file))
{
Stream bs = sr.BaseStream;
Stream cl = mainParser.CleanMarkup(bs);
try
{
xmldoc = mainParser.LoadDocument(bs);
}
catch (XmlException ex)
{
// Exceptions are usually caused by non-compliant documents.
// These errors are not critical to the operation of this program.
Console.WriteLine(filename + " " + ex.Message);
}
}
Msdn msdnParser = new Msdn(xmldoc);
ListViewItem lvitem = …Run Code Online (Sandbox Code Playgroud) 首先,让我们看看代码:
//The encoding of utf8.txt is UTF-8
StreamReader reader = new StreamReader(@"C:\\utf8.txt", Encoding.UTF8, true);
while (reader.Peek() > 0)
{
//What is the encoding of lineFromTxtFile?
string lineFromTxtFile = reader.ReadLine();
}
Run Code Online (Sandbox Code Playgroud)
正如乔尔在他的着名文章中所说:
如果你有一个字符串,内存,文件或电子邮件消息,你必须知道它所处的编码,或者你无法解释它或正确地将它显示给用户."
所以这里有我的问题:字符串lineFromTxtFile的编码是什么?UTF-8(因为它来自以UTF-8编码的文本文件)?或UTF-16(因为.NET中的字符串是"Unicode"(UTF-16))?
谢谢.
我想我的标题不是那么清楚.
我会试着解释一下:
我可以使用FileStream编写和读取文件
FileStream fs = new FileStream("C:\\Users\\Public\\text.txt", FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.None);
private void button1_Click(object sender, EventArgs e)
{
fs.Seek(0,0);
StreamReader sr = new StreamReader(fs);
textbox.Text = sr.ReadToEnd();
}
private void button2_Click(object sender, EventArgs e)
{
StreamWriter sw = new StreamWriter(fs);
sw.Write(textbox.Text);
sw.Flush();
}
Run Code Online (Sandbox Code Playgroud)
这种方式其他程序无法使用该文件,但我也无法删除内容.写入它只会添加字符串,它不会替换内容.
或者我可以在没有FileStream的情况下完成:
private void button1_Click(object sender, EventArgs e)
{
StreamReader sr = new StreamReader("C:\\Users\\Public\\text.txt");
textBox1.Text = sr.ReadToEnd();
sr.Close();
}
private void button2_Click(object sender, EventArgs e)
{
StreamWriter sw = new StreamWriter("C:\\Users\\Public\\text.txt", false);
sw.Write(textBox1.Text);
sw.Close();
}
Run Code Online (Sandbox Code Playgroud)
这样,文件的内容被替换,但它没有锁定文件.
但我想要两个.解决办法是什么?
我在使用StreamReader和line != null添加时读取文件时遇到问题textBox1
码:
using(StreamReader reader = new StreamReader("lastupdate.txt"))
{
string line;
while((line = reader.ReadLine()) != null)
{
textBox1.Text = line;
}
reader.Close();
}
Run Code Online (Sandbox Code Playgroud)
它不起作用,我不知道为什么.我尝试使用using StreamReader,我从URL下载文件,我可以在文件夹中看到该文件已下载.该lastupdate.txt是大小1KB.
这是我当前的工作代码MessageBox.如果我删除MessageBox,代码不起作用.它需要某种等待或我不知道:
WebClient client = new WebClient();
client.DownloadFileAsync(new Uri(Settings.Default.patchCheck), "lastupdate.txt"); // ok
if(File.Exists("lastupdate.txt"))
{
MessageBox.Show("Lastupdate.txt exist");
using(StreamReader reader = new StreamReader("lastupdate.txt"))
{
string line;
while((line = reader.ReadLine()) != null)
{
textBox1.Text = line;
MessageBox.Show(line.ToString());
}
reader.Close();
}
File.Delete("lastupdate.txt");
}
Run Code Online (Sandbox Code Playgroud) 我想阅读没有HTML标签和标题的网站文本.我只需要在Web浏览器中显示的文本.
我不需要这样
<html>
<body>
bla bla </td><td>
bla bla
<body>
<html>
Run Code Online (Sandbox Code Playgroud)
我只需要文本"bla bla bla bla".
我已经使用webclient和httpwebrequest方法来获取HTML内容并拆分接收的数据,但这是不可能的,因为如果我更改网站,标签可能会更改.
那么有没有办法只能以网页方式获取网站上显示的文字?
我使用以下代码从 Amazon S3 读取文本文件,并逐行处理它。这段代码可以工作,但问题是速度很慢。
GetObjectRequest getObjRequest = new GetObjectRequest()
.WithBucketName(amazonSettings.BucketName)
.WithKey(_fileKey);
using (AmazonS3 client = AWSClientFactory.CreateAmazonS3Client(
amazonSettings.AccessKey,
amazonSettings.SecretAccessKey))
using (GetObjectResponse getObjRespone = client.GetObject(getObjRequest))
using (Stream amazonStream = getObjRespone.ResponseStream)
{
StreamReader amazonStreamReader = new StreamReader(amazonStream);
tempGsContact = new GSContact();
while ((_fileLine = amazonStreamReader.ReadLine()) != null)
{
if (_fileLine.Equals("END:VCARD"))
{
// Make process 1
}
else if (!_fileLine.Equals(string.Empty))
{
//Make process 2
}
}
}
Run Code Online (Sandbox Code Playgroud)
问题是:我能否得到更充分的方式来降低时间成本?
我有一个C#程序,它正在提取一个大约的.csv文件.42,000行长.文件中的所有数据都存储如下:
Zipcode,City,State
我将所有信息都分成三个不同的列listview.
目前这个数据大约需要30到50秒才能进入我的程序.我的问题是如何才能更好地优化我的代码以缩短时间?
以下是我的代码片段.评论的代码是我之前尝试的代码,但没有成功减少时间,因此我以一种更容易阅读的方式重写了它.
//These are globally declared.
lvZip.Columns.Add("Zipcode", 150, HorizontalAlignment.Left);
lvZip.Columns.Add("City", 150, HorizontalAlignment.Left);
lvZip.Columns.Add("State", 150, HorizontalAlignment.Left);
lvZip.View = View.Details;
lvZip.Items.Clear();
//string dir = System.IO.Path.GetDirectoryName(
// System.Reflection.Assembly.GetExecutingAssembly().Location);
//string path = dir + @"\zip_code_database_edited.csv";
//var open = new StreamReader(File.OpenRead(path));
//foreach (String s in File.ReadAllLines(path))
//{
// Zipinfo = s.Split(',');
// Zipinfo[0] = Zipinfo[0].Trim();
// Zipinfo[1] = Zipinfo[1].Trim();
// Zipinfo[2] = Zipinfo[2].Trim();
// lvItem = new ListViewItem(Zipinfo);
// lvZip.Items.Add(lvItem);
//}
//open.Close();
StreamReader myreader = File.OpenText(path);
aLine = myreader.ReadLine();
while …Run Code Online (Sandbox Code Playgroud) 我有以下代码来读取和处理使用StreamReader的Http get请求的响应:
try
{
Stream stream = await ExecuteRequestAsync(uriBuilder.Uri, HttpMethod.Get).ConfigureAwait(false);
if (stream != null)
{
using (StreamReader sr = new StreamReader(stream))
{
using (JsonTextReader reader = new JsonTextReader(sr))
{
...
}
}
}
}
catch (Exception ReadingStreamException)
{
}
private async Task<stream> ExecuteRequestAsync(Uri uri, HttpMethod method)
{
Stream stream;
using (HttpRequestMessage httpRequestMessage = new HttpRequestMessage(method, uri))
{
try
{
stopwatch.Start();
using (HttpResponseMessage responseMessage = await this.httpClient.SendAsync(httpRequestMessage).ConfigureAwait(false))
{
stream = await responseMessage.Content.ReadAsStreamAsync().ConfigureAwait(false);
}
}
catch(Exception GettingStreamException)
{
// Error checking code …Run Code Online (Sandbox Code Playgroud) streamreader ×10
c# ×9
readline ×2
stream ×2
streamwriter ×2
amazon-s3 ×1
async-await ×1
dispose ×1
filestream ×1
html ×1
io ×1
listview ×1
readfile ×1
text-files ×1
unicode ×1
vb.net ×1
webclient ×1
xmlreader ×1