Class类getResource方法获取文件路径

Class类getResource方法获取文件路径,第1张

在应用程序中,经常有需求获取资源文件路径,并加载之。

其中一个方法就是使用绝对路径,比如:

但是不支持跨系统,linux上是不用C盘D盘的。

要做到像JVM一样牛逼,跨系统实现,需要使用绝对路径。

Class类提供的getResource方法就可以实现此功能。下边梳理下getResource使用时的一些情况:

如截图中的例子,一个打印了当前类文件的包路径。一个打印了classPath所在的包路径。

例子中举例资源文件A,B,C查找的方法。

ClassgetResource和ClassgetResourceAsStream在使用时,路径选择上是一样的。后者返回的是流,方便封装工具类处理。

从结果上看TestSourceclassgetResource("/") == TestSourceclassgetClassLoader()getResource("") 等效的

所以上述A,B,C的资源获取,可以写成如下:

参考博文:

>

使用 java 进行文件拷贝 相信很多人都会用,,不过效率上是否最好呢

java NIO 性能提升

第一种方法:古老的方式

Java代码

public static long forJava(File f1,File f2) throws Exception{

long time=new Date()getTime();

int length=2097152;

FileInputStream in=new FileInputStream(f1);

FileOutputStream out=new FileOutputStream(f2);

byte[] buffer=new byte[length];

while(true){

int ins=inread(buffer);

if(ins==-1){

inclose();

outflush();

outclose();

return new Date()getTime()-time;

}else

outwrite(buffer,0,ins);

}

}

public static long forJava(File f1,File f2) throws Exception{

long time=new Date()getTime();

int length=2097152;

FileInputStream in=new FileInputStream(f1);

FileOutputStream out=new FileOutputStream(f2);

byte[] buffer=new byte[length];

while(true){

int ins=inread(buffer);

if(ins==-1){

inclose();

outflush();

outclose();

return new Date()getTime()-time;

}else

outwrite(buffer,0,ins);

}

}

方法的2参数分别是原始文件,和拷贝的目的文件这里不做过多介绍

实现方法很简单,分别对2个文件构建输入输出流,并且使用一个字节数组作为我们内存的缓存器, 然后使用流从f1 中读出数据到缓存里,在将缓存数据写到f2里面去这里的缓存是2MB的字节数组

第2种方法:使用NIO中的管道到管道传输

Java代码

public static long forTransfer(File f1,File f2) throws Exception{

long time=new Date()getTime();

int length=2097152;

FileInputStream in=new FileInputStream(f1);

FileOutputStream out=new FileOutputStream(f2);

FileChannel inC=ingetChannel();

FileChannel outC=outgetChannel();

int i=0;

while(true){

if(inCposition()==inCsize()){

inCclose();

outCclose();

return new Date()getTime()-time;

}

if((inCsize()-inCposition())<20971520)

length=(int)(inCsize()-inCposition());

else

length=20971520;

inCtransferTo(inCposition(),length,outC);

inCposition(inCposition()+length);

i++;

}

}

public static long forTransfer(File f1,File f2) throws Exception{

long time=new Date()getTime();

int length=2097152;

FileInputStream in=new FileInputStream(f1);

FileOutputStream out=new FileOutputStream(f2);

FileChannel inC=ingetChannel();

FileChannel outC=outgetChannel();

int i=0;

while(true){

if(inCposition()==inCsize()){

inCclose();

outCclose();

return new Date()getTime()-time;

}

if((inCsize()-inCposition())<20971520)

length=(int)(inCsize()-inCposition());

else

length=20971520;

inCtransferTo(inCposition(),length,outC);

inCposition(inCposition()+length);

i++;

}

}

实现方法:在第一种实现方法基础上对输入输出流获得其管道,然后分批次的从f1的管道中像f2的管道中输入数据每次输入的数据最大为2MB

方法3:内存文件景象写(读文件没有使用文件景象,有兴趣的可以回去试试,,我就不试了,估计会更快)

Java代码

public static long forImage(File f1,File f2) throws Exception{

long time=new Date()getTime();

int length=2097152;

FileInputStream in=new FileInputStream(f1);

RandomAccessFile out=new RandomAccessFile(f2,"rw");

FileChannel inC=ingetChannel();

MappedByteBuffer outC=null;

MappedByteBuffer inbuffer=null;

byte[] b=new byte[length];

while(true){

if(inCposition()==inCsize()){

inCclose();

outCforce();

outclose();

return new Date()getTime()-time;

}

if((inCsize()-inCposition())<length){

length=(int)(inCsize()-inCposition());

}else{

length=20971520;

}

b=new byte[length];

inbuffer=inCmap(MapModeREAD_ONLY,inCposition(),length);

inbufferload();

inbufferget(b);

outC=outgetChannel()map(MapModeREAD_WRITE,inCposition(),length);

inCposition(blength+inCposition());

outCput(b);

outCforce();

}

}

public static long forImage(File f1,File f2) throws Exception{

long time=new Date()getTime();

int length=2097152;

FileInputStream in=new FileInputStream(f1);

RandomAccessFile out=new RandomAccessFile(f2,"rw");

FileChannel inC=ingetChannel();

MappedByteBuffer outC=null;

MappedByteBuffer inbuffer=null;

byte[] b=new byte[length];

while(true){

if(inCposition()==inCsize()){

inCclose();

outCforce();

outclose();

return new Date()getTime()-time;

}

if((inCsize()-inCposition())<length){

length=(int)(inCsize()-inCposition());

}else{

length=20971520;

}

b=new byte[length];

inbuffer=inCmap(MapModeREAD_ONLY,inCposition(),length);

inbufferload();

inbufferget(b);

outC=outgetChannel()map(MapModeREAD_WRITE,inCposition(),length);

inCposition(blength+inCposition());

outCput(b);

outCforce();

}

}

实现方法:跟伤2个例子不一样,这里写文件流没有使用管道而是使用内存文件映射(假设文件f2在内存中)在循环中从f1的管道中读取数据到字节数组里,然后在像内存映射的f2文件中写数据

第4种方法:管道对管道

Java代码

public static long forChannel(File f1,File f2) throws Exception{

long time=new Date()getTime();

int length=2097152;

FileInputStream in=new FileInputStream(f1);

FileOutputStream out=new FileOutputStream(f2);

FileChannel inC=ingetChannel();

FileChannel outC=outgetChannel();

ByteBuffer b=null;

while(true){

if(inCposition()==inCsize()){

inCclose();

outCclose();

return new Date()getTime()-time;

}

if((inCsize()-inCposition())<length){

length=(int)(inCsize()-inCposition());

}else

length=2097152;

b=ByteBufferallocateDirect(length);

inCread(b);

bflip();

outCwrite(b);

outCforce(false);

}

}

注:参数中的File可以这样定义:

FIle f1 = new File("C:\\testdat");

File f2 = new File("D:\\testdat");

以上就是关于Class类getResource方法获取文件路径全部的内容,包括:Class类getResource方法获取文件路径、servlet-api.java怎么复制到c盘根目录下、JAVA编写程序,把C盘根目录的文本test.dat复制到D盘根目录等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

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

原文地址: https://outofmemory.cn/web/9596272.html

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

发表评论

登录后才能评论

评论列表(0条)

保存