How to create a .txt file with unicode encoding


U

ujjwaltrivedi

Hey guys,
Can anyone tell me how to create a text file with Unicode Encoding. In
am using

FileStream Finalfile = new FileStream("finalfile.txt",
FileMode.Append, FileAccess.Write);

###Question:
Now this creates finalfile.txt with ANSI Encoding ...which is a
default. Either tell me how to change the default or how to create a
new file with ecoding already set to unicode.

###Background:
I am already using the work around by which i create a finalfile.txt
manually and then 'save as...' in unicode Encoding. It doesnt work
though. :(
Basically i need to write English and Japanese characters in the same
file and then use it. If i have a file say finalfile.txt with unicode
encoding and i try to write into it using this code:

FinalStream.WriteLine(EngString + "is written as" + JapaneseString
+"\n");

I am using Encoding for code page 932. And unfortunately i get same
results for any other encoding say 1252 as long as the txt file that i
use as a work-around has unicode Encoding. I dnt understand why it
always takes Japanese characters.

###The Code:

/
****************************************************************************************/
FileStream Engfile = new FileStream("engfile.txt", FileMode.Open,
FileAccess.Read);

FileStream Japanesefile = new FileStream("Japanesefile.txt",
FileMode.Open, FileAccess.Read);
FileStream Finalfile = new FileStream("finalfile.txt",
FileMode.Append, FileAccess.Write);

StreamReader EngStream = new StreamReader(Engfile,Targetencoding);
StreamReader JapaneseStream = new
StreamReader(Japanesefile,Targetencoding);
StreamWriter FinalStream = new StreamWriter(Finalfile,Targetencoding);


string EngString = null;
while ((EngString = EngStream.ReadLine()) != null)
{
string JapaneseString = JapaneseStream.ReadLine();
FinalStream.WriteLine(EngString + "~" + JapaneseString +"\n");
}
/
****************************************************************************************/
TargetEncoding is set as 932 earlier in the application.
 
Ad

Advertisements

C

Christof Nordiek

Hey guys,
Can anyone tell me how to create a text file with Unicode Encoding. In
am using

FileStream Finalfile = new FileStream("finalfile.txt",
FileMode.Append, FileAccess.Write);

###Question:
Now this creates finalfile.txt with ANSI Encoding ...which is a
default. Either tell me how to change the default or how to create a
new file with ecoding already set to unicode.
No, this doesn't create a textfile in any encoding. It simply creates an
empty file with the name "finalfile.txt".
A file for itself doesn't have any encoding. There can be text stored in it
with a certain encoding, but without knowing the encoding used on writing
the file, there is no way of determining the encoding other than opening the
file with a guessed encoding and looking if the content is any readable test
and has no odd characters in it.
###Background:
I am already using the work around by which i create a finalfile.txt
manually and then 'save as...' in unicode Encoding. It doesnt work
though. :(
Basically i need to write English and Japanese characters in the same
file and then use it. If i have a file say finalfile.txt with unicode
encoding and i try to write into it using this code:

FinalStream.WriteLine(EngString + "is written as" + JapaneseString
+"\n");

I am using Encoding for code page 932. And unfortunately i get same
results for any other encoding say 1252 as long as the txt file that i
use as a work-around has unicode Encoding. I dnt understand why it
always takes Japanese characters.

###The Code:

/
****************************************************************************************/
FileStream Engfile = new FileStream("engfile.txt", FileMode.Open,
FileAccess.Read);

FileStream Japanesefile = new FileStream("Japanesefile.txt",
FileMode.Open, FileAccess.Read);
FileStream Finalfile = new FileStream("finalfile.txt",
FileMode.Append, FileAccess.Write);

StreamReader EngStream = new StreamReader(Engfile,Targetencoding);

Here the encoding should be given, with wich the english file was written.
StreamReader JapaneseStream = new
StreamReader(Japanesefile,Targetencoding);

Here the encoding should be given, with wich the japanese file was written.
StreamWriter FinalStream = new StreamWriter(Finalfile,Targetencoding);

Here should the encoding be given, in wich the text should be stored in the
final file. This would be Encoding.Unicode for your case. (btw
Encoding.Unicode is UTF16; Unicode itself is no encoding, but a charactermap
for wich different encoding exist.)

Since you're using the same Encoding for all three files, the content can't
be changed. Only maybe some bytes are swollowed wich aren't valid in that
encoding, (if ther are any.)

I hope this helps you better understand encodings and solve your problem.
Christof
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top