|
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectcom.trolltech.qt.QSignalEmitter
com.trolltech.qt.QtJambiObject
com.trolltech.qt.core.QObject
com.trolltech.qt.core.QIODevice
com.trolltech.qt.core.QFile
public class QFile
The QFile class provides an interface for reading from and writing to files.
QFile is an I/O device for reading and writing text and binary files and resources. A QFile may be used by itself or, more conveniently, with a QTextStream or QDataStream.
The file name is usually passed in the constructor, but it can be set at any time using setFileName. QFile expects the file separator to be '/' regardless of operating system. The use of other separators (e.g., '\') is not supported.
You can check for a file's existence using exists, and remove a file using remove. (More advanced file system related operations are provided by QFileInfo and QDir.)
The file is opened with open, closed with close, and flushed with flush. Data is usually read and written using QDataStream or QTextStream, but you can also call the QIODevice-inherited functions read, readLine, readAll, write. QFile also inherits getChar(), putChar(), and ungetChar(), which work one character at a time.
The size of the file is returned by size. You can get the current file position using pos, or move to a new file position using seek. If you've reached the end of the file, atEnd returns true.
The following example reads a text file line by line:
QFile file("in.txt"); if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) return; while (!file.atEnd()) { QByteArray line = file.readLine(); process_line(line); }
The QIODevice::Text flag passed to open tells Qt to convert Windows-style line terminators ("\r\n") into C++-style terminators ("\n"). By default, QFile assumes binary, i.e. it doesn't perform any conversion on the bytes stored in the file.
The next example uses QTextStream to read a text file line by line:
QFile file("in.txt"); if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) return; QTextStream in(&file); while (!in.atEnd()) { QString line = in.readLine(); process_line(line); }
QTextStream takes care of converting the 8-bit data stored on disk into a 16-bit Unicode QString. By default, it assumes that the user system's local 8-bit encoding is used (e.g., ISO 8859-1 for most of Europe; see QTextCodec::codecForLocale() for details). This can be changed using setCodec().
To write text, we can use operator<<(), which is overloaded to take a QTextStream on the left and various data types (including QString) on the right:
QFile file("out.txt"); if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) return; QTextStream out(&file); out << "The magic number is: " << 49 << "\n";
QDataStream is similar, in that you can use operator<<() to write data and operator>>() to read it back. See the class documentation for details.
When you use QFile, QFileInfo, and QDir to access the file system with Qt, you can use Unicode file names. On Unix, these file names are converted to an 8-bit encoding. If you want to use standard C++ APIs (<cstdio> or <iostream>) or platform-specific APIs to access files instead of QFile, you can use the encodeName and decodeName functions to convert between Unicode file names and 8-bit file names.
On Unix, there are some special system files (e.g. in /proc) for which size will always return 0, yet you may still be able to read more data from such a file; the data is generated in direct response to you calling read. In this case, however, you cannot use atEnd to determine if there is more data to read (since atEnd will return true for a file that claims to have size 0). Instead, you should either call readAll, or call read or readLine repeatedly until no more data can be read. The next example uses QTextStream to read /proc/modules line by line:
QFile file("/proc/modules"); if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) return; QTextStream in(&file); QString line = in.readLine(); while (!line.isNull()) { process_line(line); line = in.readLine(); }
Unlike other QIODevice implementations, such as QTcpSocket, QFile does not emit the aboutToClose, bytesWritten, or readyRead signals. This implementation detail means that QFile is not suitable for reading and writing certain types of files, such as device files on Unix platforms.
Nested Class Summary | |
---|---|
static class |
QFile.FileError
This enum describes the errors that may be returned by the error function. |
static class |
QFile.Permission
This enum is used by the permission() function to report the permissions and ownership of a file. |
static class |
QFile.Permissions
This QFlag class provides flags for the int enum. |
Nested classes/interfaces inherited from class com.trolltech.qt.core.QIODevice |
---|
QIODevice.OpenMode, QIODevice.OpenModeFlag |
Nested classes/interfaces inherited from class com.trolltech.qt.QSignalEmitter |
---|
QSignalEmitter.Signal0, QSignalEmitter.Signal1<A>, QSignalEmitter.Signal2<A,B>, QSignalEmitter.Signal3<A,B,C>, QSignalEmitter.Signal4<A,B,C,D>, QSignalEmitter.Signal5<A,B,C,D,E>, QSignalEmitter.Signal6<A,B,C,D,E,F>, QSignalEmitter.Signal7<A,B,C,D,E,F,G>, QSignalEmitter.Signal8<A,B,C,D,E,F,G,H>, QSignalEmitter.Signal9<A,B,C,D,E,F,G,H,I> |
Field Summary |
---|
Fields inherited from class com.trolltech.qt.core.QIODevice |
---|
aboutToClose, bytesWritten, readyRead |
Constructor Summary | |
---|---|
QFile()
|
|
QFile(QObject parent)
Constructs a new file object with the given parent. |
|
QFile(java.lang.String name)
Constructs a new file object to represent the file with the given name. |
|
QFile(java.lang.String name,
QObject parent)
Constructs a new file object with the given parent to represent the file with the specified name. |
Method Summary | |
---|---|
boolean |
atEnd()
Returns true if the end of the file has been reached; otherwise returns false. |
void |
close()
First emits aboutToClose, then closes the device and sets its OpenMode to NotOpen. |
boolean |
copy(java.lang.String newName)
Copies the file currently specified by fileName to a file called newName. |
static boolean |
copy(java.lang.String fileName,
java.lang.String newName)
Copies the file fileName to newName. |
static java.lang.String |
decodeName(QByteArray localFileName)
This does the reverse of QFile::encodeName() using localFileName. |
static java.lang.String |
decodeName(java.lang.String localFileName)
Returns the Unicode version of the given localFileName. |
static QByteArray |
encodeName(java.lang.String fileName)
By default, this function converts fileName to the local 8-bit encoding determined by the user's locale. |
QFile.FileError |
error()
Returns the file error status. |
boolean |
exists()
Returns true if the file specified by fileName exists; otherwise returns false. |
static boolean |
exists(java.lang.String fileName)
Returns true if the file specified by fileName exists; otherwise returns false. |
QAbstractFileEngine |
fileEngine()
|
java.lang.String |
fileName()
Returns the name set by setFileName or to the QFile constructors. |
boolean |
flush()
Flushes any buffered data to the file. |
static QFile |
fromNativePointer(QNativePointer nativePointer)
This function returns the QFile instance pointed to by nativePointer |
int |
handle()
Returns the file handle of the file. |
boolean |
isSequential()
Returns true if the file can only be manipulated sequentially; otherwise returns false. |
boolean |
link(java.lang.String newName)
Creates a link named newName that points to the file currently specified by fileName. |
static boolean |
link(java.lang.String oldname,
java.lang.String newName)
Creates a link named newName that points to the file oldname. |
boolean |
open(int fd,
QIODevice.OpenMode flags)
Opens the existing file descripter fd in the given flags. |
boolean |
open(int fd,
QIODevice.OpenModeFlag... flags)
Opens the existing file descripter fd in the given flags. |
boolean |
open(QIODevice.OpenMode flags)
Opens the file using OpenMode flags, returning true if successful; otherwise false. |
QFile.Permissions |
permissions()
Returns the complete OR-ed together combination of QFile::Permission for the file. |
static QFile.Permissions |
permissions(java.lang.String filename)
Returns the complete OR-ed together combination of QFile::Permission for filename. |
long |
pos()
For random-access devices, this function returns the position that data is written to or read from. |
protected int |
readData(byte[] data)
Equivalent to readData(data, ). |
protected int |
readLineData(byte[] data)
Equivalent to readLineData(data, ). |
boolean |
remove()
Removes the file specified by fileName. |
static boolean |
remove(java.lang.String fileName)
Removes the file specified by the fileName given. |
boolean |
rename(java.lang.String newName)
Renames the file currently specified by fileName to newName. |
static boolean |
rename(java.lang.String oldName,
java.lang.String newName)
Renames the file oldName to newName. |
boolean |
resize(long sz)
Sets the file size (in bytes) sz. |
static boolean |
resize(java.lang.String filename,
long sz)
Sets filename to size (in bytes) sz. |
boolean |
seek(long offset)
For random-access devices, this function sets the current position to offset, returning true on success, or false if an error occurred. |
void |
setFileName(java.lang.String name)
Sets the name of the file. |
boolean |
setPermissions(QFile.Permission... permissionSpec)
Sets the permissions for the file to the permissionSpec specified. |
boolean |
setPermissions(QFile.Permissions permissionSpec)
Sets the permissions for the file to the permissionSpec specified. |
static boolean |
setPermissions(java.lang.String filename,
QFile.Permission... permissionSpec)
Sets the permissions for filename file to permissionSpec. |
static boolean |
setPermissions(java.lang.String filename,
QFile.Permissions permissionSpec)
Sets the permissions for filename file to permissionSpec. |
long |
size()
Returns the size of the file. |
java.lang.String |
symLinkTarget()
Returns the absolute path of the file or directory a symlink (or shortcut on Windows) points to, or a an empty string if the object isn't a symbolic link. |
static java.lang.String |
symLinkTarget(java.lang.String fileName)
Returns the absolute path of the file or directory referred to by the symlink (or shortcut on Windows) specified by fileName, or returns an empty string if the fileName does not correspond to a symbolic link. |
void |
unsetError()
Sets the file's error to QFile::NoError. |
protected int |
writeData(byte[] data)
Equivalent to writeData(data, ). |
Methods inherited from class com.trolltech.qt.core.QIODevice |
---|
bytesAvailable, bytesToWrite, canReadLine, errorString, getByte, isOpen, isReadable, isTextModeEnabled, isWritable, open, openMode, peek, peek, putByte, read, read, readAll, readLine, readLine, readLine, reset, setErrorString, setOpenMode, setOpenMode, setTextModeEnabled, ungetByte, waitForBytesWritten, waitForReadyRead, write, write |
Methods inherited from class com.trolltech.qt.core.QObject |
---|
blockSignals, childEvent, children, connectSlotsByName, customEvent, disposeLater, dumpObjectInfo, dumpObjectTree, dynamicPropertyNames, event, eventFilter, findChild, findChild, findChild, findChildren, findChildren, findChildren, findChildren, installEventFilter, isWidgetType, killTimer, moveToThread, objectName, parent, property, removeEventFilter, setObjectName, setParent, setProperty, signalsBlocked, startTimer, thread, timerEvent |
Methods inherited from class com.trolltech.qt.QtJambiObject |
---|
dispose, disposed, finalize, reassignNativeResources, tr, tr, tr |
Methods inherited from class com.trolltech.qt.QSignalEmitter |
---|
disconnect, disconnect, signalSender |
Methods inherited from class java.lang.Object |
---|
clone, equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Methods inherited from interface com.trolltech.qt.QtJambiInterface |
---|
disableGarbageCollection, nativeId, nativePointer, reenableGarbageCollection, setJavaOwnership |
Constructor Detail |
---|
public QFile(java.lang.String name)
Constructs a new file object to represent the file with the given name.
public QFile(java.lang.String name, QObject parent)
Constructs a new file object with the given parent to represent the file with the specified name.
public QFile()
public QFile(QObject parent)
Constructs a new file object with the given parent.
Method Detail |
---|
public final boolean copy(java.lang.String newName)
Copies the file currently specified by fileName to a file called newName. Returns true if successful; otherwise returns false.
Note that if a file with the name newName already exists, copy returns false (i.e. QFile will not overwrite it).
The source file is closed before it is copied.
public final QFile.FileError error()
Returns the file error status.
The I/O device status returns an error code. For example, if open returns false, or a read/write operation returns -1, this function can be called to find out the reason why the operation failed.
public final boolean exists()
Returns true if the file specified by fileName exists; otherwise returns false.
public final java.lang.String fileName()
Returns the name set by setFileName or to the QFile constructors.
public final boolean flush()
Flushes any buffered data to the file. Returns true if successful; otherwise returns false.
public final int handle()
Returns the file handle of the file.
This is a small positive integer, suitable for use with C library functions such as fdopen() and fcntl(). On systems that use file descriptors for sockets (i.e. Unix systems, but not Windows) the handle can be used with QSocketNotifier as well.
If the file is not open, or there is an error, handle returns -1.
public final boolean link(java.lang.String newName)
Creates a link named newName that points to the file currently specified by fileName. What a link is depends on the underlying filesystem (be it a shortcut on Windows or a symbolic link on Unix). Returns true if successful; otherwise returns false.
This function will not overwrite an already existing entity in the file system; in this case, link() will return false and set error() to return RenameError.
Note: To create a valid link on Windows, newName must have a .lnk file extension.
public final boolean open(int fd, QIODevice.OpenModeFlag... flags)
Opens the existing file descripter fd in the given flags. Returns true if successful; otherwise returns false.
When a QFile is opened using this function, close does not actually close the file.
The QFile that is opened using this function is automatically set to be in raw mode; this means that the file input/output functions are slow. If you run into performance issues, you should try to use one of the other open functions.
Warning: If fd is 0 (stdin), 1 (stdout), or 2 (stderr), you may not be able to seek. size is set to LLONG_MAX (in <climits>).
public final boolean open(int fd, QIODevice.OpenMode flags)
Opens the existing file descripter fd in the given flags. Returns true if successful; otherwise returns false.
When a QFile is opened using this function, close does not actually close the file.
The QFile that is opened using this function is automatically set to be in raw mode; this means that the file input/output functions are slow. If you run into performance issues, you should try to use one of the other open functions.
Warning: If fd is 0 (stdin), 1 (stdout), or 2 (stderr), you may not be able to seek. size is set to LLONG_MAX (in <climits>).
public final QFile.Permissions permissions()
Returns the complete OR-ed together combination of QFile::Permission for the file.
public final boolean remove()
Removes the file specified by fileName. Returns true if successful; otherwise returns false.
The file is closed before it is removed.
public final boolean rename(java.lang.String newName)
Renames the file currently specified by fileName to newName. Returns true if successful; otherwise returns false.
If a file with the name newName already exists, rename returns false (i.e., QFile will not overwrite it).
The file is closed before it is renamed.
public final boolean resize(long sz)
Sets the file size (in bytes) sz. Returns true if the file if the resize succeeds; false otherwise. If sz is larger than the file currently is the new bytes will be set to 0, if sz is smaller the file is simply truncated.
public final void setFileName(java.lang.String name)
Sets the name of the file. The name can have no path, a relative path, or an absolute path.
Do not call this function if the file has already been opened.
If the file name has no path or a relative path, the path used will be the application's current directory path at the time of the open call.
Example:
QFile file;
QDir::setCurrent("/tmp");
file.setFileName("readme.txt");
QDir::setCurrent("/home");
file.open(QIODevice::ReadOnly); // opens "/home/readme.txt" under Unix
Note that the directory separator "/" works for all operating systems supported by Qt.
public final boolean setPermissions(QFile.Permission... permissionSpec)
Sets the permissions for the file to the permissionSpec specified. Returns true if successful, or false if the permissions cannot be modified.
public final boolean setPermissions(QFile.Permissions permissionSpec)
Sets the permissions for the file to the permissionSpec specified. Returns true if successful, or false if the permissions cannot be modified.
public final java.lang.String symLinkTarget()
Returns the absolute path of the file or directory a symlink (or shortcut on Windows) points to, or a an empty string if the object isn't a symbolic link.
This name may not represent an existing file; it is only a string. QFile::exists() returns true if the symlink points to an existing file.
public final void unsetError()
Sets the file's error to QFile::NoError.
public boolean atEnd()
Returns true if the end of the file has been reached; otherwise returns false.
For regular empty files on Unix (e.g. those in /proc), this function returns true, since the file system reports that the size of such a file is 0. Therefore, you should not depend on atEnd when reading data from such a file, but rather call read until no more data can be read.
atEnd
in class QIODevice
public void close()
First emits aboutToClose, then closes the device and sets its OpenMode to NotOpen. The error string is also reset.
close
in class QIODevice
public QAbstractFileEngine fileEngine()
public boolean isSequential()
Returns true if the file can only be manipulated sequentially; otherwise returns false.
Most files support random-access, but some special files may not.
isSequential
in class QIODevice
public boolean open(QIODevice.OpenMode flags)
Opens the file using OpenMode flags, returning true if successful; otherwise false.
The flags must be QIODevice::ReadOnly, QIODevice::WriteOnly, or QIODevice::ReadWrite. It may also have additional flags, such as QIODevice::Text and QIODevice::Unbuffered.
Note: In WriteOnly or ReadWrite mode, if the relevant file does not already exist, this function will try to create a new file before opening it.
Note: Because of limitations in the native API, QFile ignores the Unbuffered flag on Windows.
open
in class QIODevice
public long pos()
For random-access devices, this function returns the position that data is written to or read from. For sequential devices or closed devices, where there is no concept of a "current position", 0 is returned.
The current read/write position of the device is maintained internally by QIODevice, so reimplementing this function is not necessary. When subclassing QIODevice, use QIODevice::seek() to notify QIODevice about changes in the device position.
pos
in class QIODevice
protected int readData(byte[] data)
Equivalent to readData(data, ).
readData
in class QIODevice
protected int readLineData(byte[] data)
Equivalent to readLineData(data, ).
readLineData
in class QIODevice
public boolean seek(long offset)
For random-access devices, this function sets the current position to offset, returning true on success, or false if an error occurred. For sequential devices, the default behavior is to do nothing and return false.
When subclassing QIODevice, you must call QIODevice::seek() at the start of your function to ensure integrity with QIODevice's built-in buffer. The base implementation always returns true.
seek
in class QIODevice
public long size()
Returns the size of the file.
For regular empty files on Unix (e.g. those in /proc), this function returns 0; the contents of such a file are generated on demand in response to you calling read.
size
in class QIODevice
protected int writeData(byte[] data)
Equivalent to writeData(data, ).
writeData
in class QIODevice
public static boolean copy(java.lang.String fileName, java.lang.String newName)
Copies the file fileName to newName. Returns true if successful; otherwise returns false.
If a file with the name newName already exists, copy returns false (i.e., QFile will not overwrite it).
public static java.lang.String decodeName(QByteArray localFileName)
This does the reverse of QFile::encodeName() using localFileName.
public static QByteArray encodeName(java.lang.String fileName)
By default, this function converts fileName to the local 8-bit encoding determined by the user's locale. This is sufficient for file names that the user chooses. File names hard-coded into the application should only use 7-bit ASCII filename characters.
public static boolean exists(java.lang.String fileName)
Returns true if the file specified by fileName exists; otherwise returns false.
public static boolean link(java.lang.String oldname, java.lang.String newName)
Creates a link named newName that points to the file oldname. What a link is depends on the underlying filesystem (be it a shortcut on Windows or a symbolic link on Unix). Returns true if successful; otherwise returns false.
public static QFile.Permissions permissions(java.lang.String filename)
Returns the complete OR-ed together combination of QFile::Permission for filename.
public static boolean remove(java.lang.String fileName)
Removes the file specified by the fileName given.
Returns true if successful; otherwise returns false.
public static boolean rename(java.lang.String oldName, java.lang.String newName)
Renames the file oldName to newName. Returns true if successful; otherwise returns false.
If a file with the name newName already exists, rename returns false (i.e., QFile will not overwrite it).
public static boolean resize(java.lang.String filename, long sz)
Sets filename to size (in bytes) sz. Returns true if the file if the resize succeeds; false otherwise. If sz is larger than filename currently is the new bytes will be set to 0, if sz is smaller the file is simply truncated.
public static boolean setPermissions(java.lang.String filename, QFile.Permission... permissionSpec)
Sets the permissions for filename file to permissionSpec.
public static boolean setPermissions(java.lang.String filename, QFile.Permissions permissionSpec)
Sets the permissions for filename file to permissionSpec.
public static java.lang.String symLinkTarget(java.lang.String fileName)
Returns the absolute path of the file or directory referred to by the symlink (or shortcut on Windows) specified by fileName, or returns an empty string if the fileName does not correspond to a symbolic link.
This name may not represent an existing file; it is only a string. QFile::exists() returns true if the symlink points to an existing file.
public static QFile fromNativePointer(QNativePointer nativePointer)
nativePointer
- the QNativePointer of which object should be returned.public static java.lang.String decodeName(java.lang.String localFileName)
|
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |