Solved

how to create a unicode file and how to read file

Posted on 2004-09-01
11
270 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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

[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

Question has a verified solution.

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

Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

623 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