java的File类访问MTP设备时候路径怎么写

java的File类访问MTP设备时候路径怎么写,第1张

File file = new File("D:\\123txt");

你这种不用绝对路径是不行的,

只有一个方法,在web工程启动servlet中获取到webroot路径,在servlet的init中使用String webRoot = getServletContext()getRealPath("/");获取,然后使用这webRoot变量追加路径,再new File(),这样的话要求就是,你的服务必须要启动,否则不会init,无法得到工程发布目录的相对路径

读取配置文件 , xxxproperties放在webroot/WEB-INF/classes/目录下

首先将配置文件转换成InputStream,有两种方式,原理一样,都是通过类加载器得到资源:

(1)InputStream inputStream = ThreadcurrentThread()getContextClassLoader()getResourceAsStream("xxproperties");

(2) InputStream inputStream =

thisgetClass() getClassLoader()getResourceAsStream( "xxproperties" );

调用对象的getClass()方法是获得对象当前的类类型,这部分数据存在方法区中,

而后在类类型上调用 getClassLoader()方法是得到当前类型的类加载器,我们知道在Java中所有的类都是通过加载器加载到虚拟机中的,而且类加载器之间存在父 子关系,就是子知道父,父不知道子,这样不同的子加载的类型之间是无法访问的(虽然它们都被放在方法区中),所以在这里通过当前类的加载器来加载资源也就 是保证是和类类型同一个加载器加载的。

最后调用了类加载器的getResourceAsStream()方法来加载资源。

(3) 然后加载配置文件,读取属性值

Properties prop = new Properties();

propload(input);

String value = propgetProperty("PropertyName");

inputclose();

在java中获得文件的路径在我们做上传文件 *** 作时是不可避免的。

web 上运行

1:thisgetClass()getClassLoader()getResource("/")getPath();

thisgetClass()getClassLoader()getResource("")getPath(); 得到的是 ClassPath的绝对URI路径。

如:/D:/jboss-422GA/server/default/deploy/hpwar/WEB-INF/classes/

SystemgetProperty("userdir");

thisgetClass()getClassLoader()getResource("")getPath(); 得到的是 项目的绝对路径。

如:/D:/jboss-422GA/server/default/deploy/hpwar

2:thisgetClass()getResource("/")getPath();

thisgetClass()getResource("")getPath(); 得到的是当前类 文件的URI目录。

如:/D:/jboss-422GA/server/default/deploy/hpwar/WEB-INF/classes/com/jebel/helper/

thisgetClass()getResource("")getPath(); X 不 能运行

3:ThreadcurrentThread()getContextClassLoader()getResource("/")getPath()

ThreadcurrentThread()getContextClassLoader()getResource("")getPath() 得到的是 ClassPath的绝对URI路径。

如:/D:/jboss-422GA/server/default/deploy/hpwar/WEB-INF/classes/

ThreadcurrentThread()getContextClassLoader()getResource("")getPath() 得到的是 项目的绝对路径。

如:/D:/jboss-422GA/server/default/deploy/hpwar

在本地运行中

1:thisgetClass()getClassLoader()getResource("")getPath();

thisgetClass()getClassLoader()getResource("")getPath(); 得到的是 ClassPath的绝对URI路径。

如:/D:/myProjects/hp/WebRoot/WEB-INF/classes

thisgetClass()getClassLoader()getResource("")getPath(); X 不 能运行

2:thisgetClass()getResource("")getPath();

thisgetClass()getResource("")getPath(); 得到的是当前类 文件的URI目录。

如:/D:/myProjects/hp/WebRoot/WEB-INF/classes/com/jebel/helper/

/D:/myProjects/hp/WebRoot/WEB-INF/classes/ 得到的是 ClassPath的绝对URI路径。

如:/D:/myProjects/hp/WebRoot/WEB-INF/classes

3:ThreadcurrentThread()getContextClassLoader()getResource("")getPath()

ThreadcurrentThread()getContextClassLoader()getResource("")getPath() 得到的是 ClassPath的绝对URI路径。。

如:/D:/myProjects/hp/WebRoot/WEB-INF/classes

ThreadcurrentThread()getContextClassLoader()getResource("/")getPath() X 不 能运行

最后

在Web应用程序中,我们一般通过ServletContextgetRealPath("/")方法得到Web应用程序的根目录的绝对路径。

还有requestgetContextPath(); 在Weblogic中要用requestgetServletContext()getContextPath();但如果打包成war部署到Weblogic服务器,项目内部并没有文件结构的概念,用这种方式是始终得到null,获取不到路径,目前还没有找到具体的解决方案。

如果是在纯java类中

String dirpath = SystemgetProperty("userdir"); String xmlFile = dirpath + "/WebRoot/WEB-INF/serverxml"; String fileName = dirPath + "/serverxml";

在servlet中

String dirPath = getServletContext()getRealPath( "/WEB-INF"); String xmlFile = dirPath + "/serverxml";

在jsp中

String dirPath= requestgetServletContext()getRealPath("/WEB-INF"); String xmlFile = dirPath+"serverxml";

我自己的xml文件是放在WebRoot/WEB_INF下

参考:

>

很多朋友都想知道java如何获取当前目录路径?下面就一起来了解一下吧~

1、利用SystemgetProperty()函数获取当前路径:

Systemoutprintln(SystemgetProperty("userdir"));//userdir指定了当前的路径

2、使用File提供的函数获取当前路径:

File directory = new File("");//设定为当前文件夹 try{ Systemoutprintln(directorygetCanonicalPath());//获取标准的路径 Systemoutprintln(directorygetAbsolutePath());//获取绝对路径 }catch(Exceptin e){} FilegetCanonicalPath()和FilegetAbsolutePath()大约只是对于new File("")和new File("")两种路径有所区别。 # 对于getCanonicalPath()函数,“"就表示当前的文件夹,而”“则表示当前文件夹的上一级文件夹 # 对于getAbsolutePath()函数,则不管””、“”,返回当前的路径加上你在new File()时设定的路径 # 至于getPath()函数,得到的只是你在new File()时设定的路径 比如当前的路径为 C:/test : File directory = new File("abc"); directorygetCanonicalPath(); //得到的是C:/test/abc directorygetAbsolutePath(); //得到的是C:/test/abc direcotrygetPath(); //得到的是abc File directory = new File(""); directorygetCanonicalPath(); //得到的是C:/test directorygetAbsolutePath(); //得到的是C:/test/ direcotrygetPath(); //得到的是 File directory = new File(""); directorygetCanonicalPath(); //得到的是C:/ directorygetAbsolutePath(); //得到的是C:/test/ direcotrygetPath(); //得到的是 另外:SystemgetProperty()中的字符串参数如下: SystemgetProperty()参数大全 # javaversion Java Runtime Environment version # javavendor Java Runtime Environment vendor # javavendorurl Java vendor URL # javahome Java installation directory # javavmspecificationversion Java Virtual Machine specification version # javavmspecificationvendor Java Virtual Machine specification vendor # javavmspecificationname Java Virtual Machine specification name # javavmversion Java Virtual Machine implementation version # javavmvendor Java Virtual Machine implementation vendor # javavmname Java Virtual Machine implementation name # javaspecificationversion Java Runtime Environment specification version # javaspecificationvendor Java Runtime Environment specification vendor # javaspecificationname Java Runtime Environment specification name # javaclassversion Java class format version number # javaclasspath Java class path # javalibrarypath List of paths to search when loading libraries # javaiotmpdir Default temp file path # javacompiler Name of JIT compiler to use # javaextdirs Path of extension directory or directories # osname Operating system name # osarch Operating system architecture # osversion Operating system version # fileseparator File separator ("/" on UNIX) # pathseparator Path separator (":" on UNIX) # lineseparator Line separator ("/n" on UNIX) # username User’s account name # userhome User’s home directory # userdir User’s current working directory

JAVA中获取路径 关键字: java中获取路径

1、jsp中取得路径:

以工程名为TEST为例:

(1)得到包含工程名的当前页面全路径:requestgetRequestURI() 结果:/TEST/testjsp (2)得到工程名:requestgetContextPath() 结果:/TEST (3)得到当前页面所在目录下全名称:requestgetServletPath() 结果:如果页面在jsp目录下 /TEST/jsp/testjsp (4)得到页面所在服务器的全路径:applicationgetRealPath("页面jsp") 结果:D:/resin/webapps/TEST/testjsp (5)得到页面所在服务器的绝对路径:absPath=new javaioFile(applicationgetRealPath(requestgetRequestURI()))getParent(); 结果:D:/resin/webapps/TEST

2、在类中取得路径: (1)类的绝对路径:ClassclassgetClass()getResource("/")getPath() 结果:/D:/TEST/WebRoot/WEB-INF/classes/pack/ (2)得到工程的路径:SystemgetProperty("userdir") 结果:D:/TEST

以上就是关于java的File类访问MTP设备时候路径怎么写全部的内容,包括:java的File类访问MTP设备时候路径怎么写、java web工程,读取配置文件路径问题、如何获取web应用的部署路径等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/9504392.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-04-29
下一篇2023-04-29

发表评论

登录后才能评论

评论列表(0条)

    保存