• <output id="aynwq"><form id="aynwq"><code id="aynwq"></code></form></output>

    <mark id="aynwq"><option id="aynwq"></option></mark>
  • <mark id="aynwq"><option id="aynwq"></option></mark><label id="aynwq"><dl id="aynwq"></dl></label>
  • 學(xué)習(xí)啦 > 知識大全 > 知識百科 > 百科知識 > java什么是包

    java什么是包

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

    java什么是包

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

      包是用來分類管理類文件的,包相當(dāng)于文件夾,類相當(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);

      }

      }

      }

      /**

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

      * @param packageName 包名

      * @return 類的完整名稱

      */

      public static List getClassName(String packageName) {

      return getClassName(packageName, true);

      }

      /**

      * 獲取某包下所有類

      * @param packageName 包名

      * @param childPackage 是否遍歷子包

      * @return 類的完整名稱

      */

      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)目文件獲取某包下所有類

      * @param filePath 文件路徑

      * @param className 類名集合

      * @param childPackage 是否遍歷子包

      * @return 類的完整名稱

      */

      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獲取某包下所有類

      * @param jarPath jar文件路徑

      * @param childPackage 是否遍歷子包

      * @return 類的完整名稱

      */

      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中搜索該包,并獲取該包下所有類

      * @param urls URL集合

      * @param packagePath 包路徑

      * @param childPackage 是否遍歷子包

      * @return 類的完整名稱

      */

      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 主站蜘蛛池模板: 亚洲成av人片在线看片| 国产欧美久久一区二区| 人人妻人人做人人爽| 一个人看的日本www| 精品人妻中文字幕有码在线| 成人综合激情另类小说| 很黄很色的女同性互慰小说| 国产乱码精品一区二区三区四川人| 久久精品二三区| 韩国精品一区二区三区无码视频| 日韩内射美女片在线观看网站| 国产成人无码精品一区在线观看| 久久青草免费91线频观看不卡| 黑人巨茎大战欧美白妇| 男人j进女人p视频免费观看| 女人色毛片女人色毛片中国 | 国产aaa女人十八毛片| 久久久久999| 综合色在线观看| 婷婷四房综合激情五月在线| 国产免费人成在线视频| 久久久久亚洲AV成人无码电影 | 男的把j放进女人下面视频免费| 女人高潮内射99精品| 亚洲第一色在线| 全黄大全大色全免费大片| 最近中文字幕mv手机免费高清| 国产午夜av秒播在线观看| 中国一级特黄的片子免费 | 亚洲亚洲人成综合网络| 黄瓜视频在线观看网址| 日本三级特黄在线观看| 又粗又紧又湿又爽a视频| chinese帅哥18kt| 欧美日韩亚洲一区| 国产妇女馒头高清泬20P多毛| 久久99热精品| 男高中生大粗吊gvlive| 国产精品夜间视频香蕉| 亚洲精品中文字幕无码蜜桃| 波多野结衣xfplay在线观看|