Solved

how to create a unicode file and how to read file

Posted on 2004-09-01
11
251 Views
Last Modified: 2010-04-23
hi all,

i need some ideas on how to create a unicode file using a vb.net application.
after the creation of the file, the application is able to read the specific file..

i had tried using below 3 ways:

(1)
    'file stream to read the job taken file
    Dim jtFile As New FileStream("\My Documents\Personal\JobTaken.txt", FileMode.Append, FileAccess.Write)

    'writer to write to the job taken file
    Dim jtWriter As New StreamWriter(jtFile)

    Dim jtStr As String
    jtStr = "123,123,123"

   jtWriter.WriteLine(jtStr)

*****read method*****
however this way does not write unicode file,and the app is stil unable to read the file..

           Dim line2 As String 'text for jobTaken.txt
           Dim fileR2 As String = "\My Documents\Personal\JobTaken.txt"
           Dim utf2 As System.Text.UnicodeEncoding = New System.Text.UnicodeEncoding
           Dim jtReader As TextReader
            jtReader = New StreamReader(fileR2, utf2, False)

(2)
    Dim utf As System.Text.UnicodeEncoding = New System.Text.UnicodeEncoding
    Dim fs As New FileStream("\My Documents\Personal\JobTaken.txt", FileMode.Append, FileAccess.Write)
    Dim sw As New StreamWriter(fs, utf)
    Dim adt As New System.IO.BinaryWriter(fs, utf)

Dim jtStr As String
    jtStr = "123,123,123"

adt.Write(jtStr)

use the same read method..

(3)
    Dim fs As New FileStream("\My Documents\Personal\JobTaken.txt", FileMode.Append, FileAccess.Write)
    Dim bin As New BinaryWriter(fs)

Dim jtStr As String
    jtStr = "123,123,123"

 bin.Write(jtStr)

use the same read method..

the above three methods create three diiferent methods of creating files,
but use the same method to read file..
however the app alway give me a System.IO exception..

pls help...
0
Comment
Question by:TPoly
  • 3
  • 3
  • 2
  • +1
11 Comments
 
LVL 18

Expert Comment

by:DotNetLover_Baan
ID: 11960265
0
 
LVL 18

Expert Comment

by:DotNetLover_Baan
ID: 11960271
see my earlier PAQ... here I am merging to files in Binary...
http://www.experts-exchange.com/Programming/Programming_Languages/Dot_Net/VB_DOT_NET/Q_21034883.html
-Baan
0
 

Author Comment

by:TPoly
ID: 11960466
hi Baan,

the msdn i had tried b4 but the reader cant get any content from the file...

for the previous posted question:"Merging audio files with DirectX"

i tried using binary writer and also the binary reader, it give mi a IOExcetion..
the error :

_Error::WinIOError+0x8a
FileStream::.ctor+0xee
FileStream::.ctor+0x16
Form1::Form1_Load+0x1fc
Form::OnLoad+0x15
Form::_SetVisibleNotify+0x1d
Control::set_Visible+0x1f
Application::Run+0x7
Form1::Main+0xd

another information is i am using Pocket PC (xdaII) platform..
0
ScreenConnect 6.0 Free Trial

Want empowering updates? You're in the right place! Discover new features in ScreenConnect 6.0, based on partner feedback, to keep you business operating smoothly and optimally (the way it should be). Explore all of the extras and enhancements for yourself!

 
LVL 10

Expert Comment

by:prakash_prk
ID: 11960979

call the close method after write to a file
jtWriter.close()
call the
jtReader.close() after reading

prakash
0
 

Author Comment

by:TPoly
ID: 11961173
hi prakash,

i did call the close method after writing and reading..
however it still unable to read from the file..
the writing to the file is successful,but i am very curious why the reading is not successful..

0
 
LVL 18

Expert Comment

by:DotNetLover_Baan
ID: 11963075
Hi Lin,
what is the exact "IOExcetion" you are getting ? and where ?
use Try-Cacth to get more descriptive error ..
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 11963440
You can specify Unicode with the reader and the writer:

Dim jtWriter As New StreamWriter(jtFile, System.Text.Encoding.Unicode)
Dim jtReader As New StreamReader(jtFile, System.Text.Encoding.Unicode)
0
 

Author Comment

by:TPoly
ID: 11969947
i had specify unicode with both reader and writer, but it still cant work..


***error****
An unhandled exception of type 'System.ArgumentException' occurred in mscorlib.dll

Additional information: ArgumentException

***********

***error2****
An unhandled exception of type 'System.IO.IOException' occurred in mscorlib.dll
Additional information: IOException
***********

i had tried below codes..

=======start======
Dim s1 As New FileStream("\My Documents\Personal\JobTaken.txt", FileMode.Open,
FileAccess.Read)

        Dim objStreamReader As StreamReader
       'Pass the file path and the file name to the StreamReader constructor.

        objStreamReader = New StreamReader(s1)

        Do While objStreamReader.Peek() >= 0
           Try
                    'Read the text line by line.
                    line2 = objStreamReader.ReadLine()  'Then  do anything with that.
           Catch ex As SqlCeException
                   'Catch the error here
            End Try
        Loop 'Until strInput Is Nothing

       objStreamReader.Close()

=======end======

i not quite sure catch what exception..

s1 = nothing
objStreamReader = nothing
 
i can be sure that the app is able to detect the file..
furthermore,the file had some contents..
thus,i am really curious why the FileStream and StreamReader is getting nothing , as the file is CREATED and READ by the SAME APP with the SAME language...the creation of file is successful(but i not sure if is in unicode) but still cant read..
hope someone can help as i am rushing for a project..

any help will be greatly appreciated!!!
0
 
LVL 10

Accepted Solution

by:
prakash_prk earned 500 total points
ID: 11978936
Try this code Tpoly

-------------------------------------------------
'Reading

        Dim line2 As String 'text for jobTaken.txt
        Dim fileR2 As String = "\My Documents\personal\JobTaken.txt"
        Dim utf2 As System.Text.UnicodeEncoding = New System.Text.UnicodeEncoding

        Dim jtReader As TextReader
        jtReader = New StreamReader(fileR2, utf2, False)

        line2 = jtReader.ReadToEnd
        jtReader.Close()
        MsgBox(line2)
    End Sub

----------------------------------------
        'Writing
        Dim jtFile As New FileStream("\My Documents\personal\JobTaken.txt", FileMode.Append, FileAccess.Write)

        'writer to write to the job taken file
        Dim utf2 As System.Text.UnicodeEncoding = New System.Text.UnicodeEncoding


        Dim jtStr As String
        jtStr = "123,123,123"

        Dim i() As Byte = utf2.GetBytes(jtStr)
        jtFile.Write(i, 0, UBound(i))
        jtFile.Close()
    End Sub
-----------------------------------------------------------

regards
prakash
0

Featured Post

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

803 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question