473,378 Members | 1,609 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

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.UnicodeEncoding.Unicode.GetBytes(strin g1);

System.IO.FileStream wr=new System.IO.FileStream("C:\\myhtml.html"
,System.IO.FileMode.CreateNew);

wr.Write(content,0,content.Length);

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.UnicodeEncoding.Unicode.GetBytes(strin g1);

//System.IO.FileStream wr=new //System.IO.FileStream("C:\\myhtml.html"
,System.IO.FileMode.CreateNew);

//wr.Write(content,0,content.Length);

//wr.Close();

System.IO.StreamWriter tr=new System.IO.StreamWriter("C:\\myhtml.html"
,,false,System.Text.Encoding.Unicode);

string teststr=System.Text.Encoding.Unicode.GetString(con tent);

tr.Write(teststr);//(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 6031
Viorel <vm*********@moldova.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.UnicodeEncoding.Unicode.GetBytes(strin g1);
Note that UnicodeEncoding.Unicode is more canonically known as just
Encoding.Unicode.
System.IO.FileStream wr=new System.IO.FileStream("C:\\myhtml.html"
,System.IO.FileMode.CreateNew);

wr.Write(content,0,content.Length);

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.UnicodeEncoding.Unicode.GetBytes(strin g1);

//System.IO.FileStream wr=new //System.IO.FileStream("C:\\myhtml.html"
,System.IO.FileMode.CreateNew);

//wr.Write(content,0,content.Length);

//wr.Close();

System.IO.StreamWriter tr=new System.IO.StreamWriter("C:\\myhtml.html"
,,false,System.Text.Encoding.Unicode);

string teststr=System.Text.Encoding.Unicode.GetString(con tent);

tr.Write(teststr);//(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.com>
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
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. ...
4
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,...
5
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...
10
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...
3
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...
2
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=...
3
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...
1
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...
3
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...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.