YAML中重复的KEY的判断

package com.test.util;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

public class CheckYaml {

	public void checkYaml(String fileName) {
		BufferedReader read = null;
		try {
			read = new BufferedReader(new InputStreamReader(new FileInputStream("locator/" + fileName)));
			String temp = read.readLine();
			List<String> list = new ArrayList<String>();
			while(temp != null){
				if(!temp.startsWith(" ") && !temp.trim().startsWith("#")){
					String key = temp.substring(0, temp.length()-1);
					if(list.contains(key)){
						System.out.println("duplicate key: "+key);
					}else{
						list.add(key);
					}
				}
				temp = read.readLine();
			}
		} catch (FileNotFoundException e) {			
			e.printStackTrace();
		} catch (IOException e) {			
			e.printStackTrace();
		}finally{			
			try {
				if(read != null){
				read.close();
				}
			} catch (IOException e) {				
				e.printStackTrace();
			}
		}		
	}

	public static void main(String[] args) {
		CheckYaml cy = new CheckYaml();
		cy.checkYaml("TestBaidu.yaml");
	}

}

 

posted on 2015-05-15 13:21  张飞_  阅读(2918)  评论(0编辑  收藏  举报

导航