java我想要写某个文件,怎样获取文件的相对路径。文件位置:

java我想要写某个文件,怎样获取文件的相对路径。文件位置:,第1张

package comhmilyldexp;

import javaioFile;

public class ListFile {

private long[] count = new long[] ;

private File file;

private long[] listFile(String path) {

file = new File(path);

File[] f = filelistFiles();

for (int i = 0; i < flength; i++) {

if (f[i]isDirectory()) {

count[0]++;

thislistFile(f[i]getPath());

} else {

count[1]++;

}

}

return count;

}

/

得到指定路径下的文件和文件夹数量

@param path

要查看的路径

@return object[0]耗时(毫秒)<br>

object[1]文件夹数量<br>

object[2]文件数量

/

public Object[] getFileCount(String path) {

long t = SystemcurrentTimeMillis();

long[] count = thislistFile(path);

t = SystemcurrentTimeMillis() - t;

Object[] o = new Object[] { LongvalueOf(t), LongvalueOf(count[0]),

LongvalueOf(count[1])};

return o;

}

public static void main(String[] args) {

ListFile l = new ListFile();

Object[] count = lgetFileCount("d:\\");

Systemoutprintln(count[0]);

Systemoutprintln(count[1]);

Systemoutprintln(count[2]);

}

}

以前写的一个获取目录下有多少文件和多少文件夹的代码,

可以参考下:)

读取配置文件 , 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();

我觉得如果只是为了得到路径,那491064739的回答其实已经给你思路了,就是传入个File f,然后String s = fgetAbsolutePath();得到绝对路径!不过,似乎你问的是Path后,我的思路是传入两个参数,一个workspace的路径,一个是File f 。伪代码如下:

public String getPathInfo(String workspace,File file) throw Exception{

String path = filegeAbsolute(); //绝对路径

pathreplaceAll("\\\\","/"); //把\ 替换成 /

workspacereplaceAll("\\\\","/");

String info = pathsubString(workspacelength-1); //-1是留下/

//因为是绝对路径,所以文件名最前面的就是workspace,把前面那段去掉就是Path后的了

return info;

}

大致上逻辑就是这样吧

这里面我把se跟ee方面获取路径的给你列举出来了,希望对你有用

 Java中使用的路径,分为两种:绝对路径和相对路径。归根结底,Java本质上只能使用绝对路径来寻找资源。所有的相对路径寻找资源的方法,都不过是一些便利方法。不过是API在底层帮助我们构建了绝对路径,从而找到资源的!

在开发Web方面的应用时, 经常需要获取服务器中当前WebRoot的物理路径。

如果是Servlet , Action , Controller, 或者Filter , Listener , 拦截器等相关类时, 我们只需要获得ServletContext, 然后通过ServletContextgetRealPath("/")来获取当前应用在服务器上的物理地址。

如果在类中取不到ServletContext时,有两种方式可以做到:

1)利用Java的类加载机制:调用 XXXclassgetClassLoader()getResource(""); 方法来获取到ClassPath , 然后处理获得WebRoot目录。

这种方式只能是该class在WebRoot/WEB-INF/classes下才能生效, 如果该class被打包到一个jar文件中, 则该方法失效。这时就应该用下面一种方式。

2)spring框架的思路,在WEB-INF/webxml中,创建一个webAppRootKey的param,指定一个值(默认为webapproot)作为键值,然后通过Listener, 或者Filter,或者Servlet 执行String webAppRootKey = getServletContext()getRealPath("/"); 并将webAppRootKey对应的webapproot 分别作为Key,Value写到System Properties系统属性中。之后在程序中通过SystemgetProperty("webapproot")来获得WebRoot的物理路径。

根据第二种的思路,我们还可以再扩展一下。不过对于在部署在一台服务器中的应用来说,若还不是你所需请再往下看。

下面是一些得到classpath和当前类的绝对路径的一些方法。你可使用其中的一些方法来得到你需要的资源的绝对路径:

1DebitNoteActionclassgetResource("")

得到的是当前类FileTestclass文件的URI目录。不包括自己!

如:file:/D:/eclipse/springTest/WebRoot/WEB-INF/classes/

atacarnet/src/com/evi/modules/atacarnet/action/

2DebitNoteActionclassgetResource("/")

得到的是当前的classpath的绝对URI路径。

如:file:/D:/eclipse/springTest/WebRoot/WEB-INF/classes/

3ThreadcurrentThread()getContextClassLoader()getResource("")

得到的也是当前ClassPath的绝对URI路径

如:file:/D:/eclipse/springTest/WebRoot/WEB-INF/classes/

推荐使用该方法获取。

4DebitNoteActionclassgetClassLoader()getResource("") 或ClassLoadergetSystemResource("")

得到的也是当前ClassPath的绝对URI路径。

如:file:/D:/eclipse/springTest/WebRoot/WEB-INF/classes/

5取得服务器相对路径

SystemgetProperty("userdir")

例如:E:\apache-tomcat-5516\apache-tomcat-5516\bin

6取得项目中的绝对路径

一般用requestgetRealPath("/")或requestgetRealPath("/config/")

但现在不提倡使用requestgetRealPath("/")了,大家可试用ServletContextgetRealPath("/")方法得到Web应用程序的根目录的绝对路径。

要取得src的文件非常容易,因为src是默认的相对目录,比如你说要取得src下com目录的testjava文件,你只需要这样就够了

File f = new File(com/testjava);

但如果我要取得不在src目录或者WebRoot目录下的文件呢,而是要从src或者WebRoot同级的目录中取呢,比如说doc吧。

我的硬方法是这样实现的:

String path = thisgetServletContext()getRealPath("/");

Properties p = new Properties();

pload(new FileInputStream(new File(pathsubstring(0,(pathlastIndexOf("\\WebRoot") + 1)) + "doc/dbproperties")));

Systemoutprintln(pgetProperty("driverName"));

-------------------------------

另:Request中getContextPath、getServletPath、getRequestURI、getRequestURL、getRealPath的区别

假定你的web application 名称为news,你在浏览器中输入请求路径:>

//根据你的要求修改了一下代码,现在已经能将某文件夹下的所有指定类型文件复制到

//指定文件夹下了

import javaioBufferedInputStream;

import javaioBufferedOutputStream;

import javaioFile;

import javaioFileInputStream;

import javaioFileOutputStream;

public class ReadFiles {

public static final String FILTER = "xml";

public static final String SRC_DIR = "E:\\StudyData";// 待扫描的文件夹

public static final String DES_DIR = "E:\\testdata";// 复制后的目标文件夹

public static void main(String[] args) {

long a = SystemcurrentTimeMillis();

scanDir(SRC_DIR, DES_DIR);

Systemoutprintln("共花费时间:"+(SystemcurrentTimeMillis() - a)/1000+"秒");

}

public static void scanDir(String srcPath, String desPath) {

File dir = new File(srcPath);

File[] files = dirlistFiles();

if (files == null)

return;

for (int i = 0; i < fileslength; i++) {

if (files[i]isDirectory()) {

scanDir(files[i]getAbsolutePath(), desPath);

} else {

String strFileName = files[i]getAbsolutePath()toLowerCase();

copyFile(strFileName, desPath + files[i]getName());

}

}

}

public static void copyFile(String srcName, String destName) {

if (srcNameendsWith(FILTER)) {

Systemoutprintln("正在复制文件 "+srcName+" 至 "+destName);

try {

BufferedInputStream in = new BufferedInputStream(

new FileInputStream(srcName));

BufferedOutputStream out = new BufferedOutputStream(

new FileOutputStream(destName));

int i = 0;

byte[] buffer = new byte[2048];

while ((i = inread(buffer)) != -1) {

outwrite(buffer, 0, i);

}

outclose();

inclose();

} catch (Exception ex) {

exprintStackTrace();

}

}

}

}

以上就是关于java我想要写某个文件,怎样获取文件的相对路径。文件位置:全部的内容,包括:java我想要写某个文件,怎样获取文件的相对路径。文件位置:、Java 获取配置文件路径、java 获取当前文件的路径,路径全名等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

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

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

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

发表评论

登录后才能评论

评论列表(0条)

    保存