Solved

how to create a unicode file and how to read file

Posted on 2004-09-01
11
257 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
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

856 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