شاركت هذه المقالة معك كيفية الحصول على مسارات مختلفة في جافا للرجوع إليها. المحتوى المحدد كما يلي
package com.ygh.blog.realpath ؛ استيراد java.io.file ؛ استيراد java.io.ioException ؛ استيراد java.io.inputstream ؛ استيراد java.net.url ؛ استيراد java.util.properties ؛/** تقع*/ test public void fun1 () {file file = new file (this.getClass (). getResource ("/"). getPath ()) ؛ // d:/project/taotaoshop/src/blog-mybatis1/target/test classes system.out.println (file) ؛ } / *** احصل على المسار المطلق إلى الفئة الحالية* / test public void fun2 () {file file = new file (this.getClass (). getResource (""). getPath ()) ؛ // d:/project/taotaoshop/src/blog-mybatis1/target/test classes/com/ygh/blog/realpath system.out.println (file) ؛ } /*** احصل على مسار المشروع حيث يوجد الفصل الحالي. يمكن استخدام كلتا الطريقتين * * throws ioException */ test public void fun3 () يلقي ioException {file file = file new ("") ؛ string path = file.getCanonicalPath () ؛ // D:/project/taotaoshop/src/blog-mybatis1 system.out.println (path) ؛ // d:/project/taotaoshop/src/blog-mybatis1 system.out.println (system.getProperty ("user.dir")) ؛ } / *** احصل على مسار الملف أسفل SRC* / test public void fun4 () {url url = this.getClass (). getClassLoader (). System.out.println (url) ؛ } / *** احصل على مسار الملف أسفل حزم الشفرة المصدر الأخرى* / test public void fun5 () {// استخدم هذه الطريقة للحصول على url url url path = this.getClass (). getClassLoader (). // file:/d: /project/taotaoshop/src/blog-mybatis1/target/classes/test.txt system.out.println (url) ؛ } test public void fun6 () يلقي استثناء {url url = this.getClass (). getClassLoader (). getResource ("test2.txt") ؛ System.out.println (url.getPath ()) ؛ خصائص الخصائص = خصائص جديدة () ؛ // استخدم هذه الطريقة للحصول على دفق الإخراج المقابل للملف InputStream inputStream = this.getClass (). getClassloader (). properties.load (inputStream) ؛ ملف الملف = ملف جديد (url.getPath ()) ؛ system.out.println (properties.get ("jdbc.driverClassName")) ؛ }}فيما يلي مسار الملف المقابل للرمز
ما سبق هو كل محتوى هذه المقالة. آمل أن يكون ذلك مفيدًا لتعلم الجميع وآمل أن يدعم الجميع wulin.com أكثر.