473,624 Members | 2,496 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

About FileStream and StreamWriter!?

About FileStream and StreamWriter!?

I have a string1 which contains(of course in unicode) a html code with
Unicode charset specified in it.

1)

byte[]content= System.Text.Uni codeEncoding.Un icode.GetBytes( string1);

System.IO.FileS tream wr=new System.IO.FileS tream("C:\\myht ml.html"
,System.IO.File Mode.CreateNew) ;

wr.Write(conten t,0,content.Len gth);

wr.Close();

When I opened the file some characters are unreadable, so something is wrong
in my code above, but then I have done:

2)

byte[]content= System.Text.Uni codeEncoding.Un icode.GetBytes( string1);

//System.IO.FileS tream wr=new //System.IO.FileS tream("C:\\myht ml.html"
,System.IO.File Mode.CreateNew) ;

//wr.Write(conten t,0,content.Len gth);

//wr.Close();

System.IO.Strea mWriter tr=new System.IO.Strea mWriter("C:\\my html.html"
,,false,System. Text.Encoding.U nicode);

string teststr=System. Text.Encoding.U nicode.GetStrin g(content);

tr.Write(testst r);//(I did not simply used string1 in it because of reason
of testing)

And file was in good form, why first time wasn't good as second?

Tanks to all!

Jul 19 '05 #1
1 6055
Viorel <vm*********@mo ldova.cc> wrote:
About FileStream and StreamWriter!?

I have a string1 which contains(of course in unicode) a html code with
Unicode charset specified in it.

1)

byte[]content= System.Text.Uni codeEncoding.Un icode.GetBytes( string1);
Note that UnicodeEncoding .Unicode is more canonically known as just
Encoding.Unicod e.
System.IO.FileS tream wr=new System.IO.FileS tream("C:\\myht ml.html"
,System.IO.File Mode.CreateNew) ;

wr.Write(conten t,0,content.Len gth);

wr.Close();

When I opened the file some characters are unreadable, so something is wrong
in my code above
No, that's not necessarily the case. Are you sure the editor you're
opening the file with can copy with the UCS-2 encoding?
but then I have done:

2)

byte[]content= System.Text.Uni codeEncoding.Un icode.GetBytes( string1);

//System.IO.FileS tream wr=new //System.IO.FileS tream("C:\\myht ml.html"
,System.IO.File Mode.CreateNew) ;

//wr.Write(conten t,0,content.Len gth);

//wr.Close();

System.IO.Strea mWriter tr=new System.IO.Strea mWriter("C:\\my html.html"
,,false,System. Text.Encoding.U nicode);

string teststr=System. Text.Encoding.U nicode.GetStrin g(content);

tr.Write(testst r);//(I did not simply used string1 in it because of reason
of testing)

And file was in good form, why first time wasn't good as second?


The first probably didn't emit a byte order mark (BOM) whereas the
second one might have done. I suggest you look at the difference with a
binary file editor. If I'm write, the second file will have the bytes
0xFF 0xFE (or 0xFE 0xFF - I can never remember the order off-hand) at
the start, whereas the first won't.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet/
If replying to the group, please do not mail me too
Jul 19 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

33
4351
by: | last post by:
Hi, When constructing StreamWriter with the following.. FileStream f = new FileStream(..); StreamWriter s = new StreamWriter(f); Then attempt to write out åäö letters they become garbage. BUT
4
28343
by: Dan | last post by:
In the following example, is it necessary to close the FileStream object as well as the StreamWriter object? FileStream fs = new FileStream(fileName, FileMode.CreateNew, FileAccess.Write, FileShare.None); StreamWriter swFromFile = new StreamWriter(logFile); swFromFile.Write(textToAdd); swFromFile.Flush(); swFromFile.Close();
5
8973
by: Patrick Sannes | last post by:
Hi, At this moment I have one large sourcecode file and want to seperate it into multiple files. When I do so, the app crash after he created 6389 (156794880 bytes total) source files with the error: Filestrem will not open Win32 devices such as disk partitions and tape drives. Don't use \ \\\.\\\ in the path. After I put in an exception handler and an breakpoint in it, i discovered that the filename was a correct one. (just like the...
10
4640
by: Oscar Thornell | last post by:
Hi, I generate and temporary saves a text file to disk. Later I upload this file to Microsoft MapPoint (not so important). The file needs to be in UTF-8 encoding and I explicitly use the "Encoding.UTF8" in the constructor like this: StreamWriter writer = new StreamWriter(file, Encoding.UTF8); When I do this the StreamWriter inserts an UTF-8 preamble "" into the
3
8293
by: sbparsons | last post by:
I have a file opened as a FileStream. I have a StreamReader and StreamWriter object opened, referencing the FileStream object. My aim is to read lines from the file until I find the line where there is a unique comment as a place holder. I would then like to use the StreamWriter to insert lines after this comment and save the file. How do I instruct the StreamWriter to find the line position based on the
2
2545
by: Viorel | last post by:
About FileStream and StreamWriter!? I have a string1 which contains(of course in unicode) a html code with Unicode charset specified in it. 1) bytecontent= System.Text.UnicodeEncoding.Unicode.GetBytes(string1);
3
15706
by: Mads Westen | last post by:
Hi, I'm making some changes to my program, because I need to write with Codepage 850. Before I used FileStream, but I found code to use with StreamWriter instead. My problem is, that I don't get any output when writing with StreamWriter, but only with FileStream. I got StreamWriter to work in another class, where I only have 1 Writeline
1
1631
by: iwdu15 | last post by:
hi...just a quick question. what are the differences in using a FileStream and StreamWriter opposed to just a StreamWriter.....for instance Dim fs as New FileStream("C:\Test.txt",...) Dim sw As New StreamWriter(fs) sw.WriteLine("Stuff") sw.Flush() sw.Close()
3
5226
by: stumorgan | last post by:
I'm doing some USB communications in C# and am running into a minor annoyance. I'm using the Windows API CreateFile function to get a SafeFileHandle, which I then stuff into a FileStream and from there into StreamReader and StreamWriter objects. The StreamReader is working beautifully, but the StreamWriter isn't. If I convert the StreamWriter.BaseStream back to a FileStream and use its SafeFileHandle in the Windows API WriteFile...
0
8175
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8680
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8625
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8336
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8482
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7168
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6111
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4177
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1487
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.