我正在尝试从日志文件中的每一行文本中删除所有“换行”字符。
以下是我正在使用Stream Reader读取的示例条目:-
<![LOG[Raising event:
[SMS_CodePage(850), SMS_LocaleID(2057)]
instance of SoftDistProgramStartedEvent
{
AdvertisementId = "000216F6";
ClientID = "GUID:B55C2757-CBAE-468E-B54F-46CAF2ECF68F";
CommandLine = "\"C:\\WINNT\\system32\\cscript.exe\" /nologo Shutdown_Desktops_Overnight.vbs";
DateTime = "20130211080211.784000+000";
MachineName = "DWD*****";
PackageName = "0000073C";
ProcessID = 2516;
ProgramName = "Shutdown Desktops Overnight";
SiteCode = "S00";
ThreadID = 3640;
UserContext = "NT AUTHORITY\\SYSTEM";
WorkingDirectory = "C:\\WINNT\\system32\\CCM\\Cache\\0000073C.1.System\\";
};
]LOG]!><time="08:02:11.800+000" date="02-11-2013" component="execmgr" context="" type="1" thread="3640" file="event.cpp:522">
Run Code Online (Sandbox Code Playgroud)
在实际的日志文件中,该文件在文件中显示为一行,而“新行字符”用正方形替换。
我正在使用以下代码读取日志条目:-
using (StreamReader sr = new StreamReader(@"C:\Documents and Settings\riversd\Desktop\Logfile2.log"))
{
string Line;
while ((Line = sr.ReadLine()) != null)
{ …Run Code Online (Sandbox Code Playgroud) 我有c#代码读取文本文件并将其打印出来,如下所示:
StreamReader sr = new StreamReader(File.OpenRead(ofd.FileName));
byte[] buffer = new byte[100]; //is there a way to simply specify the length of this to be the number of bytes in the file?
sr.BaseStream.Read(buffer, 0, buffer.Length);
foreach (byte b in buffer)
{
label1.Text += b.ToString("x") + " ";
}
Run Code Online (Sandbox Code Playgroud)
反正我知道我的文件有多少个字节?
我想知道byte[] buffer事先的长度,以便在Read函数中,我可以简单地传入buffer.length第三个参数.
我的程序确实有效,但我认为它会遇到性能问题,它在循环数组时占用了总CPU使用量的40%.通常我的程序只占用CPU使用率的5%以下,我认为ReDim Preserve正在导致这种情况,因为我正在循环大约100,000多行.这是我的代码.
Dim sArray As String()
Dim fStream As New System.IO.FileStream("messages.txt", IO.FileMode.Open)
Dim sReader As New System.IO.StreamReader(fStream)
Dim Index As Integer = 0
Do While sReader.Peek >= 0
ReDim Preserve sArray(Index)
sArray(Index) = sReader.ReadLine
Index += 1
Loop
fStream.Close()
sReader.Close()
Run Code Online (Sandbox Code Playgroud)
除了ReDim Preserve之外,还有其他方法可以将值放入数组吗?在此先感谢,我现在真的陷入了这个问题.
这是我使用List的更新代码.
Dim sArray As String()
Dim sList As New List(Of String)
Dim fStream As New System.IO.FileStream("messages.txt", IO.FileMode.Open)
Dim sReader As New System.IO.StreamReader(fStream)
Dim Index As Integer = 0
Do While sReader.Peek >= 0
sList.Add(sReader.ReadLine)
Loop
sArray = sList.ToArray …Run Code Online (Sandbox Code Playgroud) 这是我的情况.
我是这样做的:
using (StreamReader srReader = new StreamReader(strInputFile))
{
// loop until EOF
while ((strCurrentLine = srReader.ReadLine()) != null)
{
// "process" strCurrentLine...
// write the "processed" strCurrentLine to a new text file
using (StreamWriter sw = new StreamWriter(strFullOutputPathFileName, false))
{
// write strCurrentLine to the new file
sw.WriteLine("stuff...");
}
}
}
Run Code Online (Sandbox Code Playgroud)
我的经理告诉我,使用using像我在循环中的语句将极大地阻碍性能.原因是因为StreamWriter实例将在循环时创建多次.所以,如果我循环1000次,我会有1000个StreamWriter物体实例,这可能严重阻碍性能.
这是真的?另外,我的方法是否是实现此目的的最佳方法?
我有一个我想读入的文本文件,并将文件中的每一行放入自己的字符串中.所以该文件将有4行:
2017-01-20
05:59:30
+353879833382
971575迈克尔
所以在代码中我需要在文件中读取并拆分每一行并将它们放入一个字符串中,即第一行将等于字符串日期,第二行将等于字符串时间等
码:
public static void ParseTXTFile(string FileName, int CompanyID)
{
try
{
FileInfo file = new FileInfo(FileName);
string Date;
string Time;
string Phone;
string JobNo;
string Name;
using (CsvReader reader = new CsvReader(new StreamReader(FileName), false))
{
while (reader.ReadNextRecord())
{
}
}
}
catch (Exception ex)
{
throw (ex);
}
}
Run Code Online (Sandbox Code Playgroud)
如何读取文件的每一行并将其设置为字符串?
简单的问题因为我太愚蠢了.我正在使用streamreader和writer,它给了我一个例外,即该文件已被另一个进程使用.我知道我必须设置一个.dispose()地方.但我真的不知道在哪里.我太盲目了.
这是我的代码:
protected void btn_Geht_Click(object sender, EventArgs e)
{
string sPath = @"C:\VSTO\Projects\Zeiterfassung\Zeiterfassung\obj\Debug\Zeiten.txt";
cZeile Geht = new cZeile();
using (StreamReader sr = new StreamReader(sPath))
{
Geht = cZeiterfassung.GetZeileObjectFromZeileString(sr.ReadLine(), ";");
Geht.Geht = DateTime.Now.ToString("hh:mm");
Geht.dtGeht = DateTime.Now;
sr.Dispose();
using (StreamWriter sw = new StreamWriter(sPath))
{
File.WriteAllText(sPath, string.Format("{0:yyyyMMdd_hhmm};{1};{2:dd.MM.yyyy};{3:hh:mm};{4:hh:mm}", Geht.ID, Geht.User, Geht.Datum, Geht.Kommt, Geht.Geht));
}
}
Run Code Online (Sandbox Code Playgroud)
我在这里得到错误:
File.WriteAllText(sPath, string.Format("{0:yyyyMMdd_hhmm};{1};{2:dd.MM.yyyy};{3:hh:mm};{4:hh:mm}", Geht.ID, Geht.User, Geht.Datum, Geht.Kommt, Geht.Geht));
进程无法访问该文件,因为它正由另一个进程使用
我对 C# 很陌生,一直在寻找有关使用 Int32.Parse、Int32.TryParse、Convert.Int32 等将字符串转换为整数的解决方案。但是我找不到合适的解决方案来接近我想要的。我在这里找到了一个类似的帖子:运算符 '>=' 不能应用于 'string' 和 'string' 类型的操作数,但仍然找不到正确的解决方案。
这是我正在尝试编码的内容的简要说明。
打开名为“compare.xls”的 Excel 文件,读取文件并写入名为“tline.txt”的文本文件
然后打开文本文件“tline.txt”,逐行读取文本,只写入带有条件语句的名为“mkmatlab.txt”的文本文件。条件语句:如果从该行读取的数字为 25 <= x <= 50,则将其写入“mkmatlab.txt”(仅当它是整数时才写入文件)。
Excel文件中的内容的一个想法:
A1..| B1....... | C1........ | D1........| E1........|
0.1 |10.2000 |53.6000 |52.7894 |24.9608
0.2 |26.8209 |55.0851 |56.4726 |35.8431
0.3 |10.1009 |56.0314 |56.5013 |27.8922
0.4 |17.7008 |60.0054 |59.7650 |37.8018
Run Code Online (Sandbox Code Playgroud)
*符号 .. 和 | 只是在这里充当间距,Excel 文件或文本文件不包含任何这些符号。
说明:第一行包含 A1 到 E1。对于列 A1,包含 0.1 到 0.4,对于列 B1 到 E1,依此类推。
我在线上有评论
if (words1[t] >= 25 && words1[t] <= 50)
Run Code Online (Sandbox Code Playgroud)
我需要帮助的地方。
StreamReader fidin …Run Code Online (Sandbox Code Playgroud) 我有一个大约 1 GB 的非常大的文本文件。
我需要计算单词和字符(非空格字符)的数量。
我写了下面的代码。
string fileName = "abc.txt";
long words = 0;
long characters = 0;
if (File.Exists(fileName))
{
using (StreamReader sr = new StreamReader(fileName))
{
string[] fields = null;
string text = sr.ReadToEnd();
fields = text.Split(' ', StringSplitOptions.RemoveEmptyEntries);
foreach (string str in fields)
{
characters += str.Length;
}
words += fields.LongLength;
}
Console.WriteLine("The word count is {0} and character count is {1}", words, characters);
}
Run Code Online (Sandbox Code Playgroud)
有没有办法使用线程使它更快,有人建议我使用线程以使其更快?
我在我的代码中发现了一个问题,如果单词或字符的数量大于long最大值,该问题就会失败。
我编写这段代码时假设只有英文字符,但也可以有非英文字符。
我特别在寻找与线程相关的建议。
我有这个功能,最多可以获取 10 个项目作为输入列表
public async Task<KeyValuePair<string, bool>[]> PayCallSendSMS(List<SmsRequest> ListSms)
{
List<Task<KeyValuePair<string, bool>>> tasks = new List<Task<KeyValuePair<string, bool>>>();
foreach (SmsRequest sms in ListSms)
{
tasks.Add(Task.Run(() => SendSMS(sms)));
}
var result = await Task.WhenAll(tasks);
return result;
}
Run Code Online (Sandbox Code Playgroud)
在这个函数中,我await要下载一些 JSON,然后反序列化它。
public async Task<KeyValuePair<string, bool>> SendSMS(SmsRequest sms)
{
//some code
using (WebResponse response = webRequest.GetResponse())
{
using (Stream responseStream = response.GetResponseStream())
{
StreamReader rdr = new StreamReader(responseStream, Encoding.UTF8);
string Json = await rdr.ReadToEndAsync();
deserializedJsonDictionary = (Dictionary<string, object>)jsonSerializer.DeserializeObject(Json);
}
}
//some code
return …Run Code Online (Sandbox Code Playgroud) 我的代码是:
StreamReader r = new StreamReader("temp.txt");
string str = r.ReadLine();
Console.WriteLine(str);
StreamWriter w = new StreamWriter("temp.txt");
w.WriteLine("new text");
str = r.ReadLine();
Console.WriteLine(str);
Run Code Online (Sandbox Code Playgroud)
当我运行它时,它抛出一个未处理的异常并且无法访问该文件。我确定文件的路径是正确的,并且没有写保护。
streamreader ×10
c# ×9
streamwriter ×3
string ×3
arrays ×2
asp.net ×2
text-files ×2
async-await ×1
dispose ×1
filestream ×1
int ×1
json ×1
loops ×1
optimization ×1
split ×1
task ×1
using ×1
vb.net ×1