Interface IFile
- All Superinterfaces:
IAdaptable
,IEncodedStorage
,IResource
,ISchedulingRule
,IStorage
Files, like folders, may exist in the workspace but not be local; non-local file resources serve as place-holders for files whose content and properties have not yet been fetched from a repository.
Files implement the IAdaptable
interface;
extensions are managed by the platform's adapter manager.
- See Also:
- Restriction:
- This interface is not intended to be implemented by clients.
- Restriction:
- This interface is not intended to be extended by clients.
-
Field Summary
Modifier and TypeFieldDescriptionstatic final int
Deprecated.see getEncoding for detailsstatic final int
Deprecated.see getEncoding for detailsstatic final int
Deprecated.see getEncoding for detailsstatic final int
Deprecated.see getEncoding for detailsstatic final int
Deprecated.see getEncoding for detailsstatic final int
Deprecated.see getEncoding for detailsstatic final int
Deprecated.see getEncoding for detailsFields inherited from interface org.eclipse.core.resources.IResource
ALLOW_MISSING_LOCAL, ALWAYS_DELETE_PROJECT_CONTENT, AVOID_NATURE_CONFIG, BACKGROUND_REFRESH, CHECK_ANCESTORS, DEPTH_INFINITE, DEPTH_ONE, DEPTH_ZERO, DERIVED, FILE, FOLDER, FORCE, HIDDEN, KEEP_HISTORY, NEVER_DELETE_PROJECT_CONTENT, NONE, NULL_STAMP, PROJECT, REPLACE, ROOT, SHALLOW, TEAM_PRIVATE, VIRTUAL
-
Method Summary
Modifier and TypeMethodDescriptionvoid
appendContents
(InputStream source, boolean force, boolean keepHistory, IProgressMonitor monitor) Appends the entire contents of the given stream to this file.void
appendContents
(InputStream source, int updateFlags, IProgressMonitor monitor) Appends the entire contents of the given stream to this file.default void
create
(byte[] content, boolean force, boolean derived, IProgressMonitor monitor) Creates the file with the given content as byte array.default void
create
(byte[] content, int createFlags, IProgressMonitor monitor) Creates the file with the given content as byte array.void
create
(InputStream source, boolean force, IProgressMonitor monitor) Creates a new file resource as a member of this handle's parent resource.void
create
(InputStream source, int updateFlags, IProgressMonitor monitor) Creates a new file resource as a member of this handle's parent resource.void
createLink
(URI location, int updateFlags, IProgressMonitor monitor) Creates a new file resource as a member of this handle's parent resource.void
createLink
(IPath localLocation, int updateFlags, IProgressMonitor monitor) Creates a new file resource as a member of this handle's parent resource.void
delete
(boolean force, boolean keepHistory, IProgressMonitor monitor) Deletes this file from the workspace.Returns the name of a charset to be used when decoding the contents of this file into characters.getCharset
(boolean checkImplicit) Returns the name of a charset to be used when decoding the contents of this file into characters.getCharsetFor
(Reader reader) Returns the name of a charset to be used to encode the given contents when saving to this file.Returns a description for this file's current contents.Returns an open input stream on the contents of this file.getContents
(boolean force) This refinement of the correspondingIStorage
method returns an open input stream on the contents of this file.int
Deprecated.usegetCharset()
insteadReturns the full path of this file.getHistory
(IProgressMonitor monitor) Returns a list of past states of this file known to this workspace.default String
getLineSeparator
(boolean checkParent) Returns line separator appropriate for the given file.getName()
Returns the name of this file.boolean
Returns whether this file is read-only.void
move
(IPath destination, boolean force, boolean keepHistory, IProgressMonitor monitor) Moves this resource to be at the given location.default byte[]
Reads the content in a byte array.default char[]
Reads the content as char array.default byte[]
readNBytes
(int maxBytes) Reads the first bytes of the content in a byte array.default String
Reads the content as String.void
setCharset
(String newCharset) Deprecated.Replaced bysetCharset(String, IProgressMonitor)
which is a workspace operation and reports changes in resource deltas.void
setCharset
(String newCharset, IProgressMonitor monitor) Sets the charset for this file.default void
setContents
(byte[] content, boolean force, boolean keepHistory, IProgressMonitor monitor) Equivalent of callingsetContents(InputStream, boolean, boolean, IProgressMonitor)
withnew ByteArrayInputStream(content)
asInputStream
.default void
setContents
(byte[] content, int updateFlags, IProgressMonitor monitor) Equivalent of callingsetContents(InputStream, int, IProgressMonitor)
withnew ByteArrayInputStream(content)
asInputStream
.void
setContents
(InputStream source, boolean force, boolean keepHistory, IProgressMonitor monitor) Sets the contents of this file to the bytes in the given input stream.void
setContents
(InputStream source, int updateFlags, IProgressMonitor monitor) Sets the contents of this file to the bytes in the given input stream.void
setContents
(IFileState source, boolean force, boolean keepHistory, IProgressMonitor monitor) Sets the contents of this file to the bytes in the given file state.void
setContents
(IFileState source, int updateFlags, IProgressMonitor monitor) Sets the contents of this file to the bytes in the given file state.default void
write
(byte[] content, boolean force, boolean derived, boolean keepHistory, IProgressMonitor monitor) Creates the file and sets the file content.Methods inherited from interface org.eclipse.core.runtime.IAdaptable
getAdapter
Methods inherited from interface org.eclipse.core.resources.IResource
accept, accept, accept, accept, accept, clearHistory, copy, copy, copy, copy, createMarker, createMarker, createProxy, delete, delete, deleteMarkers, equals, exists, findMarker, findMarkers, findMaxProblemSeverity, getFileExtension, getLocalTimeStamp, getLocation, getLocationURI, getMarker, getModificationStamp, getParent, getPathVariableManager, getPersistentProperties, getPersistentProperty, getProject, getProjectRelativePath, getRawLocation, getRawLocationURI, getResourceAttributes, getSessionProperties, getSessionProperty, getType, getWorkspace, isAccessible, isDerived, isDerived, isHidden, isHidden, isLinked, isLinked, isLocal, isPhantom, isSynchronized, isTeamPrivateMember, isTeamPrivateMember, isVirtual, move, move, move, move, refreshLocal, revertModificationStamp, setDerived, setDerived, setHidden, setLocal, setLocalTimeStamp, setPersistentProperty, setReadOnly, setResourceAttributes, setSessionProperty, setTeamPrivateMember, touch
Methods inherited from interface org.eclipse.core.runtime.jobs.ISchedulingRule
contains, isConflicting
-
Field Details
-
ENCODING_UNKNOWN
Deprecated.see getEncoding for detailsCharacter encoding constant (value 0) which identifies files that have an unknown character encoding scheme.- See Also:
-
ENCODING_US_ASCII
Deprecated.see getEncoding for detailsCharacter encoding constant (value 1) which identifies files that are encoded with the US-ASCII character encoding scheme.- See Also:
-
ENCODING_ISO_8859_1
Deprecated.see getEncoding for detailsCharacter encoding constant (value 2) which identifies files that are encoded with the ISO-8859-1 character encoding scheme, also known as ISO-LATIN-1.- See Also:
-
ENCODING_UTF_8
Deprecated.see getEncoding for detailsCharacter encoding constant (value 3) which identifies files that are encoded with the UTF-8 character encoding scheme.- See Also:
-
ENCODING_UTF_16BE
Deprecated.see getEncoding for detailsCharacter encoding constant (value 4) which identifies files that are encoded with the UTF-16BE character encoding scheme.- See Also:
-
ENCODING_UTF_16LE
Deprecated.see getEncoding for detailsCharacter encoding constant (value 5) which identifies files that are encoded with the UTF-16LE character encoding scheme.- See Also:
-
ENCODING_UTF_16
Deprecated.see getEncoding for detailsCharacter encoding constant (value 6) which identifies files that are encoded with the UTF-16 character encoding scheme.- See Also:
-
-
Method Details
-
appendContents
void appendContents(InputStream source, boolean force, boolean keepHistory, IProgressMonitor monitor) throws CoreException Appends the entire contents of the given stream to this file.This is a convenience method, fully equivalent to:
appendContents(source, (keepHistory ? KEEP_HISTORY : IResource.NONE) | (force ? FORCE : IResource.NONE), monitor);
This method changes resources; these changes will be reported in a subsequent resource change event, including an indication that this file's content have been changed.
This method is long-running; progress and cancelation are provided by the given progress monitor.
- Parameters:
source
- an input stream containing the new contents of the fileforce
- a flag controlling how to deal with resources that are not in sync with the local file systemkeepHistory
- a flag indicating whether or not to store the current contents in the local historymonitor
- a progress monitor, ornull
if progress reporting is not desired- Throws:
CoreException
- if this method fails. Reasons include:- This resource does not exist.
- The corresponding location in the local file system is occupied by a directory.
- The workspace is not in sync with the corresponding location
in the local file system and
force
isfalse
. - Resource changes are disallowed during certain types of resource change
event notification. See
IResourceChangeEvent
for more details. - The file modification validator disallowed the change.
OperationCanceledException
- if the operation is canceled. Cancelation can occur even if no progress monitor is provided.- See Also:
-
appendContents
void appendContents(InputStream source, int updateFlags, IProgressMonitor monitor) throws CoreException Appends the entire contents of the given stream to this file. The stream, which must not benull
, will get closed whether this method succeeds or fails.The
FORCE
update flag controls how this method deals with cases where the workspace is not completely in sync with the local file system. IfFORCE
is not specified, the method will only attempt to overwrite a corresponding file in the local file system provided it is in sync with the workspace. This option ensures there is no unintended data loss; it is the recommended setting. However, ifFORCE
is specified, an attempt will be made to write a corresponding file in the local file system, overwriting any existing one if need be. In either case, if this method succeeds, the resource will be marked as being local (even if it wasn't before).If this file is non-local then this method will always fail. The only exception is when
FORCE
is specified and the file exists in the local file system. In this case the file is made local and the given contents are appended.The
KEEP_HISTORY
update flag controls whether or not a copy of current contents of this file should be captured in the workspace's local history (properties are not recorded in the local history). The local history mechanism serves as a safety net to help the user recover from mistakes that might otherwise result in data loss. SpecifyingKEEP_HISTORY
is recommended except in circumstances where past states of the files are of no conceivable interest to the user. Note that local history is maintained with each individual project, and gets discarded when a project is deleted from the workspace. This flag is ignored if the file was not previously local.Update flags other than
FORCE
andKEEP_HISTORY
are ignored.Prior to modifying the contents of this file, the file modification validator (if provided by the VCM plug-in), will be given a chance to perform any last minute preparations. Validation is performed by calling
IFileModificationValidator.validateSave
on this file. If the validation fails, then this operation will fail.This method changes resources; these changes will be reported in a subsequent resource change event, including an indication that this file's content have been changed.
This method is long-running; progress and cancelation are provided by the given progress monitor.
- Parameters:
source
- an input stream containing the new contents of the fileupdateFlags
- bit-wise or of update flag constants (FORCE
andKEEP_HISTORY
)monitor
- a progress monitor, ornull
if progress reporting is not desired- Throws:
CoreException
- if this method fails. Reasons include:- This resource does not exist.
- The corresponding location in the local file system is occupied by a directory.
- The workspace is not in sync with the corresponding location
in the local file system and
FORCE
is not specified. - Resource changes are disallowed during certain types of resource change
event notification. See
IResourceChangeEvent
for more details. - The file modification validator disallowed the change.
OperationCanceledException
- if the operation is canceled. Cancelation can occur even if no progress monitor is provided.- Since:
- 2.0
- See Also:
-
create
Creates a new file resource as a member of this handle's parent resource.This is a convenience method, fully equivalent to:
create(source, (force ? FORCE : IResource.NONE), monitor);
This method changes resources; these changes will be reported in a subsequent resource change event, including an indication that the file has been added to its parent.
This method is long-running; progress and cancellation are provided by the given progress monitor.
- Parameters:
source
- an input stream containing the initial contents of the file, ornull
if the file should be marked as not localforce
- a flag controlling how to deal with resources that are not in sync with the local file systemmonitor
- a progress monitor, ornull
if progress reporting is not desired- Throws:
CoreException
- if this method fails. Reasons include:- This resource already exists in the workspace.
- The parent of this resource does not exist.
- The parent of this resource is a virtual folder.
- The project of this resource is not accessible.
- The parent contains a resource of a different type at the same path as this resource.
- The name of this resource is not valid (according to
IWorkspace.validateName
). - The corresponding location in the local file system is occupied by a directory.
- The corresponding location in the local file system is occupied
by a file and
force
isfalse
. - Resource changes are disallowed during certain types of resource change
event notification. See
IResourceChangeEvent
for more details.
OperationCanceledException
- if the operation is canceled. Cancelation can occur even if no progress monitor is provided.
-
create
Creates a new file resource as a member of this handle's parent resource. The resource's contents are supplied by the data in the given stream. This method closes the stream whether it succeeds or fails. If the stream isnull
then a file is not created in the local file system and the created file resource is marked as being non-local.The
IResource.FORCE
update flag controls how this method deals with cases where the workspace is not completely in sync with the local file system. IfIResource.FORCE
is not specified, the method will only attempt to write a file in the local file system if it does not already exist. This option ensures there is no unintended data loss; it is the recommended setting. However, ifIResource.FORCE
is specified, this method will attempt to write a corresponding file in the local file system, overwriting any existing one if need be.The
IResource.DERIVED
update flag indicates that this resource should immediately be set as a derived resource. Specifying this flag is equivalent to atomically callingIResource.setDerived(boolean)
with a value oftrue
immediately after creating the resource.The
IResource.TEAM_PRIVATE
update flag indicates that this resource should immediately be set as a team private resource. Specifying this flag is equivalent to atomically callingIResource.setTeamPrivateMember(boolean)
with a value oftrue
immediately after creating the resource.The
IResource.HIDDEN
update flag indicates that this resource should immediately be set as a hidden resource. Specifying this flag is equivalent to atomically callingIResource.setHidden(boolean)
with a value oftrue
immediately after creating the resource.Update flags other than those listed above are ignored.
This method changes resources; these changes will be reported in a subsequent resource change event, including an indication that the file has been added to its parent.
This method is long-running; progress and cancellation are provided by the given progress monitor.
- Parameters:
source
- an input stream containing the initial contents of the file, ornull
if the file should be marked as not localupdateFlags
- bit-wise or of update flag constants (IResource.FORCE
,IResource.DERIVED
, andIResource.TEAM_PRIVATE
)monitor
- a progress monitor, ornull
if progress reporting is not desired- Throws:
CoreException
- if this method fails. Reasons include:- This resource already exists in the workspace.
- The parent of this resource does not exist.
- The parent of this resource is a virtual folder.
- The project of this resource is not accessible.
- The parent contains a resource of a different type at the same path as this resource.
- The name of this resource is not valid (according to
IWorkspace.validateName
). - The corresponding location in the local file system is occupied by a directory.
- The corresponding location in the local file system is occupied
by a file and
FORCE
is not specified. - Resource changes are disallowed during certain types of resource change
event notification. See
IResourceChangeEvent
for more details.
OperationCanceledException
- if the operation is canceled. Cancelation can occur even if no progress monitor is provided.- Since:
- 2.0
- See Also:
-
create
default void create(byte[] content, boolean force, boolean derived, IProgressMonitor monitor) throws CoreException Creates the file with the given content as byte array. Same as callingcreate(InputStream, int, IProgressMonitor)
with flags depending on the boolean parameters and anew ByteArrayInputStream(content)
asInputStream
. This method is preferably over the streaming API when the content is available as byte array anyway.- Parameters:
content
- the content as byte arrayforce
- a flag controlling how to deal with resources that are not in sync with the local file systemderived
- Specifying this flag is equivalent to atomically callingIResource.setDerived(boolean)
immediately after creating the resourcemonitor
- a progress monitor, ornull
if progress reporting is not desired- Throws:
CoreException
- if this method fails or is canceled.- Since:
- 3.21
-
create
Creates the file with the given content as byte array. Same as callingcreate(InputStream, int, IProgressMonitor)
and anew ByteArrayInputStream(content)
asInputStream
. This method is preferably over the streaming API when the content is available as byte array anyway.- Parameters:
content
- the content as byte arraycreateFlags
- bit-wise or of flag constants (IResource.FORCE
,IResource.DERIVED
, andIResource.TEAM_PRIVATE
)monitor
- a progress monitor, ornull
if progress reporting is not desired- Throws:
CoreException
- if this method fails or is canceled.- Since:
- 3.21
-
write
default void write(byte[] content, boolean force, boolean derived, boolean keepHistory, IProgressMonitor monitor) throws CoreException Creates the file and sets the file content. If the file already exists in workspace its content is replaced. Shortcut for callingsetContents(byte[], boolean, boolean, IProgressMonitor)
orcreate(byte[], boolean, boolean, IProgressMonitor)
if the file does notIResource.exists()
.- Parameters:
content
- the new content bytes. Must not be null.force
- a flag controlling how to deal with resources that are not in sync with the local file systemderived
- Specifying this flag is equivalent to atomically callingIResource.setDerived(boolean)
immediately after creating the resource or atomically setting the derived flag before setting the content of an already existing file if derived==true. A value of false will not update the derived flag of an existing file.keepHistory
- a flag indicating whether or not store the current contents in the local history if the file did already existmonitor
- a progress monitor, ornull
if progress reporting is not desired- Throws:
CoreException
- if this method fails or is canceled.- Since:
- 3.21
-
createLink
void createLink(IPath localLocation, int updateFlags, IProgressMonitor monitor) throws CoreException Creates a new file resource as a member of this handle's parent resource. The file's contents will be located in the file specified by the given file system path. The given path must be either an absolute file system path, or a relative path whose first segment is the name of a workspace path variable.The
IResource.ALLOW_MISSING_LOCAL
update flag controls how this method deals with cases where the local file system file to be linked does not exist, or is relative to a workspace path variable that is not defined. IfIResource.ALLOW_MISSING_LOCAL
is specified, the operation will succeed even if the local file is missing, or the path is relative to an undefined variable. IfIResource.ALLOW_MISSING_LOCAL
is not specified, the operation will fail in the case where the local file system file does not exist or the path is relative to an undefined variable.The
IResource.REPLACE
update flag controls how this method deals with cases where a resource of the same name as the prospective link already exists. IfIResource.REPLACE
is specified, then the existing linked resource's location is replaced by localLocation's value. This does not cause the underlying file system contents of that resource to be deleted. IfIResource.REPLACE
is not specified, this method will fail if an existing resource exists of the same name.The
IResource.HIDDEN
update flag indicates that this resource should immediately be set as a hidden resource. Specifying this flag is equivalent to atomically callingIResource.setHidden(boolean)
with a value oftrue
immediately after creating the resource.Update flags other than those listed above are ignored.
This method synchronizes this resource with the local file system at the given location.
This method changes resources; these changes will be reported in a subsequent resource change event, including an indication that the file has been added to its parent.
This method is long-running; progress and cancellation are provided by the given progress monitor.
- Parameters:
localLocation
- a file system path where the file should be linkedupdateFlags
- bit-wise or of update flag constants (IResource.ALLOW_MISSING_LOCAL
,IResource.REPLACE
andIResource.HIDDEN
)monitor
- a progress monitor, ornull
if progress reporting is not desired- Throws:
CoreException
- if this method fails. Reasons include:- This resource already exists in the workspace.
- The workspace contains a resource of a different type at the same path as this resource.
- The parent of this resource does not exist.
- The parent of this resource is not an open project
- The name of this resource is not
valid (according to
IWorkspace.validateName
). - The corresponding location in the
local file system does not exist, or is
relative to an undefined variable, and
ALLOW_MISSING_LOCAL
is not specified. - The corresponding location in the local file system is occupied by a directory (as opposed to a file).
- Resource changes are disallowed
during certain types of resource change
event notification. See
IResourceChangeEvent
for more details. - The team provider for the project which contains this folder does not permit linked resources.
- This folder's project contains a nature which does not permit linked resources.
OperationCanceledException
- if the operation is canceled. Cancelation can occur even if no progress monitor is provided.- Since:
- 2.1
- See Also:
-
createLink
Creates a new file resource as a member of this handle's parent resource. The file's contents will be located in the file specified by the given URI. The given URI must be either absolute, or a relative URI whose first path segment is the name of a workspace path variable.The
ALLOW_MISSING_LOCAL
update flag controls how this method deals with cases where the file system file to be linked does not exist, or is relative to a workspace path variable that is not defined. IfALLOW_MISSING_LOCAL
is specified, the operation will succeed even if the local file is missing, or the path is relative to an undefined variable. IfALLOW_MISSING_LOCAL
is not specified, the operation will fail in the case where the file system file does not exist or the path is relative to an undefined variable.The
IResource.REPLACE
update flag controls how this method deals with cases where a resource of the same name as the prospective link already exists. IfIResource.REPLACE
is specified, then any existing resource with the same name is removed from the workspace to make way for creation of the link. This does not cause the underlying file system contents of that resource to be deleted. IfIResource.REPLACE
is not specified, this method will fail if an existing resource exists of the same name.The
IResource.HIDDEN
update flag indicates that this resource should immediately be set as a hidden resource. Specifying this flag is equivalent to atomically callingIResource.setHidden(boolean)
with a value oftrue
immediately after creating the resource.Update flags other than those listed above are ignored.
This method synchronizes this resource with the file system at the given location.
This method changes resources; these changes will be reported in a subsequent resource change event, including an indication that the file has been added to its parent.
This method is long-running; progress and cancellation are provided by the given progress monitor.
- Parameters:
location
- a file system URI where the file should be linkedupdateFlags
- bit-wise or of update flag constants (IResource.ALLOW_MISSING_LOCAL
,IResource.REPLACE
andIResource.HIDDEN
)monitor
- a progress monitor, ornull
if progress reporting is not desired- Throws:
CoreException
- if this method fails. Reasons include:- This resource already exists in the workspace.
- The workspace contains a resource of a different type at the same path as this resource.
- The parent of this resource does not exist.
- The parent of this resource is not an open project
- The name of this resource is not valid (according to
IWorkspace.validateName
). - The corresponding location in the file system does not exist, or
is relative to an undefined variable, and
ALLOW_MISSING_LOCAL
is not specified. - The corresponding location in the file system is occupied by a directory (as opposed to a file).
- Resource changes are disallowed during certain types of resource change
event notification. See
IResourceChangeEvent
for more details. - The team provider for the project which contains this folder does not permit linked resources.
- This folder's project contains a nature which does not permit linked resources.
OperationCanceledException
- if the operation is canceled. Cancelation can occur even if no progress monitor is provided.- Since:
- 3.2
- See Also:
-
delete
Deletes this file from the workspace.This is a convenience method, fully equivalent to:
delete((keepHistory ? KEEP_HISTORY : IResource.NONE) | (force ? FORCE : IResource.NONE), monitor);
This method changes resources; these changes will be reported in a subsequent resource change event, including an indication that this folder has been removed from its parent.
This method is long-running; progress and cancellation are provided by the given progress monitor.
- Parameters:
force
- a flag controlling whether resources that are not in sync with the local file system will be toleratedkeepHistory
- a flag controlling whether files under this folder should be stored in the workspace's local historymonitor
- a progress monitor, ornull
if progress reporting is not desired- Throws:
CoreException
- if this method fails. Reasons include:- This resource could not be deleted for some reason.
- This resource is out of sync with the local file system
and
force
isfalse
. - Resource changes are disallowed during certain types of resource change
event notification. See
IResourceChangeEvent
for more details.
OperationCanceledException
- if the operation is canceled. Cancelation can occur even if no progress monitor is provided.- See Also:
-
getCharset
Returns the name of a charset to be used when decoding the contents of this file into characters.This refinement of the corresponding
IEncodedStorage
method is a convenience method, fully equivalent to:getCharset(true);
Note 1: this method does not check whether the result is a supported charset name. Callers should be prepared to handle
UnsupportedEncodingException
where this charset is used.Note 2: this method returns a cached value for the encoding that may be out of date if the file is not synchronized with the local file system and the encoding has since changed in the file system.
- Specified by:
getCharset
in interfaceIEncodedStorage
- Returns:
- the name of a charset
- Throws:
CoreException
- if this method fails. Reasons include:- This resource could not be read.
- This resource is not local.
- The corresponding location in the local file system is occupied by a directory.
- Since:
- 3.0
- See Also:
-
getCharset
Returns the name of a charset to be used when decoding the contents of this file into characters.If checkImplicit is
false
, this method will return the charset defined by callingsetCharset
, provided this file exists, ornull
otherwise.If checkImplicit is
true
, this method uses the following algorithm to determine the charset to be returned:- the charset defined by calling #setCharset, if any, and this file exists, or
- the charset automatically discovered based on this file's contents, if one can be determined, or
- the default encoding for this file's parent (as defined by
IContainer#getDefaultCharset
).
Note 1: this method does not check whether the result is a supported charset name. Callers should be prepared to handle
UnsupportedEncodingException
where this charset is used.Note 2: this method returns a cached value for the encoding that may be out of date if the file is not synchronized with the local file system and the encoding has since changed in the file system.
- Returns:
- the name of a charset, or
null
- Throws:
CoreException
- if this method fails. Reasons include:- This resource could not be read.
- This resource is not local.
- The corresponding location in the local file system is occupied by a directory.
- Since:
- 3.0
- See Also:
-
getCharsetFor
Returns the name of a charset to be used to encode the given contents when saving to this file. This file does not have to exist. The character stream is not automatically closed.This method uses the following algorithm to determine the charset to be returned:
- if this file exists, the charset returned by IFile#getCharset(false), if one is defined, or
- the charset automatically discovered based on the file name and the given contents, if one can be determined, or
- the default encoding for the parent resource (as defined by
IContainer#getDefaultCharset
).
Note: this method does not check whether the result is a supported charset name. Callers should be prepared to handle
UnsupportedEncodingException
where this charset is used.- Parameters:
reader
- a character stream containing the contents to be saved into this file- Returns:
- the name of a charset
- Throws:
CoreException
- if this method fails. Reasons include:- The given character stream could not be read.
- Since:
- 3.1
- See Also:
-
getContentDescription
Returns a description for this file's current contents. Returnsnull
if a description cannot be obtained.Calling this method produces a similar effect as calling
getDescriptionFor(getContents(), getName(), IContentDescription.ALL)
onIContentTypeManager
, but provides better opportunities for improved performance. Therefore, when manipulatingIFile
s, clients should call this method instead ofIContentTypeManager.getDescriptionFor
.- Returns:
- a description for this file's current contents, or
null
- Throws:
CoreException
- if this method fails. The status code associated with exception reflects the cause of the failure. Reasons include:-
IResourceStatus.RESOURCE_NOT_FOUND
- This file does not exist. Please notice that a successfulIResource.exists()
check prior to callinggetContentDescription()
does not guarantee the file existence since the file may be deleted outside Eclipse at the very last moment. -
IResourceStatus.RESOURCE_NOT_LOCAL
- This resource is not local. -
IResourceStatus.OUT_OF_SYNC_LOCAL
- The workspace is not in sync with the corresponding location in the local file system (andResourcesPlugin.PREF_LIGHTWEIGHT_AUTO_REFRESH
is disabled). -
IResourceStatus.FAILED_DESCRIBING_CONTENTS
- An I/O error occurred while reading the file.
-
- Since:
- 3.0
- See Also:
-
getContents
Returns an open input stream on the contents of this file.This refinement of the corresponding
IStorage
method is a convenience method returning an open input stream. It's equivalent to:getContents(RefreshManager#PREF_LIGHTWEIGHT_AUTO_REFRESH);
If lightweight auto-refresh is not enabled this method will throw a CoreException when opening out-of-sync resources.
The client is responsible for closing the stream when finished.- Specified by:
getContents
in interfaceIStorage
- Returns:
- an input stream containing the contents of the file
- Throws:
CoreException
- if this method fails. The status code associated with exception reflects the cause of the failure. Reasons include:-
IResourceStatus.RESOURCE_NOT_FOUND
- This file does not exist. Please notice that a successfulIResource.exists()
check prior to callinggetContents()
does not guarantee the file existence since the file may be deleted outside Eclipse at the very last moment. -
IResourceStatus.RESOURCE_NOT_LOCAL
- This resource is not local. -
IResourceStatus.RESOURCE_WRONG_TYPE
- The file-system resource is not a file. -
IResourceStatus.OUT_OF_SYNC_LOCAL
- The workspace is not in sync with the corresponding location in the local file system (andResourcesPlugin.PREF_LIGHTWEIGHT_AUTO_REFRESH
is disabled).
-
-
getContents
This refinement of the correspondingIStorage
method returns an open input stream on the contents of this file. The client is responsible for closing the stream when finished. If force istrue
the file is opened and an input stream returned regardless of the sync state of the file. The file is not synchronized with the workspace. If force isfalse
the method fails if not in sync.- Parameters:
force
- a flag controlling how to deal with resources that are not in sync with the local file system- Returns:
- an input stream containing the contents of the file
- Throws:
CoreException
- if this method fails. The status code associated with exception reflects the cause of the failure. Reasons include:-
IResourceStatus.RESOURCE_NOT_FOUND
- This file does not exist. Please notice that a successfulIResource.exists()
check prior to callinggetContents()
does not guarantee the file existence since the file may be deleted outside Eclipse at the very last moment. -
IResourceStatus.RESOURCE_NOT_LOCAL
- This resource is not local. -
IResourceStatus.RESOURCE_WRONG_TYPE
- The file-system resource is not a file. -
IResourceStatus.OUT_OF_SYNC_LOCAL
- The workspace is not in sync with the corresponding location in the local file system (andResourcesPlugin.PREF_LIGHTWEIGHT_AUTO_REFRESH
is disabled).
-
-
getEncoding
Deprecated.usegetCharset()
insteadReturns a constant identifying the character encoding of this file, or ENCODING_UNKNOWN if it could not be determined. The returned constant will be one of the ENCODING_* constants defined on IFile. This method attempts to guess the file's character encoding by analyzing the first few bytes of the file. If no identifying pattern is found at the beginning of the file, ENC_UNKNOWN will be returned. This method will not attempt any complex analysis of the file to make a guess at the encoding that is used.- Returns:
- The character encoding of this file
- Throws:
CoreException
- if this method fails. Reasons include:- This resource does not exist.
- This resource could not be read.
- This resource is not local.
- The corresponding location in the local file system is occupied by a directory.
-
getFullPath
IPath getFullPath()Returns the full path of this file. This refinement of the correspondingIStorage
andIResource
methods links the semantics of resource and storage object paths such thatIFile
s always have a path and that path is relative to the containing workspace.- Specified by:
getFullPath
in interfaceIResource
- Specified by:
getFullPath
in interfaceIStorage
- Returns:
- the absolute path of this resource
- See Also:
-
getHistory
Returns a list of past states of this file known to this workspace. Recently added states first.This method is long-running; progress and cancellation are provided by the given progress monitor.
- Parameters:
monitor
- a progress monitor, ornull
if progress reporting is not desired- Returns:
- an array of states of this file
- Throws:
CoreException
- if this method fails.OperationCanceledException
- if the operation is canceled. Cancelation can occur even if no progress monitor is provided.
-
getName
String getName()Returns the name of this file. This refinement of the correspondingIStorage
andIResource
methods links the semantics of resource and storage object names such thatIFile
s always have a name and that name equivalent to the last segment of its full path. -
isReadOnly
boolean isReadOnly()Returns whether this file is read-only. This refinement of the correspondingIStorage
andIResource
methods links the semantics of read-only resources and read-only storage objects.- Specified by:
isReadOnly
in interfaceIResource
- Specified by:
isReadOnly
in interfaceIStorage
- Returns:
true
if this resource is read-only,false
otherwise- See Also:
-
move
void move(IPath destination, boolean force, boolean keepHistory, IProgressMonitor monitor) throws CoreException Moves this resource to be at the given location.This is a convenience method, fully equivalent to:
move(destination, (keepHistory ? KEEP_HISTORY : IResource.NONE) | (force ? FORCE : IResource.NONE), monitor);
This method changes resources; these changes will be reported in a subsequent resource change event, including an indication that this file has been removed from its parent and a new file has been added to the parent of the destination.
This method is long-running; progress and cancellation are provided by the given progress monitor.
- Parameters:
destination
- the destination pathforce
- a flag controlling whether resources that are not in sync with the local file system will be toleratedkeepHistory
- a flag controlling whether files under this folder should be stored in the workspace's local historymonitor
- a progress monitor, ornull
if progress reporting is not desired- Throws:
CoreException
- if this resource could not be moved. Reasons include:- This resource does not exist.
- This resource is not local.
- The resource corresponding to the parent destination path does not exist.
- The resource corresponding to the parent destination path is a closed project.
- A resource at destination path does exist.
- A resource of a different type exists at the destination path.
- This resource is out of sync with the local file system
and
force
isfalse
. - The workspace and the local file system are out of sync at the destination resource or one of its descendents.
- Resource changes are disallowed during certain types of resource change
event notification. See
IResourceChangeEvent
for more details.
OperationCanceledException
- if the operation is canceled. Cancelation can occur even if no progress monitor is provided.- See Also:
-
setCharset
Deprecated.Replaced bysetCharset(String, IProgressMonitor)
which is a workspace operation and reports changes in resource deltas.Sets the charset for this file. Passing a value ofnull
will remove the charset setting for this resource.- Parameters:
newCharset
- a charset name, ornull
- Throws:
CoreException
- if this method fails. Reasons include:- This resource does not exist.
- An error happened while persisting this setting.
- Since:
- 3.0
- See Also:
-
setCharset
Sets the charset for this file. Passing a value ofnull
will remove the charset setting for this resource.This method changes resources; these changes will be reported in a subsequent resource change event, including an indication that this file's encoding has changed.
This method is long-running; progress and cancellation are provided by the given progress monitor.
- Parameters:
newCharset
- a charset name, ornull
monitor
- a progress monitor, ornull
if progress reporting is not desired- Throws:
OperationCanceledException
- if the operation is canceled. Cancelation can occur even if no progress monitor is provided.CoreException
- if this method fails. Reasons include:- This resource does not exist.
- An error happened while persisting this setting.
- Resource changes are disallowed during certain types of resource change
event notification. See
IResourceChangeEvent
for more details.
- Since:
- 3.0
- See Also:
-
setContents
void setContents(InputStream source, boolean force, boolean keepHistory, IProgressMonitor monitor) throws CoreException Sets the contents of this file to the bytes in the given input stream.This is a convenience method, fully equivalent to:
setContents(source, (keepHistory ? KEEP_HISTORY : IResource.NONE) | (force ? FORCE : IResource.NONE), monitor);
This method changes resources; these changes will be reported in a subsequent resource change event, including an indication that this file's contents have been changed.
This method is long-running; progress and cancellation are provided by the given progress monitor.
- Parameters:
source
- an input stream containing the new contents of the fileforce
- a flag controlling how to deal with resources that are not in sync with the local file systemkeepHistory
- a flag indicating whether or not store the current contents in the local historymonitor
- a progress monitor, ornull
if progress reporting is not desired- Throws:
CoreException
- if this method fails. Reasons include:- This resource does not exist.
- The corresponding location in the local file system is occupied by a directory.
- The workspace is not in sync with the corresponding location
in the local file system and
force
isfalse
. - Resource changes are disallowed during certain types of resource change
event notification. See
IResourceChangeEvent
for more details. - The file modification validator disallowed the change.
OperationCanceledException
- if the operation is canceled. Cancelation can occur even if no progress monitor is provided.- See Also:
-
setContents
void setContents(IFileState source, boolean force, boolean keepHistory, IProgressMonitor monitor) throws CoreException Sets the contents of this file to the bytes in the given file state.This is a convenience method, fully equivalent to:
setContents(source, (keepHistory ? KEEP_HISTORY : IResource.NONE) | (force ? FORCE : IResource.NONE), monitor);
This method changes resources; these changes will be reported in a subsequent resource change event, including an indication that this file's content have been changed.
This method is long-running; progress and cancellation are provided by the given progress monitor.
- Parameters:
source
- a previous state of this resourceforce
- a flag controlling how to deal with resources that are not in sync with the local file systemkeepHistory
- a flag indicating whether or not store the current contents in the local historymonitor
- a progress monitor, ornull
if progress reporting is not desired- Throws:
CoreException
- if this method fails. Reasons include:- This resource does not exist.
- The state does not exist.
- The corresponding location in the local file system is occupied by a directory.
- The workspace is not in sync with the corresponding location
in the local file system and
force
isfalse
. - Resource changes are disallowed during certain types of resource change
event notification. See
IResourceChangeEvent
for more details. - The file modification validator disallowed the change.
OperationCanceledException
- if the operation is canceled. Cancelation can occur even if no progress monitor is provided.- See Also:
-
setContents
void setContents(InputStream source, int updateFlags, IProgressMonitor monitor) throws CoreException Sets the contents of this file to the bytes in the given input stream. The stream will get closed whether this method succeeds or fails. If the stream isnull
then the content is set to be the empty sequence of bytes.The
FORCE
update flag controls how this method deals with cases where the workspace is not completely in sync with the local file system. IfFORCE
is not specified, the method will only attempt to overwrite a corresponding file in the local file system provided it is in sync with the workspace. This option ensures there is no unintended data loss; it is the recommended setting. However, ifFORCE
is specified, an attempt will be made to write a corresponding file in the local file system, overwriting any existing one if need be. In either case, if this method succeeds, the resource will be marked as being local (even if it wasn't before).The
KEEP_HISTORY
update flag controls whether or not a copy of current contents of this file should be captured in the workspace's local history (properties are not recorded in the local history). The local history mechanism serves as a safety net to help the user recover from mistakes that might otherwise result in data loss. SpecifyingKEEP_HISTORY
is recommended except in circumstances where past states of the files are of no conceivable interest to the user. Note that local history is maintained with each individual project, and gets discarded when a project is deleted from the workspace. This flag is ignored if the file was not previously local.Update flags other than
FORCE
andKEEP_HISTORY
are ignored.Prior to modifying the contents of this file, the file modification validator (if provided by the VCM plug-in), will be given a chance to perform any last minute preparations. Validation is performed by calling
IFileModificationValidator.validateSave
on this file. If the validation fails, then this operation will fail.This method changes resources; these changes will be reported in a subsequent resource change event, including an indication that this file's content have been changed.
This method is long-running; progress and cancellation are provided by the given progress monitor.
- Parameters:
source
- an input stream containing the new contents of the fileupdateFlags
- bit-wise or of update flag constants (FORCE
andKEEP_HISTORY
)monitor
- a progress monitor, ornull
if progress reporting is not desired- Throws:
CoreException
- if this method fails. Reasons include:- This resource does not exist.
- The corresponding location in the local file system is occupied by a directory.
- The workspace is not in sync with the corresponding location
in the local file system and
FORCE
is not specified. - Resource changes are disallowed during certain types of resource change
event notification. See
IResourceChangeEvent
for more details. - The file modification validator disallowed the change.
OperationCanceledException
- if the operation is canceled. Cancelation can occur even if no progress monitor is provided.- Since:
- 2.0
- See Also:
-
setContents
Sets the contents of this file to the bytes in the given file state.The
FORCE
update flag controls how this method deals with cases where the workspace is not completely in sync with the local file system. IfFORCE
is not specified, the method will only attempt to overwrite a corresponding file in the local file system provided it is in sync with the workspace. This option ensures there is no unintended data loss; it is the recommended setting. However, ifFORCE
is specified, an attempt will be made to write a corresponding file in the local file system, overwriting any existing one if need be. In either case, if this method succeeds, the resource will be marked as being local (even if it wasn't before).The
KEEP_HISTORY
update flag controls whether or not a copy of current contents of this file should be captured in the workspace's local history (properties are not recorded in the local history). The local history mechanism serves as a safety net to help the user recover from mistakes that might otherwise result in data loss. SpecifyingKEEP_HISTORY
is recommended except in circumstances where past states of the files are of no conceivable interest to the user. Note that local history is maintained with each individual project, and gets discarded when a project is deleted from the workspace. This flag is ignored if the file was not previously local.Update flags other than
FORCE
andKEEP_HISTORY
are ignored.Prior to modifying the contents of this file, the file modification validator (if provided by the VCM plug-in), will be given a chance to perform any last minute preparations. Validation is performed by calling
IFileModificationValidator.validateSave
on this file. If the validation fails, then this operation will fail.This method changes resources; these changes will be reported in a subsequent resource change event, including an indication that this file's content have been changed.
This method is long-running; progress and cancellation are provided by the given progress monitor.
- Parameters:
source
- a previous state of this resourceupdateFlags
- bit-wise or of update flag constants (FORCE
andKEEP_HISTORY
)monitor
- a progress monitor, ornull
if progress reporting is not desired- Throws:
CoreException
- if this method fails. Reasons include:- This resource does not exist.
- The state does not exist.
- The corresponding location in the local file system is occupied by a directory.
- The workspace is not in sync with the corresponding location
in the local file system and
FORCE
is not specified. - Resource changes are disallowed during certain types of resource change
event notification. See
IResourceChangeEvent
for more details. - The file modification validator disallowed the change.
OperationCanceledException
- if the operation is canceled. Cancelation can occur even if no progress monitor is provided.- Since:
- 2.0
- See Also:
-
setContents
default void setContents(byte[] content, boolean force, boolean keepHistory, IProgressMonitor monitor) throws CoreException Equivalent of callingsetContents(InputStream, boolean, boolean, IProgressMonitor)
withnew ByteArrayInputStream(content)
asInputStream
. This is preferable (potentially faster) when the content is available as byte array anyway.- Parameters:
content
- the content bytesforce
- a flag controlling how to deal with resources that are not in sync with the local file systemkeepHistory
- a flag indicating whether or not store the current contents in the local historymonitor
- a progress monitor, ornull
if progress reporting is not desired- Throws:
CoreException
- if this method fails or is canceled.- Since:
- 3.21
-
setContents
default void setContents(byte[] content, int updateFlags, IProgressMonitor monitor) throws CoreException Equivalent of callingsetContents(InputStream, int, IProgressMonitor)
withnew ByteArrayInputStream(content)
asInputStream
. This is preferable (potentially faster) when the content is available as byte array anyway.- Parameters:
content
- the content bytesupdateFlags
- bit-wise or of update flag constants (FORCE
andKEEP_HISTORY
)monitor
- a progress monitor, ornull
if progress reporting is not desired- Throws:
CoreException
- if this method fails or is canceled.- Since:
- 3.21
-
readAllBytes
Reads the content in a byte array. This method is not intended for reading in large files that do not fit in a byte array. Preferable (faster) equivalent of callinggetContents(true).readAllBytes()
.- Returns:
- content bytes
- Throws:
CoreException
- on error- Since:
- 3.21
- See Also:
-
readNBytes
Reads the first bytes of the content in a byte array. Equivalent of callinggetContents(true).readNBytes(maxBytes)
but also closing the stream.- Parameters:
maxBytes
- The maximal length of the returned array. If maxBytes is greater or equal to the file length the whole content is returned. If maxBytes is smaller then the file length then only the first maxBytes bytes are returned.- Returns:
- content bytes
- Throws:
CoreException
- on error- Since:
- 3.21
- See Also:
-
readAllChars
Reads the content as char array. Skips the UTF BOM header if any. This method is not intended for reading in large files that do not fit in a char array. Preferable (potentially faster) equivalent of callingreadString().toCharArray()
.- Returns:
- content as char array without UTF BOM header
- Throws:
CoreException
- on error- Since:
- 3.21
- See Also:
-
readString
Reads the content as String. Skips the UTF BOM header if any. This method is not intended for reading in large files that do not fit in a String.- Returns:
- content String without UTF BOM header
- Throws:
CoreException
- on error- Since:
- 3.21
- See Also:
-
getLineSeparator
Returns line separator appropriate for the given file.If checkParent is
false
, this method will return the line separator by reading the file, ornull
otherwise.If checkParent is
true
, this method uses the following algorithm to determine the line separator to be returned:- the line separator currently used in that file (same as output with checkImplicit=false), or
- delegates to
IProject.getDefaultLineSeparator()
- Parameters:
checkParent
- whether to look up in parent containers settings to retrieve a value- Returns:
- line separator for the current file
- Throws:
CoreException
- if this method fails. Reasons include:- This resource could not be read.
- This resource is not local.
- The corresponding location in the local file system is occupied by a directory.
- Since:
- 3.18
- See Also:
-