Home  >  Article  >  Java  >  How to determine whether a string is a file path in java

How to determine whether a string is a file path in java

王林
王林Original
2020-05-16 11:21:234966browse

How to determine whether a string is a file path in java

我们可以使用matches()方法与正则表达式进行判断。

matches() 方法用于检测字符串是否匹配给定的正则表达式。

具体的正则表达式为:【^[A-z]:\\\\(.+?\\\\)*$】。

(视频教程推荐:java视频

具体代码:

	public static void main(String[] args) {
//		String source = "z:\\java\\kl\\$kls\\";
		String source = args[0]+File.separator;
		String targer = args[1]+File.separator;
		System.out.println(source);
		System.out.println(targer);
		if(source.matches("^[A-z]:\\\\(.+?\\\\)*$") && targer.matches("^[A-z]:\\\\(.+?\\\\)*$")){
			System.out.println("磁盘路径正确。。。");
		}
		
	}

推荐教程:java开发入门

The above is the detailed content of How to determine whether a string is a file path in java. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn