Home > Java > javaTutorial > body text

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

王林
Release: 2020-05-16 11:21:23
Original
4979 people have browsed it

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("磁盘路径正确。。。");
		}
		
	}
Copy after login

推荐教程: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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template