Difference between revisions of "Documentation/DevGuide/Basic/File IO"
Line 8: | Line 8: | ||
{{Documentation/DevGuideLanguages|Documentation/DevGuide/Basic/{{SUBPAGENAME}}}} | {{Documentation/DevGuideLanguages|Documentation/DevGuide/Basic/{{SUBPAGENAME}}}} | ||
{{DISPLAYTITLE:File I/O}} | {{DISPLAYTITLE:File I/O}} | ||
− | {{ | + | {{AOo}} Basic has a complete set of statements and runtime functions to access the operating system's file system that are compatible to Visual Basic. For platform independence, the ability to handle file names in <code>file://</code> URL notation has been added. |
− | It is not recommended to use this classic Basic file interface in the UNO context, because many interfaces in the {{ | + | It is not recommended to use this classic Basic file interface in the UNO context, because many interfaces in the {{AOo}} API expect file I/O specific parameters whose types, for example, <idl>com.sun.star.io.XInputStream</idl> are not compatible with the classic Basic file API. |
− | {{Tip|The module '''UCB''' of Basic library '''Tools''' of the container ''' | + | {{Tip|The module '''UCB''' of Basic library '''Tools''' of the container '''OpenOffice Macros''' shows some examples of file I/O using the API : |
* Function ReadDirectories( ) | * Function ReadDirectories( ) | ||
* Sub SaveDataToFile( ) | * Sub SaveDataToFile( ) | ||
Line 20: | Line 20: | ||
}} | }} | ||
− | For programming, the file I/O in {{ | + | For programming, the file I/O in {{AOo}} API context with the service <idl>com.sun.star.ucb.SimpleFileAccess</idl> should be used. This service supports the interface <idl>com.sun.star.ucb.XSimpleFileAccess2</idl>, including the main interface <idl>com.sun.star.ucb.XSimpleFileAccess</idl> that provides fundamental methods to access the file system. The methods are explained in detail in the corresponding interface documentation. The following list provides an overview about the operations supported by this service: |
* copy, move and remove files and folders (methods <code>copy()</code>, <code>move()</code>, <code>kill()</code>) | * copy, move and remove files and folders (methods <code>copy()</code>, <code>move()</code>, <code>kill()</code>) | ||
* prompt for information about files and folders (methods <code>isFolder()</code>, <code>isReadOnly()</code>, <code>getSize()</code>, <code>getContentType()</code>, <code>getDateTimeModified()</code>, <code>exists()</code>) | * prompt for information about files and folders (methods <code>isFolder()</code>, <code>isReadOnly()</code>, <code>getSize()</code>, <code>getContentType()</code>, <code>getDateTimeModified()</code>, <code>exists()</code>) | ||
− | * open or create files (<code>openFileRead()</code>, <code>openFileWrite()</code>, <code>openFileReadWrite()</code>). These functions return objects that support the corresponding stream interfaces <idl>com.sun.star.io.XInputStream</idl>, <idl>com.sun.star.io.XOutputStream</idl> and <idl>com.sun.star.io.XStream</idl>. These interfaces are used to read and write files. The <code>XSimpleFileAccess2</code> does not have methods of its own for these operations. Additionally, these interfaces are often necessary as parameters to access methods of several other interfaces. The opened files have to be closed by calling the appropriate methods | + | * open or create files (<code>openFileRead()</code>, <code>openFileWrite()</code>, <code>openFileReadWrite()</code>). These functions return objects that support the corresponding stream interfaces <idl>com.sun.star.io.XInputStream</idl>, <idl>com.sun.star.io.XOutputStream</idl> and <idl>com.sun.star.io.XStream</idl>. These interfaces are used to read and write files. The <code>XSimpleFileAccess2</code> does not have methods of its own for these operations. Additionally, these interfaces are often necessary as parameters to access methods of several other interfaces. The opened files have to be closed by calling the appropriate methods <idlm>com.sun.star.io.XInputStream:closeInput</idlm>() or <idlm>com.sun.star.io.XOutputStream:closeOutput</idlm>(). |
* The <code>XSimpleFileAccess2</code> also does not have methods to ask for or set the position within a file stream. This is done by calling methods of the <idl>com.sun.star.io.XSeekable</idl> interface that is supported by the objects returned by the <code>openXXX()</code> methods. | * The <code>XSimpleFileAccess2</code> also does not have methods to ask for or set the position within a file stream. This is done by calling methods of the <idl>com.sun.star.io.XSeekable</idl> interface that is supported by the objects returned by the <code>openXXX()</code> methods. | ||
Line 30: | Line 30: | ||
* The service <idl>com.sun.star.io.TextInputStream</idl> supporting <idl>com.sun.star.io.XTextInputStream</idl> and <idl>com.sun.star.io.XActiveDataSink</idl> | * The service <idl>com.sun.star.io.TextInputStream</idl> supporting <idl>com.sun.star.io.XTextInputStream</idl> and <idl>com.sun.star.io.XActiveDataSink</idl> | ||
− | * The service is initialized by passing an object supporting <code>XInputStream</code> to the | + | * The service is initialized by passing an object supporting <code>XInputStream</code> to the <idlm>com.sun.star.io.XActiveDataSink:setInputStream</idlm>() method, for example, an object returned by <idlm> com.sun.star.ucb.XSimpleFileAccess:openFileRead>/idlm>(). |
− | :Then the method | + | :Then the method <idlm>com.sun.star.io.XTextInputStream:readLine</idlm>() and [<idlm>com.sun.star.io.XTextInputStream:readString</idlm>() are used to read text from the input stream/file. The method <idlm>com.sun.star.io.XTextInputStream:isEOF</idlm>() is used to check for if the end of the file is reached. The <idlm>com.sun.star.io.XTextInputStream:setEncoding</idlm>() sets a text encoding where UTF-8 is the default. |
* The service <idl>com.sun.star.io.TextOutputStream</idl> supporting <idl>com.sun.star.io.XTextOutputStream</idl> and <idl>com.sun.star.io.XActiveDataSource</idl> | * The service <idl>com.sun.star.io.TextOutputStream</idl> supporting <idl>com.sun.star.io.XTextOutputStream</idl> and <idl>com.sun.star.io.XActiveDataSource</idl> | ||
− | * The service is initialized by passing an object supporting <code>XOutputStream</code> to the | + | * The service is initialized by passing an object supporting <code>XOutputStream</code> to the <idlm>com.sun.star.io.XActiveDataSource:setOutputStream</idlm>() method, for example, an object returned by <idlm>com.sun.star.ucb.XSimpleFileAccess:openFileWrite</idlm>(). |
− | Then the method | + | Then the method <idlm>com.sun.star.io.XTextOutputStream:writeString</idlm>() is used to write text to the output stream. |
{{PDL1}} | {{PDL1}} | ||
[[Category:Documentation/Developer's Guide/Basic and Dialogs]] | [[Category:Documentation/Developer's Guide/Basic and Dialogs]] |
Revision as of 20:51, 20 December 2020
Apache OpenOffice Basic has a complete set of statements and runtime functions to access the operating system's file system that are compatible to Visual Basic. For platform independence, the ability to handle file names in file://
URL notation has been added.
It is not recommended to use this classic Basic file interface in the UNO context, because many interfaces in the Apache OpenOffice API expect file I/O specific parameters whose types, for example, com.sun.star.io.XInputStream are not compatible with the classic Basic file API.
For programming, the file I/O in Apache OpenOffice API context with the service com.sun.star.ucb.SimpleFileAccess should be used. This service supports the interface com.sun.star.ucb.XSimpleFileAccess2, including the main interface com.sun.star.ucb.XSimpleFileAccess that provides fundamental methods to access the file system. The methods are explained in detail in the corresponding interface documentation. The following list provides an overview about the operations supported by this service:
- copy, move and remove files and folders (methods
copy()
,move()
,kill()
) - prompt for information about files and folders (methods
isFolder()
,isReadOnly()
,getSize()
,getContentType()
,getDateTimeModified()
,exists()
) - open or create files (
openFileRead()
,openFileWrite()
,openFileReadWrite()
). These functions return objects that support the corresponding stream interfaces com.sun.star.io.XInputStream, com.sun.star.io.XOutputStream and com.sun.star.io.XStream. These interfaces are used to read and write files. TheXSimpleFileAccess2
does not have methods of its own for these operations. Additionally, these interfaces are often necessary as parameters to access methods of several other interfaces. The opened files have to be closed by calling the appropriate methods closeInput() or closeOutput(). - The
XSimpleFileAccess2
also does not have methods to ask for or set the position within a file stream. This is done by calling methods of the com.sun.star.io.XSeekable interface that is supported by the objects returned by theopenXXX()
methods.
Two more services are instantiated at the global service manager that extends the service com.sun.star.ucb.SimpleFileAccess by functionality specific to text files:
- The service com.sun.star.io.TextInputStream supporting com.sun.star.io.XTextInputStream and com.sun.star.io.XActiveDataSink
- The service is initialized by passing an object supporting
XInputStream
to the setInputStream() method, for example, an object returned by readLine" class="external text">openFileRead>/idlm>()/- Then the method readLine() and [readString() are used to read text from the input stream/file. The method isEOF() is used to check for if the end of the file is reached. The setEncoding() sets a text encoding where UTF-8 is the default.
- The service com.sun.star.io.TextOutputStream supporting com.sun.star.io.XTextOutputStream and com.sun.star.io.XActiveDataSource
- The service is initialized by passing an object supporting
XOutputStream
to the setOutputStream() method, for example, an object returned by openFileWrite().
Then the method writeString() is used to write text to the output stream.
Content on this page is licensed under the Public Documentation License (PDL).