this.getClass()とgetResourceAsStream()の本質クラスのロード


(1).新FileInputStream(「a.txt」)では、このa.txtは工事のルートディレクトリの下にあるはずです.例えばtest_files/wdag0437ir.zip
(2).this.getClass().getClassLoader().getResourceAsStream(「a.txt」)もbinディレクトリの下、またはクラスをロードする場所-パッケージ外
(3).this.getClass().getResourceAsStream(「a.txt」)もbinディレクトリの下、またはクラスをロードする場所–パッケージ内
(4).this.getClass()はいったい何を得たのですか?クラスのオブジェクトです.Studentクラスのオブジェクトではありませんが、Studentもクラスです.Classも普通のクラスで、インスタンス化することもできます.このクラスオブジェクトからリソースバイトストリームが得られるのはなぜですか?1冊の本の対象から、この本の名前を得ることができて、この本がいったい何ページあるかを得ることができます.本と本の間には違いがある.1つのクラスオブジェクトと1つのクラスオブジェクトも異なり、これらのクラスの位置は必然的に異なります.getResourceAsStream:その本質は、クラスの位置に応じて、プロファイルの位置をロードすることです.テスト時にどのように使用するか、相対的なパスを直接使用するかが便利です.
 
1. getClass().getResourceAsStreamアプリケーション
 
import java.applet.Applet;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Image;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.Properties;

public class Prop11 extends Applet {
  Image im;
  Font f;
  String msg;

  public void paint(Graphics g) {
    g.setFont(f);
    if (im != null)
      g.drawImage(im, 50, 100, this);
    if (msg != null)
      g.drawString(msg, 50, 50);
  }

  public void init() {
    InputStream is = getClass().getResourceAsStream("prop11.list");
    Properties p = new Properties();
    try {
      p.load(is);
      f = Font.decode(p.getProperty("MyProg.font"));
      msg = p.getProperty("MyProg.message");
      String name = p.getProperty("MyProg.image");
      URL url = getClass().getResource(name);
      im = getImage(url);
    } catch (IOException e) {
      System.out.println("error loading props...");
    }
  }
}

File: prop11.list

MyProg.font.size=20
MyProg.font.type=italic-bold
MyProg.font.name=Helvetica
MyProg.message=Hello World


  2.クラスパスに1つ以上のアイテムを追加
 
import java.util.Properties;

/**
 * Try adding one or more item(s) to class path.
 */
public class SysPropSet {

  static Properties p = System.getProperties();

  public static void main(String[] argv) {
    System.out.println("System Properties:");
    System.out.println("java.class.path now = " + getClassPath());
    p.setProperty("java.class.path", getClassPath() + ';'
        + "C:/jdk1.2/lib/tools.jar");
    System.out.println("java.class.path now = " + getClassPath());
    try {
      Class.forName("sun.tools.javap.JavaP");
    } catch (Exception e) {
      System.err.println(e);
      return;
    }
    System.out.println("Got it!!");
  }

  static String getClassPath() {
    return p.getProperty("java.class.path", null);
  }
}


  3.≪リソース・ファイルのロード|Load Resource File|ldap≫:≪絶対的にクラスからのパス|Absolute From Class Path|ldap≫
 
import java.io.IOException;
import java.net.URL;

public class Main {
  public static void main(String[] args) throws IOException {
    // absolute from the classpath
    URL url = Main.class.getResource("/mypackage/foo.txt");
  }
}


  4.≪ロード・リソース・ファイル|Load Resource File|oem_src≫:クラスに対する場所
 
import java.io.IOException;
import java.net.URL;

public class Main {
  public static void main(String[] args) throws IOException {
    // relative to the class location
    URL url = Main.class.getResource("foo.txt");
  }
}


 
  5.相対ファイルロードクラスパス
import java.io.IOException;
import java.net.URL;

public class Main {
  public static void main(String[] args) throws IOException {
    // relative document
    URL url = Main.class.getResource("docs/bar.txt");
  }
}