这是我的代码:
public static TextWriter twLog = null;
private int fileNo = 1;
private string line = null;
TextReader tr = new StreamReader("file_no.txt");
TextWriter tw = new StreamWriter("file_no.txt");
line = tr.ReadLine();
if(line != null){
fileNo = int.Parse(line);
twLog = new StreamWriter("log_" + line + ".txt");
}else{
twLog = new StreamWriter("log_" + fileNo.toString() + ".txt");
}
System.IO.File.WriteAllText("file_no.txt",string.Empty);
tw.WriteLine((fileNo++).ToString());
tr.Close();
tw.Close();
twLog.Close();
Run Code Online (Sandbox Code Playgroud)
它抛出此错误:
IOException:路径C:\ Users\Water Simulation\file_no.txt上的共享冲突
我要做的只是打开一个带有log_x.txt名称的文件,并从file_no.txt文件中取出"x".如果file_no.txt文件为空,请将日志文件的名称设为log_1.txt并写入"fileNo + 1" to file_no.txt.新程序启动后,新的日志文件名必须是log_2.txt.But我收到此错误,我无法理解我做错了什么.谢谢你的帮助.
我有两个课程,我不能以任何方式改变:
第1类:将a TextWriter
作为构造函数参数并将其用作输出流.
第2类:提供方法WriteLine(string)
.
我需要一个适配器,这样Class1的所有输出都写入Class2.因此,我启动了一个适配器,它扩展TextWriter
并缓冲传入的文本,并在新行到达时将其刷新到class2实例.
但是,TextWriter中有很多方法 - 我应该实现哪些方法?Class1中的输出仅为字符串.
根据MSDN,应该至少覆盖Write(char),但是,这强制我自己完成所有\ r \n新行处理...
Q1:你知道更好的方法来实现我的目标吗?Q2:如果不是,我应该覆盖哪些TextWriter方法以实现最小的实现工作.
我试图在内存中创建一个文本文件,添加一些行,最后将文件保存在文本文件中.我可以处理savedialog部分,但我不知道如何从内存中获取文本文件.任何帮助和提示都将受到关注.
到目前为止我所做的是:
//Initialize in memory text writer
MemoryStream ms = new MemoryStream();
TextWriter tw = new StreamWriter(ms);
tw.WriteLine("HELLO WORLD!");
tw.WriteLine("I WANT TO SAVE THIS FILE AS A .TXT FILE!);
Run Code Online (Sandbox Code Playgroud)
请注意 我将调用tw.WriteLine()在不同的地方添加更多行,所以我想在程序结束时保存它(所以这个主题应该包含在使用{}之类的东西中)
UPDATE
StringBuilder似乎是一个更可靠的选择!当我使用MemoryStream执行此操作时,我的文本文件中出现了奇怪的剪切.
谢谢.
我正在尝试用我的C#程序用Unix风格的换行编写一个文本文件.
由于某种原因,以下代码不起作用:
TextWriter fileTW = ...
fileTW.NewLine = "\n";
fileTW.WriteLine("Hello World!");
Run Code Online (Sandbox Code Playgroud)
这也不是:
TextWriter fileTW = ...
fileTW.Write("Hello World! + \n");
Run Code Online (Sandbox Code Playgroud)
在这两种情况下,'\n'都被'\ r \n'替换,我不想要!我一直用十六进制编辑器验证这一点,它显示每行以0x0D0A结尾.
有任何想法吗?
谢谢!
编辑:
对不起大家,误报!
请允许我解释一下......
我的TextWriter正在写一个MemoryStream,然后使用SharpZLib将其添加到tar存档中.事实证明,通过使用WinZIP提取文本文件,它正在用\ r \n替换\n的每个实例.如果我将相同的tar存档复制到我的Ubuntu机器并在那里提取,只有\n就在那里.奇怪的!
对不起,如果我浪费了任何人的时间!谢谢!
using (TextWriter writer = File.CreateText(path2))
{
writer.Write(SomeText);
}
Run Code Online (Sandbox Code Playgroud)
这是一段有问题的代码.当我写入文件时,没关系,直到其他应用程序打开文件.然后我得到错误.
如何编写可以同时读取的文件?
我正在写文字给TextWriter
.我希望UTF-16字节顺序标记(BOM
)出现在输出中:
public void ProcessRequest(HttpContext context)
{
context.Response.ContentEncoding = new UnicodeEncoding(true, true);
WriteStuffToTextWriter(context.Response.Output);
}
Run Code Online (Sandbox Code Playgroud)
除了输出不包含字节顺序标记:
HTTP/1.1 200 OK
Server: ASP.NET Development Server/10.0.0.0
Date: Thu, 06 Sep 2012 21:09:23 GMT
X-AspNet-Version: 4.0.30319
Content-Disposition: attachment; filename="Transactions_Calendar_20120906.csv"
Cache-Control: private
Content-Type: text/csv; filename="Transactions_Calendar_20120906.csv"; charset=utf-16BE
Content-Length: 95022
Connection: Close
JobName,ShiftName,6////09////2012 12::::00::::00 ??,...
Run Code Online (Sandbox Code Playgroud)
我如何告诉a TextWriter
编写编码标记?
context.Response.ContentEncoding = new UnicodeEncoding(true, true);
Run Code Online (Sandbox Code Playgroud)
byteOrderMark
类型:true表示提供Unicode字节顺序标记; 否则,错误.System.Boolean
我开始做以下事情:
using (TextWriter textWriter = new StreamWriter(filePath, append))
{
foreach (MyClassA myClassA in myClassAs)
{
textWriter.WriteLine(myIO.GetCharArray(myClassA));
if (myClassA.MyClassBs != null)
myClassA.MyClassBs.ToList()
.ForEach(myClassB =>
textWriter.WriteLine(myIO.GetCharArray((myClassB)));
if (myClassA.MyClassCs != null)
myClassA.MyClassCs.ToList()
.ForEach(myClassC =>
textWriter.WriteLine(myIO.GetCharArray(myClassC)));
}
}
Run Code Online (Sandbox Code Playgroud)
这似乎很慢(35,000行约35秒).
然后我尝试按照这里的示例创建一个缓冲区,使用以下代码,但它没有获得任何东西.我仍然看到大约35秒的时间.我是如何实现缓冲区的?
using (TextWriter textWriter = new StreamWriter(filePath, append))
{
char[] newLineChars = Environment.NewLine.ToCharArray();
//Chunk through 10 lines at a time.
int bufferSize = 500 * (RECORD_SIZE + newLineChars.Count());
char[] buffer = new char[bufferSize];
int recordLineSize = RECORD_SIZE + newLineChars.Count();
int bufferIndex = 0; …
Run Code Online (Sandbox Code Playgroud) 我正在编写一个包含自定义记录器的控制台应用程序。记录器需要将发送到Console.Error的所有内容涂成红色。现在,我有了第三方引用,这些引用也写入了Console.Out和Console.Error,因此我必须以一种也能说明它们的方式来执行此操作,因为我没有它们的源代码。我已经设置了记录器,以便任何写入控制台的代码都可以通过调用Console.SetOut&Console.SetError方法使用记录器的TextWriter。它有点工作,但我猜想有某种同步问题?在我的ErrorWriter类中,您可以看到我正在设置控制台的前景色,然后调用base.Write(),但结果与预期的不一样。来自Console.Out的应为灰色的文本显示为红色,并且在中间流时突然变为灰色。它一直在相同的字符位置上将颜色从红色变回灰色,但是我假设base.Write()实际上并不会立即吐出到控制台中。有某种滞后时间/缓冲区。我试过调用base.Flush(),但这会使Console.Out的所有文本变成红色,这甚至更糟。我该如何解决这个问题?
public class Logger
{
private static TextWriter _out;
private static ErrorWriter _error;
public Logger()
{
Initiliaze();
}
public static TextWriter Out
{
get
{
return _out;
}
}
public static TextWriter Error
{
get
{
return _error;
}
}
private static void Initiliaze()
{
if (_out == null)
_out = new StreamWriter(Console.OpenStandardOutput());
if (_error == null)
_error = new ErrorWriter(Console.OpenStandardError());
Console.SetOut(Out);
Console.SetOut(Error);
}
}
public class ErrorWriter : StreamWriter
{
// constructors omitted to save space
public override void …
Run Code Online (Sandbox Code Playgroud) 我编写了一个接受a TextWriter
作为参数的方法(通常是Console.Out,但不一定).
当我调用此方法时,会向TextWriter写入一些进度信息.但是,由于此方法可能会运行很长时间,我想用一些状态信息更新我的UI.
目前,我正在使用StringWriter
但它没有任何事件.所以第一次从StringWriter
方法完成后得到结果.
现在我正在搜索一个继承自TextWriter
并触发TextChanged事件或类似事件的类.我知道这不应该是难以实现的,但我敢打赌CLR团队已经为我做了,我找不到合适的课程.
我正在保存一个外部工具使用的 XML 文件。
遗憾的是,该工具无法理解EF BB BF
文件开头的编码 BOM(字节顺序标记:):
<?xml version="1.0" encoding="UTF-8"?>
...
00000000h: EF BB BF 3C 3F 78 6D 6C 20 76 65 72 73 69 6F 6E <?xml version
00000010h: 3D 22 31 2E 30 22 20 65 6E 63 6F 64 69 6E 67 3D ="1.0" encoding=
00000020h: 22 55 54 46 2D 38 22 3F 3E ... "UTF-8"?>
Run Code Online (Sandbox Code Playgroud)
我的代码:
XmlDocument doc = new XmlDocument();
// Stuff...
using (TextWriter sw = new StreamWriter(file, false, Encoding.UTF8)) { …
Run Code Online (Sandbox Code Playgroud) textwriter ×10
c# ×9
.net ×2
encoding ×1
events ×1
file-io ×1
memorystream ×1
newline ×1
stringwriter ×1
textreader ×1
utf-16 ×1
utf-8 ×1
xml ×1