文件的读取工具 properties getClass().getResourceAsStream

119 阅读2分钟

文件的读取工具


/**
 * properties文件的读取工具
 *
 */
public class PropertiesReader {

   
   public Map<String, String> getProperties() {

      Properties props = new Properties();
      Map<String, String> map = new HashMap<String, String>();
      try {
         InputStream in = getClass().getResourceAsStream("type.properties");
         props.load(in);
         Enumeration en = props.propertyNames();
         while (en.hasMoreElements()) {
            String key = (String) en.nextElement();
            String property = props.getProperty(key);
            map.put(key, property);
            // System.out.println(key + "  " + property);
         }
      } catch (Exception e) {
         e.printStackTrace();
      }
      return map;
   }
}

getClass().getResourceAsStream

type.properties

left=com.LeftHair
right=com.RightHair
in=com.InHair


/**
 * Finds a resource with a given name.  The rules for searching resources
 * associated with a given class are implemented by the defining
 * {@linkplain ClassLoader class loader} of the class.  This method
 * delegates to this object's class loader.  If this object was loaded by
 * the bootstrap class loader, the method delegates to {@link
 * ClassLoader#getSystemResourceAsStream}.
 *
 * <p> Before delegation, an absolute resource name is constructed from the
 * given resource name using this algorithm:
 *
 * <ul>
 *
 * <li> If the {@code name} begins with a {@code '/'}
 * (<tt>'&#92;u002f'</tt>), then the absolute name of the resource is the
 * portion of the {@code name} following the {@code '/'}.
 *
 * <li> Otherwise, the absolute name is of the following form:
 *
 * <blockquote>
 *   {@code modified_package_name/name}
 * </blockquote>
 *
 * <p> Where the {@code modified_package_name} is the package name of this
 * object with {@code '/'} substituted for {@code '.'}
 * (<tt>'&#92;u002e'</tt>).
 *
 * </ul>
 *
 * @param  name name of the desired resource
 * @return      A {@link java.io.InputStream} object or {@code null} if
 *              no resource with this name is found
 * @throws  NullPointerException If {@code name} is {@code null}
 * @since  JDK1.1
 */
 public InputStream getResourceAsStream(String name) {
    name = resolveName(name);
    ClassLoader cl = getClassLoader0();
    if (cl==null) {
        // A system class.
        return ClassLoader.getSystemResourceAsStream(name);
    }
    return cl.getResourceAsStream(name);
}

 
public InputStream getResourceAsStream(String name) {
    URL url = getResource(name);
    try {
        return url != null ? url.openStream() : null;
    } catch (IOException e) {
        return null;
    }
}



public URL getResource(String name) {
    URL url;
    if (parent != null) {
        url = parent.getResource(name);
    } else {
        url = getBootstrapResource(name);
    }
    if (url == null) {
        url = findResource(name);
    }
    return url;
}

 

有相同的数据(相交) Collections.disjoint

Collections disjoint()方法

  • disjoint() 方法可在java.util包。
  • disjoint() 方法Collection 是否可能包含任何公共元素。
  • disjoint() 方法 静态方法,因此可以可以通过类名访问它
  • disjoint() 方法在检查不存在公共元素可能抛出异常。
    NullPointerException :当给定参数为空时,可能會拋出此異常存在。

用法:

public static boolean disjoint(Collection cl1, Collection cl2);

參數:

  • Collection cl1, Collection cl2– 代表不同的 Collection 對象。

返回值: 方法的返回类型是boolean, 当 Collection 对象中不存在公共元素是返回真,否则返回假。

例:


List<String> orderCityIDList = new ArrayList<>();
List<String> cityIds = new ArrayList<>();

orderCityIDList.add(travelCityID);


if (!StringUtils.isEmpty(journeyInfo.getDepartureCityID())) {
    cityIds.add(journeyInfo.getDepartureCityID());
}
if (!StringUtils.isEmpty(journeyInfo.getArrivalCityID())) {
    cityIds.addAll(Arrays.asList(journeyInfo.getArrivalCityID().split("[,,]")));
}
if (!Collections.disjoint(cityIds, orderCityIDList)) {
    matchInfo.setIsCityMatch(1);
    continue;
}

public class Disjoint {
    public static void main(String args[]) {
        List < Integer > l1 = new LinkedList < Integer > ();
        List < Integer > l2 = new LinkedList < Integer > ();

        l1.add(10);
        l1.add(20);
        l1.add(30);
        l1.add(40);

        l2.add(60);
        l2.add(70);
        l2.add(80);
        l2.add(90);
        boolean status = Collections.disjoint(l1, l2);
      
        System.out.println("Collections.disjoint(l1,l2):" + status);
    }
}

**輸出**

l1:[10, 20, 30, 40] l2:[60, 70, 80, 90]

Collections.disjoint(l1,l2):true

 时间匹配校验
 List<String> beginDate = new ArrayList<>(); 
 
 List<String> endDate = new ArrayList<>(); 
 
beginDate.add(StringUtils.*isEmpty*(journeyInfo.getBeginTime()) ? "" : journeyInfo.getBeginTime()); 

endDate.add(StringUtils.*isEmpty*(journeyInfo.getEndTime()) ? "" : journeyInfo.getEndTime());
 
List<LocalDate> beginLocalDate = beginDate.stream().filter(d -> !StringUtils.isEmpty(d)).map(d -> LocalDate.parse(d, DateTimeFormatter.ofPattern("yyyy-MM-dd"))).collect(Collectors.toList());

List<LocalDate> endLocalDate = endDate.stream().filter(d -> !StringUtils.isEmpty(d)).map(d -> LocalDate.parse(d, DateTimeFormatter.ofPattern("yyyy-MM-dd"))).collect(Collectors.toList());
beginLocalDate.addAll(endLocalDate);

LocalDate maxLocalDate = Collections.max(beginLocalDate);
LocalDate minLocalDate = Collections.min(beginLocalDate);
if (journeyDateControl>0) {
    maxLocalDate = maxLocalDate.plusDays(journeyDateControl);
    minLocalDate = minLocalDate.minusDays(journeyDateControl);
}
LocalDate now = LocalDate.now();
if (!(now.isBefore(minLocalDate) || now.isAfter(maxLocalDate))) {
    matchInfo.setIsTimeMatch(1);
}