Solved

how to create a unicode file and how to read file

Posted on 2004-09-01
11
266 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

 
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

752 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