You are not logged in.

Dear visitor, welcome to Jabaco - Community. If this is your first visit here, please read the Help. It explains in detail how this page works. To use all features of this page, you should consider registering. Please use the registration form, to register here or read more information about the registration process. If you are already registered, please login here.

A1880

Intermediate

  • "A1880" is male
  • "A1880" started this thread

Posts: 500

Date of registration: Jan 1st 2009

Location: Hanover, Germany

Occupation: Software Engineer

Hobbies: Hilbert Curves

  • Send private message

1

Sunday, September 6th 2009, 4:06pm

error writing text to VBFileHandler file

Hi,
I found a problem writing to a file:

Jabaco Source

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
Public Sub Command1_Click()
   Dim fileName As String = System.getenv("tmp") & "\myFile.txt" 
   Dim f As VBFileHandler = Open(fileName, Output, Write, Shared)
   Dim i As Integer 
   Dim cnt As Integer = 0
   Dim res As Boolean 
   
   For i = 1 To 100
      res = f.writeString(cnt & ": neque porro quisquam est, qui dolorem ipsum quia dolor sit amet, " & _
                          "consectetur, adipisci velit, sed quia non numquam eius")
      cnt = cnt + 1
      
      If Not res Then
         Debug.Print "premature exit at line " & cnt
      End If
   Next i
   
   f.close 
   
   Debug.Print cnt & "Lines written to " & fileName
   
End Sub


The file ends up with just one line of text.
Only the last line is written properly.
The first 99 lines are ignored/skipped.
The writeString always returned a true status.

The source code in the Jabaco framework:

Jabaco Source

1
2
3
4
5
6
Public Function writeString(s As String) As Boolean     
    If myOutput = Nothing Then Exit Function    
    If myMode <> Append Then myOutput.getChannel.truncate 0    
    myOutput.write s    
    writeString = True 
End Function


My impression is that "truncate 0" discards all but the last line of text.

The workaround for the time being is something like this:

Source code

1
2
3
4
5
6
7
Private myWriter As java#io#PrintWriter

myWriter = New java#io#PrintWriter(New java#io#BufferedWriter(New java#io#FileWriter(f)))

myWriter.println cnt & ": neque porro quisquam est, ..."

myWriter.close


Anny comments?

Greetings!

A1880

A1880

Intermediate

  • "A1880" is male
  • "A1880" started this thread

Posts: 500

Date of registration: Jan 1st 2009

Location: Hanover, Germany

Occupation: Software Engineer

Hobbies: Hilbert Curves

  • Send private message

2

Tuesday, February 2nd 2010, 11:39am

I guess the implementation of "FileSystem" in the Jabaco framework is not complete yet.

As a workaround, you can use Java mechanisms directly.

Sample:

Jabaco Source

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
Option Explicit

Public Sub Command1_Click()
   Dim lnr As LineNumberReader 
   Dim s As String
   
   On Error Goto ErrHandler

   lnr = openReader("w:\some not existing path\foo.xyz")
   
   s = lnr.readLine()
   
   MsgBox s
   
   Exit Sub
   
ErrHandler:
   MsgBox "click handler failed"
End Sub



Public Function openReader(fileName As String) As java#io#LineNumberReader 
   Dim f As LineNumberReader = Null

   On Error Goto ErrHandler
   
  	f = New LineNumberReader(New java#io#InputStreamReader( _
            New java#io#FileInputStream(fileName), _
            java#nio#charset#Charset.forName("ISO8859-1") ))

   Exit Function
   
ErrHandler:
   MsgBox "Could not open file '" & fileName & "'" _
     & vbCrLf & "(" & Err.getMessage() & "'"
End Function


Volunteers for testing, fixing and improving the open source Jabaco framework are welcome!

Greetings

A1880

Rate this thread
WoltLab Burning Board