⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 file.java

📁 纯java操作系统jnode,安装简单和操作简单的个人使用的Java操作系统
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
	/**
	  * This method returns a <code>File</code> object representing the
	  * canonical path of this object.
	  *
	  * @return A <code>File</code> instance representing the canonical path of
	  * this object.
	  *
	  * @exception IOException If an error occurs.
	  */
	public File getCanonicalFile() throws IOException {
		return (new File(getCanonicalPath()));
	}

	/*************************************************************************/

	/**
	  * This method returns a <code>String</code> the represents this file's
	  * parent.  <code>null</code> is returned if the file has no parent.  The
	  * parent is determined via a simple operation which removes the
	  *
	  * @return The parent directory of this file
	  */
	public String getParent() {
		if (PlatformHelper.isRootDirectory(path))
			return null;

		String par_path = path;

		int pos = PlatformHelper.lastIndexOfSeparator(par_path);
		if (pos == -1)
			return null;

		return (par_path.substring(0, pos));
	}

	/*************************************************************************/

	/**
	  * This method returns a <code>File</code> object representing the parent
	  * file of this one.
	  *
	  * @param A <code>File</code> for the parent of this object.  <code>null</code>
	  * will be returned if this object does not have a parent.
	  */
	public File getParentFile() {
		String parent = getParent();
		if (parent == null) {
			return null;
		}
      if (parent.length() == 0)
        return null;

		return new File(parent);
	}

	/*************************************************************************/

	/**
	  * This method returns true if this object represents an absolute file
	  * path and false if it does not.  The definition of an absolute path varies
	  * by system.  As an example, on GNU systems, a path is absolute if it starts
	  * with a "/".
	  *
	  * @return <code>true</code> if this object represents an absolute file name, <code>false</code> otherwise.
	  */
	public boolean isAbsolute() {
		return PlatformHelper.beginWithRootPathPrefix(path) > 0;
	}

	/**
	  * This method tests whether or not the current thread is allowed to
	  * to read the file pointed to by this object.  This will be true if and
	  * and only if 1) the file exists and 2) the <code>SecurityManager</code>
	  * (if any) allows access to the file via it's <code>checkRead</code>
	  * method 3) the file is readable.
	  *
	  * @return <code>true</code> if reading is allowed, <code>false</code> otherwise
	  *
	  * @exception SecurityException If the <code>SecurityManager</code> does not allow access to the file
	  */
	public boolean canRead() throws SecurityException {
		// Test for existence. This also does the SecurityManager check
		if (!exists()) {
			return false;
		} else {
			try {
				return VMIOUtils.getAPI().canRead(this);
			} catch (IOException ex) {
				return false;
			}
		}
	}

	/**
	  * This method test whether or not the current thread is allowed to
	  * write to this object.  This will be true if and only if 1) The
	  * <code>SecurityManager</code> (if any) allows write access to the
	  * file and 2) The file exists and 3) The file is writable.  To determine
	  * whether or not a non-existent file can be created, check the parent
	  * directory for write access.
	  *
	  * @return <code>true</code> if writing is allowed, <code>false</code> otherwise
	  *
	  * @exception SecurityException If the <code>SecurityManager</code> does not allow access to the file
	  */
	public boolean canWrite() throws SecurityException {
		// We still need to do a SecurityCheck since exists() only checks
		// for read access
		final SecurityManager sm = System.getSecurityManager();
		if (sm != null) {
			sm.checkWrite(path);
		}

		// Test for existence.  This is required by the spec
		if (!exists()) {
			return false;
		} else if (!isDirectory()) {
			try {
				return VMIOUtils.getAPI().canWrite(this);
			} catch (IOException ioe) {
				return (false);
			}
		} else {
			try {
				File test = createTempFile("test-dir-write", null, this);
				return (test != null && test.delete());
			} catch (IOException ioe) {
				return (false);
			}
		}
	}

	/**
	  * This method sets the file represented by this object to be read only.
	  * A read only file or directory cannot be modified.  Please note that 
	  * GNU systems allow read only files to be deleted if the directory it
	  * is contained in is writable.
	  *
	  * @return <code>true</code> if the operation succeeded, <code>false</code>
	  * otherwise.
	  *
	  * @exception SecurityException If the <code>SecurityManager</code> does
	  * not allow this operation.
	  */
	public boolean setReadOnly() throws SecurityException {
		// Test for existence.
		if (!exists()) {
			return false;
		}

		// We still need to do a SecurityCheck since exists() only checks
		// for read access
		final SecurityManager sm = System.getSecurityManager();
		if (sm != null) {
			sm.checkWrite(path);
		}
		
		try {
			VMIOUtils.getAPI().setReadOnly(this);
			return true;
		} catch (IOException ex) {
			return false;
		}
	}

	/**
	  * This method tests whether or not the file represented by the object
	  * actually exists on the filesystem.
	  *
	  * @return <code>true</code> if the file exists, <code>false</code>otherwise.
	  *
	  * @exception SecurityException If reading of the file is not permitted
	  */
	public boolean exists() throws SecurityException {
        // Check the SecurityManager
		final SecurityManager sm = System.getSecurityManager();
		if (sm != null) {
			sm.checkRead(path);
		}

		try {
			return VMIOUtils.getAPI().fileExists(this);
		} catch (IOException ex) {
         return false;
		}
	}

	/**
	  * This method tests whether or not the file represented by this object
	  * is a "plain" file.  A file is a plain file if and only if it 1) Exists,
	  * 2) Is not a directory or other type of special file.
	  *
	  * @return <code>true</code> if this is a plain file, <code>false</code> otherwise
	  *
	  * @exception SecurityException If reading of the file is not permitted
	  */
	public boolean isFile() throws SecurityException {
		// Check the SecurityManager
		final SecurityManager sm = System.getSecurityManager();
		if (sm != null) {
			sm.checkRead(path);
		}

		try {
			return VMIOUtils.getAPI().isFile(this);
		} catch (IOException ex) {
			return false;
		}
	}

	/*************************************************************************/

	/**
	  * This method tests whether or not the file represented by this object
	  * is a directory.  In order for this method to return <code>true</code>,
	  * the file represented by this object must exist and be a directory.
	  * 
	  * @return <code>true</code> if this file is a directory, <code>false</code> otherwise
	  *
	  * @exception SecurityException If reading of the file is not permitted
	  */
	public boolean isDirectory() throws SecurityException {
		// Check the SecurityManager
		final SecurityManager sm = System.getSecurityManager();
		if (sm != null) {
			sm.checkRead(path);
		}

		try {
			return VMIOUtils.getAPI().isDirectory(this);
		} catch (IOException ex) {
			return false;
		}
	}

	/*************************************************************************/

	/**
	  * This method tests whether or not this file represents a "hidden" file.
	  * On GNU systems, a file is hidden if its name begins with a "."
	  * character.  Files with these names are traditionally not shown with
	  * directory listing tools.
	  *
	  * @return <code>true</code> if the file is hidden, <code>false</code>
	  * otherwise.
	  */
	public boolean isHidden() {
		if (getName().startsWith("."))
			return (true);
		else
			return (false);
	}

	/**
	  * This method returns the length of the file represented by this object,
	  * or 0 if the specified file does not exist.
	  *
	  * @return The length of the file
	  *
	  * @exception SecurityException If reading of the file is not permitted
	  */
	public long length() throws SecurityException {
		// Check the SecurityManager
		final SecurityManager sm = System.getSecurityManager();
		if (sm != null) {
			sm.checkRead(path);
		}

		try {
			return VMIOUtils.getAPI().getLength(this);
		} catch (IOException ex) {
			return 0;
		}
	}

	/*************************************************************************/

	/**
	  * This method returns the last modification time of this file.  The
	  * time value returned is an abstract value that should not be interpreted
	  * as a specified time value.  It is only useful for comparing to other
	  * such time values returned on the same system.  In that case, the larger
	  * value indicates a more recent modification time. 
	  * <p>
	  * If the file does not exist, then a value of 0 is returned.
	  *
	  * @return The last modification time of the file
	  *
	  * @exception SecurityException If reading of the file is not permitted
	  */
	public long lastModified() throws SecurityException {
		// Check the SecurityManager
		final SecurityManager sm = System.getSecurityManager();
		if (sm != null) {
			sm.checkRead(path);
		}

		try {
			return VMIOUtils.getAPI().getLastModified(this);
		} catch (IOException ex) {
			return 0;
		}
	}

	/**
	  * This method sets the modification time on the file to the specified
	  * value.  This is specified as the number of seconds since midnight
	  * on January 1, 1970 GMT.
	  *
	  * @param time The desired modification time.
	  *
	  * @return <code>true</code> if the operation succeeded, <code>false</code>
	  * otherwise.
	  *
	  * @exception IllegalArgumentException If the specified time is negative.
	  * @exception SecurityException If the <code>SecurityManager</code> will
	  * not allow this operation.
	  */
	public boolean setLastModified(long time)
	throws IllegalArgumentException, SecurityException {
		if (time < 0) {
			throw new IllegalArgumentException(
				"Negative modification time: " + time);
		}

		// Check the SecurityManager
		SecurityManager sm = System.getSecurityManager();
		if (sm != null) {
			sm.checkWrite(path);
		}

		try {
			VMIOUtils.getAPI().setLastModified(this, time);
			return true;
		} catch (IOException ex) {
			return false;
		}
	}

	/*************************************************************************/

	/**
	  * This method creates a new file of zero length with the same name as
	  * the path of this <code>File</code> object if an only if that file
	  * does not already exist.
	  * <p>
	  * A <code>SecurityManager</code>checkWrite</code> check is done prior
	  * to performing this action.
	  *
	  * @return <code>true</code> if the file was created, <code>false</code> if
	  * the file alread existed.
	  *
	  * @exception IOException If an I/O error occurs
	  * @exception SecurityException If the <code>SecurityManager</code> will
	  * not allow this operation to be performed.
	  */
	public boolean createNewFile() throws IOException, SecurityException {
		SecurityManager sm = System.getSecurityManager();
		if (sm != null)
			sm.checkWrite(path);

		return (createInternal(getPath()));
	}

	/*************************************************************************/

	/**
	  * This method deletes the file represented by this object.  If this file
	  * is a directory, it must be empty in order for the delete to succeed.
	  *
	  * @return <code>true</code> if the file was deleted, <code>false</code> otherwise
	  *
	  * @exception SecurityException If deleting of the file is not allowed
	  */
	public synchronized boolean delete() throws SecurityException {
		// Check the SecurityManager
		final SecurityManager sm = System.getSecurityManager();
		if (sm != null) {
			sm.checkDelete(path);
		}

		try {
			VMIOUtils.getAPI().delete(this);
			return true;
		} catch (IOException ex) {
			return false;
		}
	}

	/*************************************************************************/

	/**
	  * Calling this method requests that the file represented by this object
	  * be deleted when the virtual machine exits.  Note that this request cannot
	  * be cancelled.  Also, it will only be carried out if the virtual machine
	  * exits normally.
	  *
	  * @exception SecurityException If deleting of the file is not allowed
	  */
	public void deleteOnExit() throws SecurityException {
		// Check the SecurityManager
		SecurityManager sm = System.getSecurityManager();
		if (sm != null)
			sm.checkDelete(path);

		// Sounds like we need to do some VM specific stuff here. We could delete
		// the file in finalize() and set FinalizeOnExit to true, but delete on
		// finalize != delete on exit and we should not be setting up system
		// parameters without the user's knowledge.
		//********TODO: IMPLEMENT ME!!!!!!***************
		return;
	}

	/*************************************************************************/

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -