Difference between revisions of "Documentation/DevGuide/Basic/File IO"

From Apache OpenOffice Wiki
Jump to: navigation, search
(Initial author Sun Microsystems, Inc.)
 
 
(5 intermediate revisions by 3 users not shown)
Line 6: Line 6:
 
|NextPage=Documentation/DevGuide/Basic/Date and Time Functions
 
|NextPage=Documentation/DevGuide/Basic/Date and Time Functions
 
}}
 
}}
{{DISPLAYTITLE:File I/O}}
+
{{Documentation/DevGuideLanguages|Documentation/DevGuide/Basic/{{SUBPAGENAME}}}}
 +
{{DISPLAYTITLE:File I/O}}
 
{{PRODUCTNAME}} 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.
 
{{PRODUCTNAME}} 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 {{PRODUCTNAME}} API expect file I/O specific parameters whose types, for example, <idl>com.sun.star.io.XInputStream</idl> are not compatible to the classic Basic file API.
+
It is not recommended to use this classic Basic file interface in the UNO context, because many interfaces in the {{PRODUCTNAME}} 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 '''{{OOo}} Macros''' shows some examples of file I/O using the API :
 +
* Function ReadDirectories( )
 +
* Sub SaveDataToFile( )
 +
* Function LoadDataFromFile( )
 +
* Function CreateFolder( )
 +
}}
  
 
For programming, the file I/O in {{PRODUCTNAME}} 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:
 
For programming, the file I/O in {{PRODUCTNAME}} 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:
Line 31: Line 40:
  
 
{{PDL1}}
 
{{PDL1}}
[[Category: Basic and Dialogs]]
+
 
 +
[[Category:Documentation/Developer's Guide/Basic and Dialogs]]

Latest revision as of 19:54, 14 July 2018



OpenOffice.org 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 OpenOffice.org 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.


Tip.png The module UCB of Basic library Tools of the container Apache OpenOffice Macros shows some examples of file I/O using the API :
  • Function ReadDirectories( )
  • Sub SaveDataToFile( )
  • Function LoadDataFromFile( )
  • Function CreateFolder( )


For programming, the file I/O in OpenOffice.org 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. The XSimpleFileAccess2 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 com.sun.star.io.XInputStream:closeInput() or com.sun.star.io.XOutputStream: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 the openXXX() 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:

Then the method com.sun.star.io.XTextInputStream:readLine() and com.sun.star.io.XTextInputStream:readString() are used to read text from the input stream/file. The method com.sun.star.io.XTextInputStream:isEOF() is used to check for if the end of the file is reached. The com.sun.star.io.XTextInputStream:setEncoding() sets a text encoding where UTF-8 is the default.

Then the method com.sun.star.io.XTextOutputStream:writeString() is used to write text to the output stream.

Content on this page is licensed under the Public Documentation License (PDL).
Personal tools
In other languages