我正在阅读Socket的InputStream.因为我正在动态解析传入的数据,所以我需要逐个字符地阅读.
有BufferedReader.read()同样的事情InputStream.read()吗?(假设BufferedReader是以InputStream为基础构造的)
在单独读取每个字符时使用InputStream.read()更有效吗?还是有更好的方法吗?
尝试通过StreamReader将800MB文本文件加载到DataTable时,我遇到了OutOfMemory Exceptions.我想知道是否有办法从内存流中批量加载DataTable,即从StreamReader读取文本文件的前10,000行,创建DataTable,使用DataTable执行某些操作,然后将下10,000行加载到StreamReader中等等.
我的谷歌在这里不是很有帮助,但似乎应该有一个简单的方法来做到这一点.最后,我将使用SqlBulkCopy将DataTables写入MS SQL数据库,因此如果有一种比我描述的更简单的方法,我会感谢快速指向正确的方向.
编辑 - 这是我正在运行的代码:
public static DataTable PopulateDataTableFromText(DataTable dt, string txtSource)
{
StreamReader sr = new StreamReader(txtSource);
DataRow dr;
int dtCount = dt.Columns.Count;
string input;
int i = 0;
while ((input = sr.ReadLine()) != null)
{
try
{
string[] stringRows = input.Split(new char[] { '\t' });
dr = dt.NewRow();
for (int a = 0; a < dtCount; a++)
{
string dataType = dt.Columns[a].DataType.ToString();
if (stringRows[a] == "" && (dataType == "System.Int32" || dataType == "System.Int64"))
{
stringRows[a] …Run Code Online (Sandbox Code Playgroud) Facebook图形API将用户的电子邮件地址返回给我
foo\u0040bar.com.
在JSON对象中.我需要将其转换为
foo@bar.com.
.NET中必须有一个内置方法,可以将Unicode字符表达式(\ u1234)更改为实际的unicode符号.
你知道这是什么吗?
注意:我不希望使用JSON.NET或JavaScriptSerializer来解决性能问题.
我认为问题出在我的StreamReader中:
requestUrl = "https://graph.facebook.com/me?access_token=" + accessToken;
request = WebRequest.Create(requestUrl) as HttpWebRequest;
try
{
using (HttpWebResponse response2 = request.GetResponse() as HttpWebResponse)
{
// Get the response stream
reader = new StreamReader(response2.GetResponseStream(),System.Text.Encoding.UTF8);
string json = reader.ReadToEnd();
Run Code Online (Sandbox Code Playgroud)
我为StreamReader尝试了不同的编码,UTF8,UTF7,Unicode,......都没有用.
非常感谢!
感谢LB纠正我.问题不在StreamReader中.
在阅读之前,我需要检查一行是否包含字符串,我想使用while循环来执行此操作
while(reader.ReadLine() != null)
{
array[i] = reader.ReadLine();
}
Run Code Online (Sandbox Code Playgroud)
这显然不起作用,那么我该如何选择呢?
我有一个StreamWriter对象写入日志文件,它初始化如下:
StreamWriter streamWriter = File.AppendText(GetLogFilePath());
streamWriter.AutoFlush = true;
Run Code Online (Sandbox Code Playgroud)
稍后在我的代码中,我需要关闭日志文件,然后读取部分已注销的内容.我有以下代码:
streamWriter.Close();
streamWriter = null;
StreamReader logFileStream = File.OpenText(GetLogFilePath());
Run Code Online (Sandbox Code Playgroud)
抛出异常: 进程无法访问该文件,因为它正由另一个进程使用.
据推测,问题是当我关闭StreamWriter时,日志文件未正确关闭.
有没有更好的方法来关闭StreamWriter以确保它已打开的文件正确关闭?
更新:我已经解决了我的问题.它是由一个无关的问题(一个单独的应用程序正在访问我试图访问的文件)引起的.我接受Dmitry Martovoi的回答,因为我认为这对将来有类似问题的其他人最有用; 但重要的一点是,解决方案确实需要在每次写入日志时打开日志文件,这可能会导致不必要的开销.
看到解决方案的评论 - 文件在错误的地方
我到处寻找答案,但我找不到答案.这对我来说真的很令人沮丧,因为我从来没有从使用任何其他编程语言的文件中读取这么多麻烦.
我正在尝试从文本文件中提取用户名和密码,以获得基本的即时消息程序.我不打算发布所有代码 - 它太长了,而且很可能不相关,因为文本文件是在程序的最开始读取的.
这是我试图读取的文本文件("users.ul")的内容:
admin.password
billy.bob
sally.sal
Run Code Online (Sandbox Code Playgroud)
以下是从文本文件中读取的代码:
users = new Dictionary<string, User>();
System.Console.WriteLine("users.ul exists: " + File.Exists("users.ul"));
// Check the status of users.ul. If it exists, fill the user dictionary with its data.
if (File.Exists("users.ul"))
{
// Usernames are listed first in users.ul, and are followed by a period and then the password associated with that username.
StreamReader reader = new StreamReader("users.ul");
string line;
int count = 0;
while ((line = reader.ReadLine()) != null)
{ …Run Code Online (Sandbox Code Playgroud) 我一直面临着制作一种方法的挑战,该方法将非常大的文本文件读入程序,这些文件的范围可以从2gb到100gb.
到目前为止,这个想法一直是读取方法中的几千行文本.
目前,使用流阅读器设置程序,逐行读取文件并处理在该行上找到的必要数据区域.
using (StreamReader reader = new StreamReader("FileName"))
{
string nextline = reader.ReadLine();
string textline = null;
while (nextline != null)
{
textline = nextline;
Row rw = new Row();
var property = from matchID in xmldata
from matching in matchID.MyProperty
where matchID.ID == textline.Substring(0, 3).TrimEnd()
select matching;
string IDD = textline.Substring(0, 3).TrimEnd();
foreach (var field in property)
{
Field fl = new Field();
fl.Name = field.name;
fl.Data = textline.Substring(field.startByte - 1, field.length).TrimEnd();
fl.Order = order;
fl.Show = true; …Run Code Online (Sandbox Code Playgroud) 我正在尝试搜索他们居住的城镇中存储在文本文件中的客户帐户.有三个客户帐户都有不同的城镇.我认为该程序找到了正确的客户,因为它确实返回数据写入屏幕但不是全部.这些图像更好地解释了它.试图嵌入他们,但没有积分
文本文件内容:

当搜索住在利物浦的顾客时(尽管图像没有显示,但光标开始在DOB下面闪烁5行)

寻找贝尔法斯特客户.请注意,它会选择coreect DOB,但错误的客户编号和Surname.(我把评论中的链接放在一起不会让我发布超过2个链接)
下面是方法的代码::
static void FindTown(CustomerStruct[] CustomerDeats)
{
string City;
bool CityMatch = false;
Console.Clear();
begin:
try
{
Console.WriteLine("Please enter the customers Town ");
City = Console.ReadLine();
}
catch
{
Console.WriteLine("Failed. Please try again.");
goto begin;
}
var pathToTown = @"..\..\..\Files\Customer.txt";
using (StreamReader sr = new StreamReader(pathToTown))
while (!CityMatch)
{
{
RecCount = 0;
CustomerDeats[RecCount].Town = sr.ReadLine();
if (City == CustomerDeats[RecCount].Town)
{
Console.WriteLine("\n\n");
CityMatch = true;
CustomerDeats[RecCount].CustomerNo = sr.ReadLine();
Console.WriteLine(CustomerDeats[RecCount].CustomerNo);
CustomerDeats[RecCount].Surname = sr.ReadLine();
Console.WriteLine(CustomerDeats[RecCount].Surname);
CustomerDeats[RecCount].Forename = sr.ReadLine(); …Run Code Online (Sandbox Code Playgroud) 我正在使用下面的代码根据您在此处看到的逻辑将大文本文件拆分为较小的文件.我在File.WriteAllText行上收到错误,说tempfile不存在.流是一个标题记录,后跟多个报告数据行,后跟报告行的一端,然后重新开始.有谁知道为什么我的临时文件不会在这里创建,我错过了什么?谢谢.
private static void SplitFile()
{
StreamReader sr = new StreamReader($"{_processDir}{_processFile}");
StreamWriter sw = null;
string fileName = string.Empty;
while (!sr.EndOfStream)
{
string line = sr.ReadLine();
if (line.Split('\t')[0] == "FILEIDENTIFIER")
{
//line is a header record
sw = new StreamWriter("{_processDir}tempfile.txt", false);
sw.WriteLine(line);
}
else if (line.Contains("END OF\tREPORT"))
{
//line is end of report
sw.Close();
File.WriteAllText($"{_processDir}{fileName}.txt", File.ReadAllText($"{_processDir}tempfile.txt"));
}
else
{
//line is a report datarow
fileName = line.Split('\t')[0];
sw.WriteLine(line);
}
}
}
Run Code Online (Sandbox Code Playgroud) 我想在.net核心中获取Http Request主体,我使用了以下代码:
using (var reader
= new StreamReader(req.Body, Encoding.UTF8))
{
bodyStr = reader.ReadToEnd();
}
req.Body.Position = 0
Run Code Online (Sandbox Code Playgroud)
但是我得到了这个错误:
System.ObjectDisposedException:无法访问已处置的对象。对象名称:“ FileBufferingReadStream”。
该行的using语句后发生错误
如何在.net核心中获取HttpRequest主体?以及如何解决此错误?
streamreader ×10
c# ×9
.net ×2
streamwriter ×2
asp.net-core ×1
asynchronous ×1
buffering ×1
httpcontext ×1
inputstream ×1
java ×1
sockets ×1
unicode ×1
utf-8 ×1