{"id":107650,"date":"2020-05-29T07:36:50","date_gmt":"2020-05-28T23:36:50","guid":{"rendered":"http:\/\/4563.org\/?p=107650"},"modified":"2020-05-29T07:36:50","modified_gmt":"2020-05-28T23:36:50","slug":"%e6%b1%82%e6%95%99%e5%a4%a7%e4%bd%ac%ef%bc%8c-java-%e8%bf%90%e8%a1%8c-jar-%e7%9a%84%e6%97%b6%e5%80%99%ef%bc%8c%e8%af%bb%e5%8f%96%e4%b8%8d%e5%88%b0%e9%85%8d%e7%bd%ae%e6%96%87%e4%bb%b6","status":"publish","type":"post","link":"http:\/\/4563.org\/?p=107650","title":{"rendered":"\u6c42\u6559\u5927\u4f6c\uff0c Java \u8fd0\u884c jar \u7684\u65f6\u5019\uff0c\u8bfb\u53d6\u4e0d\u5230\u914d\u7f6e\u6587\u4ef6"},"content":{"rendered":"<div>\n<div>\n<div>\n<h1>                  \u6c42\u6559\u5927\u4f6c\uff0c Java \u8fd0\u884c jar \u7684\u65f6\u5019\uff0c\u8bfb\u53d6\u4e0d\u5230\u914d\u7f6e\u6587\u4ef6               <\/h1>\n<p> <\/p>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : NoKey <\/span>  <span><i><\/i> 2<\/span> <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<div isfirst=\"1\"> <\/p>\n<p>\u5f53\u524d\u9879\u76ee\u6253\u5305\u6210 jar \u6267\u884c<\/p>\n<p>\u7136\u540e\u6211\u5728 jar \u540c\u76ee\u5f55\u4e0b\u653e\u4e86\u4e00\u4e2a resource \u76ee\u5f55\uff0c\u4e0b\u9762\u653e\u4e86\u914d\u7f6e\u6587\u4ef6<\/p>\n<p>\u4f7f\u7528<\/p>\n<pre><code>InputStream is = MyClass.class.getClassLoader().getResourceAsStream(\"resouce\/myfile.properties\")  <\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u5f0f\uff0c\u65e0\u6cd5\u8bfb\u53d6\u5230\u6587\u4ef6<\/p>\n<p>\u7136\u540e\uff0c\u6211\u5c1d\u8bd5\u4e86\u4e00\u4e0b<\/p>\n<pre><code>String path =  MyClass.class.getClassLoader().getResource(\"\").getPath(); <\/code><\/pre>\n<p>\u83b7\u53d6\u4e0d\u5230\u8def\u5f84<\/p>\n<p>\u8bf7\u5927\u4f6c\u6307\u70b9\u4e00\u4e0b\uff0c\u8c22\u8c22\u3002<\/p>\n<\/p><\/div>\n<div> <b>\u5927\u4f6c\u6709\u8a71\u8aaa<\/b> (<span>9<\/span>)        <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<ul>\n<li data-pid=\"1787127\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : putaozhenhaochi <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u4f60\u8fd9\u662f\u8bfb\u53d6 jar \u5305\u5916\u90e8\u914d\u7f6e\u4e86\u5427\u3002<\/p>\n<p>\u7f51\u4e0a\u627e\u7684\u4e00\u6bb5\u4ee3\u7801 \uff0c\u4f60\u53c2\u8003\u4e0b<br \/>\u8bfb\u53d6 jar \u5305\u5185\u914d\u7f6e\u6587\u4ef6\uff1a<\/p>\n<p>InputStream in = this.getClass().getClassLoader().getResourceAsStream(&#8220;\/configfilename.properties<br \/>\u8bfb\u53d6 jar \u5305\u5916\u914d\u7f6e\u6587\u4ef6\uff1a<\/p>\n<p>String filePath = System.getProperty(&#8220;user.dir&#8221;) + &#8220;\/conf\/configfilename.properties&#8221;; <br \/>InputStream in = new BufferedInputStream(new FileInputStream(filePath));                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"1787128\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : sweetsorrow211 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u5982\u679c\u662f Spring\uff1a <br \/>ClassPathResource resource = new ClassPathResource(&#8220;resouce\/myfile.json&#8221;); <br \/>File file = resource.getFile();                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"1787129\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : xiaofan2 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             InputStream is = MyClass.class.getClassLoader().getResourceAsStream(&#8220;resouce\/myfile.properties&#8221;)<\/p>\n<p>\u8fd9\u4e2a\u6539\u6210<br \/>InputStream is = MyClass.class.getClassLoader().getResourceAsStream(&#8220;\/myfile.properties&#8221;)<br \/>\u8fd9\u6837\u8bfb\u53d6\u7684\u662f\u4f60 jar \u5305\u5185 classes \u76ee\u5f55\u4e0b\u7684 myfile.properties \u6587\u4ef6                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"1787130\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : NoKey <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @putaozhenhaochi \u5b9e\u9645\u4e0a\u76ee\u524d\u5c31\u53ea\u80fd\u4f7f\u7528\u5148\u8bfb\u53d6\u6587\u4ef6\uff0c\u5728\u5f97\u5230\u6d41\u7684\u65b9\u5f0f\u3002\u4e0d\u8fc7\u6ca1\u641e\u660e\u767d\uff0c\u4e3a\u5565\u76f4\u63a5\u8fd0\u884c jar \u5f97\u4e0d\u5230 classpath                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"1787131\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : BBCCBB <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u662f Class.getResourceAsStream \u548c ClassLoader.getResourceAsStream \u7684\u533a\u522b, \u4f60\u641c\u4e00\u4e0b,\u770b\u61c2\u5c31\u80fd\u89e3\u51b3\u4e86                                                             <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"1787132\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : holddie <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             InputStream is = MyClass.class.getClassLoader().getResourceAsStream(&#8220;resouce\/myfile.properties&#8221;)<\/p>\n<p>\u5f15\u7528\u4f60\u8fd9\u53e5\u8bdd\uff0c\u4f60\u7684 resource \u5355\u8bcd\u5c11\u4e2a r                                                            <\/p><\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"1787133\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : RJH <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             Spring \u6709\u73b0\u6210\u7684\u7c7b\uff0c\u4ee5\u4e0b\u4ee3\u7801\u5c31\u53ef\u4ee5\u4e86<br \/>\/\/ \u8bfb\u53d6 resource \u76ee\u5f55\u4e0b\u7684 test.txt \u6587\u4ef6\uff0c\u6253\u6210 jar \u5305\u4e5f\u53ef\u4ee5\u8bfb\u53d6\u5230<br \/>Resource resource = new ClassPathResource(&#8220;\/test.txt&#8221;);<br \/>InputStream resourceInputStream = resource.getInputStream();                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"1787134\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : egfegdfr <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u4f60\u8fd9\u4e2a\u8bfb\u53d6\u7684\u662f jar \u5916\u7684\u914d\u7f6e\u6587\u4ef6\u3002\u9700\u8981\u6ce8\u610f\u6587\u4ef6\u8def\u5f84                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"1787135\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : falsemask <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             getResourceAsStream \u53ea\u80fd\u83b7\u53d6\u7f16\u8bd1\u540e\u7684\u8def\u5f84\u4e0b\u7684\u6587\u4ef6\u5427                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li>\n","protected":false},"excerpt":{"rendered":"<p>\u6c42\u6559\u5927\u4f6c\uff0c Java \u8fd0\u884c jar&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[],"tags":[],"_links":{"self":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/107650"}],"collection":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=107650"}],"version-history":[{"count":0,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/107650\/revisions"}],"wp:attachment":[{"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=107650"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=107650"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=107650"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}