Solved

how to create a unicode file and how to read file

Posted on 2004-09-01
11
229 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
 
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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This video discusses moving either the default database or any database to a new volume.
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

707 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now