路径path相关模式

PatternsRequestCondition
private String getMatchingPattern(String pattern, String lookupPath) {
		if (pattern.equals(lookupPath)) {
			return pattern;
		}
//判断前缀索引
		if (this.useSuffixPatternMatch) {
//是否配置了扩展符以及当前URL是否是以”.“结尾
			if (!this.fileExtensions.isEmpty() && lookupPath.indexOf('.') != -1) {
				for (String extension : this.fileExtensions) {
//调用自定义的AntPathMatcher 进行匹配
					if (this.pathMatcher.match(pattern + extension, lookupPath)) {
						return pattern + extension;
					}
				}
			}
			else {
				boolean hasSuffix = pattern.indexOf('.') != -1;
//判断匹配模式是否以”.*“为结尾
				if (!hasSuffix && this.pathMatcher.match(pattern + ".*", lookupPath)) {
					return pattern + ".*";
				}
			}
		}
		if (this.pathMatcher.match(pattern, lookupPath)) {
			return pattern;
		}
//判断是否是 斜线匹配
		if (this.useTrailingSlashMatch) {
			if (!pattern.endsWith("/") && this.pathMatcher.match(pattern + "/", lookupPath)) {
				return pattern +"/";
			}
		}
		return null;
	}

/**
 * 通常情况下resources目录下的文件打包后存在于类路径下。
 * 包路径下如果加入文本文件,打包后在对应的类路径下是无法找到的。即不会打包对应路径下文本文件。
 *
 * getResource & getResourceAsStream:加载路径其实都是一样的,只不过结果一个表示URL,另一个为InputStream
 *
 */
public class CustomResource {

    public static void main(String[] args) throws IOException {
        m5();
    }


    /**
     * 该方法等价于m4功能
     * @throws IOException
     */
    public static void m5() throws IOException {

        InputStream systemResourceAsStream = ClassLoader.getSystemResourceAsStream("test.txt");
        read(systemResourceAsStream);
        URL systemResource = ClassLoader.getSystemResource("custom/custom.txt");
        read(systemResource.openStream());
    }

    /**
     * 通过类加载器加载资源是禁止使用"/"。
     * @throws IOException
     */
    public static void m4() throws IOException {
        URL resource = CustomResource.class.getClassLoader().getResource("test.txt");
        read(resource.openStream());
        //返回为null
        System.out.println(CustomResource.class.getClassLoader().getResource("/"));
        URL resource1 = CustomResource.class.getClassLoader().getResource("custom/custom.txt");
        read(resource1.openStream());

        InputStream resourceAsStream = CustomResource.class.getClassLoader().getResourceAsStream("custom/custom.txt");
        read(resourceAsStream);
    }

    /**
     * 对用的查找路径同样为类路径的根路径以及当前类的路径下。
     * @throws IOException
     */
    public static void m3() throws IOException {
        //根路径
        InputStream resourceAsStream = CustomResource.class.getResourceAsStream("/1.txt");
        //对应类的类路径
        InputStream resourceAsStream1 = CustomResource.class.getResourceAsStream("1.txt");
        read(resourceAsStream1);
        read(resourceAsStream);
    }

    /**
     * target/classes/net/clazzloader/resource 路径下加载文件。
     * 前提是将1.txt文件打包后存在于对应路径下。
     * @throws IOException
     */
    public static void m1() throws IOException {
        URL resource = CustomResource.class.getResource("1.txt");
        InputStream inputStream = resource.openStream();
        read(inputStream);
    }

    /**
     * 类路径的根路径,即target/classes。文本文件打包后的路径位置为target/classes/1.txt
     * @throws IOException
     */
    public static void m2() throws IOException {
        URL resource = CustomResource.class.getResource("/1.txt");
        InputStream inputStream = resource.openStream();
        read(inputStream);
    }

    public static void read(InputStream inputStream) throws IOException {
        StringBuilder rtn = new StringBuilder();
        int read = 0;
        while ((read = inputStream.read()) != -1){
            rtn.append((char)read);
        }
        inputStream.close();
        System.out.println(new String(rtn.toString()));
    }
}
JdkTimer.class.getClassLoader().getResourceAsStream("net/csdn/timer/quartz.properties")

如上方式通过类加载器加载指定文件:

  1. 路径不能加绝对路径“/”。
  2. 相对路径是指类路径:classpath,而非当前类所在的路径。所以必须在classpath路径下新建路径“net/csdn/timer”,而不是在类JdkTimer同包下新建quartz.properties文件。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
二叉树的最大路径和ACM模式是一个经典的算法问题。在这个模式中,我们需要找到二叉树中两个节点之间的路径,使得路径上节点值的和最大。为了解决这个问题,我们可以使用递归的方法。 首先,我们定义一个辅助函数helper来计算从当前节点开始的最大路径和。在这个函数中,我们首先检查当前节点是否为空,如果是空节点,则返回0。然后,我们递归地计算左子树和右子树的最大路径和,分别存储在变量l和r中。接下来,我们计算过当前节点的最大路径和curSum,它可以是当前节点的值与左子树路径和、右子树路径和的和中的最大值。然后,我们计算如果将当前节点作为根节点时的最大路径和curMax,它可以是curSum与左子树路径和、右子树路径和的和中的最大值。最后,我们更新全局最大路径和m为m和curMax中的较大值,并返回过当前节点的最大路径和curSum。 在主函数maxPathSum中,我们首先检查根节点是否为空,如果是空节点,则返回0。然后,我们定义一个变量m来存储全局最大路径和的初始值INT_MIN。接下来,我们调用辅助函数helper来计算最大路径和,并将m传入函数,以便在函数中更新最大值m。最后,我们返回最大路径和m。 代码实现如下: ``` int maxPathSum(TreeNode* root) { if(!root) return 0; int m=INT_MIN; helper(root,m); return m; } int helper(TreeNode* root,int &m) { if(!root) return 0; int l=helper(root->left,m); int r=helper(root->right,m); int curSum=max(root->val,max(l+root->val,r+root->val)); int curMax=max(curSum,l+r+root->val); m=max(m,curMax); return curSum; } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值