资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

java传目录代码 java目录存放java源代码

JAVA编写程序,把C盘根目录的文本test.dat复制到D盘根目录

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

渑池ssl适用于网站、小程序/APP、API接口等需要进行数据传输应用场景,ssl证书未来市场广阔!成为创新互联的ssl证书销售渠道,可以享受市场价格4-6折优惠!如果有意向欢迎电话联系或者加微信:028-86922220(备注:SSL证书合作)期待与您的合作!

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=in.read(buffer);

if(ins==-1){

in.close();

out.flush();

out.close();

return new Date().getTime()-time;

}else

out.write(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=in.read(buffer);

if(ins==-1){

in.close();

out.flush();

out.close();

return new Date().getTime()-time;

}else

out.write(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=in.getChannel();

FileChannel outC=out.getChannel();

int i=0;

while(true){

if(inC.position()==inC.size()){

inC.close();

outC.close();

return new Date().getTime()-time;

}

if((inC.size()-inC.position())20971520)

length=(int)(inC.size()-inC.position());

else

length=20971520;

inC.transferTo(inC.position(),length,outC);

inC.position(inC.position()+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=in.getChannel();

FileChannel outC=out.getChannel();

int i=0;

while(true){

if(inC.position()==inC.size()){

inC.close();

outC.close();

return new Date().getTime()-time;

}

if((inC.size()-inC.position())20971520)

length=(int)(inC.size()-inC.position());

else

length=20971520;

inC.transferTo(inC.position(),length,outC);

inC.position(inC.position()+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=in.getChannel();

MappedByteBuffer outC=null;

MappedByteBuffer inbuffer=null;

byte[] b=new byte[length];

while(true){

if(inC.position()==inC.size()){

inC.close();

outC.force();

out.close();

return new Date().getTime()-time;

}

if((inC.size()-inC.position())length){

length=(int)(inC.size()-inC.position());

}else{

length=20971520;

}

b=new byte[length];

inbuffer=inC.map(MapMode.READ_ONLY,inC.position(),length);

inbuffer.load();

inbuffer.get(b);

outC=out.getChannel().map(MapMode.READ_WRITE,inC.position(),length);

inC.position(b.length+inC.position());

outC.put(b);

outC.force();

}

}

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=in.getChannel();

MappedByteBuffer outC=null;

MappedByteBuffer inbuffer=null;

byte[] b=new byte[length];

while(true){

if(inC.position()==inC.size()){

inC.close();

outC.force();

out.close();

return new Date().getTime()-time;

}

if((inC.size()-inC.position())length){

length=(int)(inC.size()-inC.position());

}else{

length=20971520;

}

b=new byte[length];

inbuffer=inC.map(MapMode.READ_ONLY,inC.position(),length);

inbuffer.load();

inbuffer.get(b);

outC=out.getChannel().map(MapMode.READ_WRITE,inC.position(),length);

inC.position(b.length+inC.position());

outC.put(b);

outC.force();

}

}

实现方法:跟伤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=in.getChannel();

FileChannel outC=out.getChannel();

ByteBuffer b=null;

while(true){

if(inC.position()==inC.size()){

inC.close();

outC.close();

return new Date().getTime()-time;

}

if((inC.size()-inC.position())length){

length=(int)(inC.size()-inC.position());

}else

length=2097152;

b=ByteBuffer.allocateDirect(length);

inC.read(b);

b.flip();

outC.write(b);

outC.force(false);

}

}

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

FIle f1 = new File("C:\\test.dat");

File f2 = new File("D:\\test.dat");

java 上传文件到tomcat虚拟目录下实例,tomcat配置好了,java代码应如何实现?

界面上使用这个虚拟路径,就可以输出这个图片或者文件路径的。

java中怎么把文件上传到服务器的指定路径?

文件从本地到服务器的功能,其实是为了解决目前浏览器不支持获取本地文件全路径。不得已而想到上传到服务器的固定目录,从而方便项目获取文件,进而使程序支持EXCEL批量导入数据。

java中文件上传到服务器的指定路径的代码:

在前台界面中输入:

form method="post" enctype="multipart/form-data"  action="../manage/excelImport.do"

请选文件:input type="file"  name="excelFile"

input type="submit" value="导入" onclick="return impExcel();"/

/form

action中获取前台传来数据并保存

/**

* excel 导入文件

* @return

* @throws IOException

*/

@RequestMapping("/usermanager/excelImport.do")

public String excelImport(

String filePath,

MultipartFile  excelFile,HttpServletRequest request) throws IOException{

log.info("action:{} Method:{} start","usermanager","excelImport" );

if (excelFile != null){

String filename=excelFile.getOriginalFilename();

String a=request.getRealPath("u/cms/www/201509");

SaveFileFromInputStream(excelFile.getInputStream(),request.getRealPath("u/cms/www/201509"),filename);//保存到服务器的路径

}

log.info("action:{} Method:{} end","usermanager","excelImport" );

return "";

}

/**

* 将MultipartFile转化为file并保存到服务器上的某地

*/

public void SaveFileFromInputStream(InputStream stream,String path,String savefile) throws IOException

{    

FileOutputStream fs=new FileOutputStream( path + "/"+ savefile);

System.out.println("------------"+path + "/"+ savefile);

byte[] buffer =new byte[1024*1024];

int bytesum = 0;

int byteread = 0;

while ((byteread=stream.read(buffer))!=-1)

{

bytesum+=byteread;

fs.write(buffer,0,byteread);

fs.flush();

}

fs.close();

stream.close();

}

java如何拷贝文件到另一个目录下

下面列举出4种方式:

1、使用FileStreams复制

这是最经典的方式将一个文件的内容复制到另一个文件中。 使用FileInputStream读取文件A的字节,使用FileOutputStream写入到文件B。正如你所看到的我们执行几个读和写操作try的数据,所以这应该是一个低效率的,下一个方法我们将看到新的方式。 这是第一个方法的代码:

2、使用FileChannel复制

Java NIO包括transferFrom方法,根据文档应该比文件流复制的速度更快。 这是第二种方法的代码:

3、使用Commons IO复制

Apache Commons IO提供拷贝文件方法在其FileUtils类,可用于复制一个文件到另一个地方。它非常方便使用Apache Commons FileUtils类时,您已经使用您的项目。基本上,这个类使用Java NIO FileChannel内部。 这是第三种方法的代码:

4、使用Java7的Files类复制

如果你有一些经验在Java 7中你可能会知道,可以使用复制方法的Files类文件,从一个文件复制到另一个文件。 这是第四个方法的代码:


文章题目:java传目录代码 java目录存放java源代码
链接URL:http://cdkjz.cn/article/ddocgpe.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

业务热线:400-028-6601 / 大客户专线   成都:13518219792   座机:028-86922220