Advertisement
Guest User

Yaml code

a guest
Jul 18th, 2018
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.96 KB | None | 0 0
  1. package utils;
  2.  
  3. import java.io.File;
  4. import java.io.FileNotFoundException;
  5. import java.util.HashMap;
  6. import java.util.Iterator;
  7. import java.util.Map;
  8. import java.util.Scanner;
  9.  
  10. public class Main {
  11.  
  12.     public static void main(String[] args) {
  13.        
  14.         System.out.println(getString("C:\\Users\\Gebruiker\\Desktop\\config.yml", "items.item"));;
  15.  
  16.     }
  17.    
  18.     public static String getString(String file, String path) {
  19.        
  20.         File f = new File(file);
  21.        
  22.         Scanner scanner = null;
  23.         try {scanner = new Scanner(f);} catch (FileNotFoundException e) {}
  24.        
  25.         HashMap<Integer, String> map = new HashMap<>();
  26.         HashMap<Integer, Integer> mapSpacing = new HashMap<>();
  27.        
  28.         int lineNumber = 0;
  29.        
  30.         while(scanner.hasNext()) {
  31.             lineNumber++;
  32.             String line = scanner.nextLine();
  33.             map.put(lineNumber, line);
  34.            
  35.             String tmp = line.split(":")[0];
  36.             String[] split = tmp.split(" ");
  37.             int spaces = split.length - 1;
  38.             mapSpacing.put(lineNumber, spaces);
  39.         }
  40.        
  41.         int size = (path.length() - path.replace(".", "").length()) * 2;
  42.        
  43.         String name = path.substring(path.lastIndexOf(".") + 1, path.length());
  44.        
  45.         Iterator<?> it = map.entrySet().iterator();
  46.         while (it.hasNext()) {
  47.             @SuppressWarnings("unchecked")
  48.             Map.Entry<Integer, String> p = (Map.Entry<Integer, String>)it.next();
  49.            
  50.             if (mapSpacing.get(p.getKey()) == size) {
  51.                 String tmp[] = p.getValue().split(":");
  52.                 String tmp1 = tmp[0].replace(" ", "");
  53.                
  54.                 if (tmp1.equals(name)) {
  55.  
  56.                     String split = name + ": ";
  57.                    
  58.                     for (int i=0; i < size; i++) {
  59.                         split = " " + split;
  60.                     }
  61.                    
  62.                     String tmpValue[] = p.getValue().split(split);
  63.                    
  64.                     if (tmpValue.length < 2) {
  65.                         return "";
  66.                     }
  67.                    
  68.                     String value = tmpValue[1];
  69.                    
  70.                     return value;
  71.                    
  72.                 }
  73.             }
  74.         }
  75.         return "";
  76.     }
  77.  
  78. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement