利用StringTokenizer类获取文件名的两种方法

第一种方法:

String filePath = "D:\\a\\b\\ccc.txt";
		File file = new File(filePath);
		String fileName = file.getName();
		StringTokenizer sto = new StringTokenizer(fileName, ".");
		while(sto.hasMoreTokens()){
			System.out.println(sto.nextToken());
		}

 

第二种方法:

StringTokenizer sto1 = new StringTokenizer(filePath, "\\");
		String fileName1 = "";
		int count = sto1.countTokens();
		for(int i=0;i<count;i++){
			if(i==count-1){
				fileName1 = sto1.nextToken();
			}else{
				sto1.nextToken();
			}
		}
		StringTokenizer sto11 = new StringTokenizer(fileName1,".");
		while(sto11.hasMoreTokens()){
			System.out.println(sto11.nextToken());
		}

 由于只是获取文件名,其实就是截取字符串,两种方式所花费的时间可以说是一样的。

 

 

你可能感兴趣的:(StringTokenizer)