国产宅男网站在线|亚洲A级性爱免费视频|亚洲中精品级在线|午夜福利AA毛

  • <dd id="gf5jf"><th id="gf5jf"></th></dd>

    <cite id="gf5jf"><label id="gf5jf"></label></cite>
  • <div id="gf5jf"><listing id="gf5jf"></listing></div>
    學(xué)習(xí)啦 > 知識(shí)大全 > 知識(shí)百科 > 百科知識(shí) > java什么是包

    java什么是包

    時(shí)間: 歐東艷656 分享

    java什么是包

      “包”(Packet)是TCP/IP協(xié)議通信傳輸中的數(shù)據(jù)單位,一般也稱“數(shù)據(jù)包”。

      包是用來(lái)分類(lèi)管理類(lèi)文件的,包相當(dāng)于文件夾,類(lèi)相當(dāng)于文件。

    java中的包:

      [java] view plaincopypackage com.itkt.mtravel.hotel.util;

      import java.io.File;

      import java.net.URL;

      import java.net.URLClassLoader;

      import java.util.ArrayList;

      import java.util.Enumeration;

      import java.util.List;

      import java.util.jar.JarEntry;

      import java.util.jar.JarFile;

      public class PackageUtil {

      public static void main(String[] args) throws Exception {

      String packageName = "com.wang.vo.request.hotel";

      // List classNames = getClassName(packageName);

      List classNames = getClassName(packageName, false);

      if (classNames != null) {

      for (String className : classNames) {

      System.out.println(className);

      }

      }

      }

      /**

      * 獲取某包下(包括該包的所有子包)所有類(lèi)

      * @param packageName 包名

      * @return 類(lèi)的完整名稱

      */

      public static List getClassName(String packageName) {

      return getClassName(packageName, true);

      }

      /**

      * 獲取某包下所有類(lèi)

      * @param packageName 包名

      * @param childPackage 是否遍歷子包

      * @return 類(lèi)的完整名稱

      */

      public static List getClassName(String packageName, boolean childPackage) {

      List fileNames = null;

      ClassLoader loader = Thread.currentThread().getContextClassLoader();

      String packagePath = packageName.replace(".", "/");

      URL url = loader.getResource(packagePath);

      if (url != null) {

      String type = url.getProtocol();

      if (type.equals("file")) {

      fileNames = getClassNameByFile(url.getPath(), null, childPackage);

      } else if (type.equals("jar")) {

      fileNames = getClassNameByJar(url.getPath(), childPackage);

      }

      } else {

      fileNames = getClassNameByJars(((URLClassLoader) loader).getURLs(), packagePath, childPackage);

      }

      return fileNames;

      }

      /**

      * 從項(xiàng)目文件獲取某包下所有類(lèi)

      * @param filePath 文件路徑

      * @param className 類(lèi)名集合

      * @param childPackage 是否遍歷子包

      * @return 類(lèi)的完整名稱

      */

      private static List getClassNameByFile(String filePath, List className, boolean childPackage) {

      List myClassName = new ArrayList();

      File file = new File(filePath);

      File[] childFiles = file.listFiles();

      for (File childFile : childFiles) {

      if (childFile.isDirectory()) {

      if (childPackage) {

      myClassName.addAll(getClassNameByFile(childFile.getPath(), myClassName, childPackage));

      }

      } else {

      String childFilePath = childFile.getPath();

      if (childFilePath.endsWith(".class")) {

      childFilePath = childFilePath.substring(childFilePath.indexOf("\classes") + 9, childFilePath.lastIndexOf("."));

      childFilePath = childFilePath.replace("\", ".");

      myClassName.add(childFilePath);

      }

      }

      }

      return myClassName;

      }

      /**

      * 從jar獲取某包下所有類(lèi)

      * @param jarPath jar文件路徑

      * @param childPackage 是否遍歷子包

      * @return 類(lèi)的完整名稱

      */

      private static List getClassNameByJar(String jarPath, boolean childPackage) {

      List myClassName = new ArrayList();

      String[] jarInfo = jarPath.split("!");

      String jarFilePath = jarInfo[0].substring(jarInfo[0].indexOf("/"));

      String packagePath = jarInfo[1].substring(1);

      try {

      JarFile jarFile = new JarFile(jarFilePath);

      Enumeration entrys = jarFile.entries();

      while (entrys.hasMoreElements()) {

      JarEntry jarEntry = entrys.nextElement();

      String entryName = jarEntry.getName();

      if (entryName.endsWith(".class")) {

      if (childPackage) {

      if (entryName.startsWith(packagePath)) {

      entryName = entryName.replace("/", ".").substring(0, entryName.lastIndexOf("."));

      myClassName.add(entryName);

      }

      } else {

      int index = entryName.lastIndexOf("/");

      String myPackagePath;

      if (index != -1) {

      myPackagePath = entryName.substring(0, index);

      } else {

      myPackagePath = entryName;

      }

      if (myPackagePath.equals(packagePath)) {

      entryName = entryName.replace("/", ".").substring(0, entryName.lastIndexOf("."));

      myClassName.add(entryName);

      }

      }

      }

      }

      } catch (Exception e) {

      e.printStackTrace();

      }

      return myClassName;

      }

      /**

      * 從所有jar中搜索該包,并獲取該包下所有類(lèi)

      * @param urls URL集合

      * @param packagePath 包路徑

      * @param childPackage 是否遍歷子包

      * @return 類(lèi)的完整名稱

      */

      private static List getClassNameByJars(URL[] urls, String packagePath, boolean childPackage) {

      List myClassName = new ArrayList();

      if (urls != null) {

      for (int i = 0; i < urls.length; i++) {

      URL url = urls[i];

      String urlPath = url.getPath();

      // 不必搜索classes文件夾

      if (urlPath.endsWith("classes/")) {

      continue;

      }

      String jarPath = urlPath + "!/" + packagePath;

      myClassName.addAll(getClassNameByJar(jarPath, childPackage));

      }

      }

      return myClassName;

      }

      }

    245718