Java FilePermission equals()方法

java.io.FileOutputStream.equals(Object obj) 用于检查两个FilePermission对象是否相等。

1 语法

public boolean equals(Object obj)

2 参数

obj:要测试的对象是否与此对象相等。

3 返回值

如果obj是FilePermission,并且与此对象具有相同的路径名和操作,则此方法返回true。

4 示例 

package com.yiidian;

/**
 * 一点教程网: http://www.yiidian.com
 */
/**
 * java.io.FileOutputStream.equals(Object obj) 方法的例子
 */
import java.io.FilePermission;
import java.io.IOException;

public class Demo {
    public static void main(String[] args) throws IOException {
        FilePermission fp = null;
        FilePermission fp1 = null;
        FilePermission fp2 = null;
        FilePermission fp3 = null;
        boolean bool = false;

        try {
            // create new file permissions
            fp = new FilePermission("d://test.txt", "read");
            fp1 = new FilePermission("d://test1.txt", "read");
            fp2 = new FilePermission("d://test.txt", "write");
            fp3 = new FilePermission("d://test.txt", "read");

            // checks two file permission objects for equality
            bool = fp.equals(fp1);

            // prints
            System.out.println(bool);

            // checks two file permission objects for equality
            bool = fp.equals(fp2);

            // prints
            System.out.println(bool);

            // checks two file permission objects for equality
            bool = fp.equals(fp3);

            // prints
            System.out.print(bool);

        } catch(Exception ex) {
            // if an error occurs
            ex.printStackTrace();
        } finally {

        }
    }
}

输出结果为:

false
false
true

热门文章

优秀文章