java编写程序,把c盘根目录的文本test.dat复制到d盘根目录

author author     2023-04-16     564

关键词:

使用 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=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");
参考技术A 代码没有给你个思路:
1,创建2个文件操作流一个读,一个写。
2,先创建要生成的文件,然后开始循环读源文件,然后每次将读到的数据写到生成的文件里去。
3,关闭2个文件流。
参考技术B import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;

public class CopyFile
public static void main(String[] args) throws FileNotFoundException, IOException
Copy("D://a.java","D://text.txt");//第一个是目标文件的目录,第二个是你想拷贝的目录


public static void Copy(String F,String D) throws IOException, FileNotFoundException
String file = F;
BufferedReader in = new BufferedReader(new InputStreamReader(
new FileInputStream(file), "GBK"));

PrintWriter w = new PrintWriter(new OutputStreamWriter(
new FileOutputStream(D), "utf-8"));
// .readLine(); 每次读取一行, 到 \n,\r为止
// 如果到文件末尾, 返回null
String str;
while ((str = in.readLine()) != null)
System.out.println(str);
w.write(str + "\n");

in.close();
w.close();

文件的基本操作---复制/移动或删除文件

复制/移动或删除文件File类的Copy/Move方法1.将C盘根目录下的Text.txt文本文件复制到D盘根目录下File.Copy("C:\Text.txt","D:\Text.txt");2.将C盘根目录下的Text.txt文本文件移动到D盘根目录下File.Move("C:\Text.txt","D:\Text.txt");3.删除指定的文件File.Del... 查看详情

挣扎于范围存储 - 我如何在 Java 的应用程序目录中编写纯文本文件?

】挣扎于范围存储-我如何在Java的应用程序目录中编写纯文本文件?【英文标题】:StrugglingwithScopedStorage-howwouldIwriteaplaintextfileinappdirectoryinJava?【发布时间】:2022-01-0901:36:00【问题描述】:我正在将我们的应用程序迁移到API版本30... 查看详情

bat命令行是啥??

...而批处理的能力主要取决于你所使用的命令。第三,每个编写好的批处理文件都相当于一个DOS的外部命令,你可以把它所在的目录放到你的DOS搜索路径(path)中来使得它可以在任意位置运行。一个良好的习惯是在硬盘上建立一个bat... 查看详情

使用eclipse编写和运行java程序(基础)

1.首先java程序的运行你需要下载和安装JDK,这是java运行的必备环境。2.在桌面上找到eclipes,双击打开。3.在eclipes启动的过程中,会弹出一个窗口,让你填写java工作区的保存目录,在这个目录下会保存你写的所有的源代码文件,... 查看详情

java里绝对路径和相对路径的区别是啥?

...成”Example.class”文件.我们在调用”javaExample”来运行该程序.此时我们已经启动了一个jvm,这个jvm是在d盘根目录下被启动的,所以此jvm所加载的程序中File类的相对路径也就是相对这个路径的,即d盘根目录:D:/.同时”当前用户目录”... 查看详情

怎样运行自己编好的java小程序?

参考技术A怎样运行自己编好的JAVA小程序?写个DOS批处理,javacNotepad.JavajavaNotepad~~~~~~~~~~~~~~~~~~~~怎样才能运行JAVA小程序?JDK+记事本是最简单的不过JDK需要配置环境变量之类的要想方便的话用Myeclipse参考下载地址:chinesedocument.kaifag... 查看详情

java示例代码_指导java编写的程序在另一个程序中编写文本

java示例代码_指导java编写的程序在另一个程序中编写文本 查看详情

有java编写一个输出“helloworld!”的applet程序和嵌入该applet的hyml页面

...C:\ghq\>javacHelloWorld.java<Enter>  注意:如果编写的源程序违反了Java编程语言的语法规则,Java编译器将在屏幕上显示语法错误提示信息。源文件中必须不含任何语法错误,Java编译器才能成功地把源程序转换为appletviewe... 查看详情

怎么用codeblocks编写c语言的图形程序

...codeblocks中,可以通过集成EGE库,来实现C语言图形程序的编写,具体方式如下:一、安装:1、下载ege安装包;2、将安装包解压;3、把压缩包里include目录下所有文件,复制到编译器安装目录下的include目录内,例如D:\\MinGW\\include\\... 查看详情

c盘目录下的文件夹

...下载软件时把路径改到其他硬盘分区去Drivers:部分驱动程序的文件夹Favorites:收藏夹 Pro 查看详情

vs2012新建项目出错了,怎么改啊?

1、在visualstudio当中创建一个C#控制台应用程序,选择新建项目,然后选择visualC#,再选中控制台应用程序,输入项目名称,选择位置,确定即可。2、创建完成之后,在program.cs中最上方加写usingSystem.IO;,如图所示,注意后面的分... 查看详情

如何设置开始—运行—cmd;的默认路径为c盘根目录?即不需要键入cd\

我的意思是说键入cmd之后的目录就为C盘根目录,不需要键入cd\。Win+R:regedit,打开注册表,找到如下路径:“HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\CommandProcessor”右边AutoRun的字符串,并设置该字串值为CD/DC:\来改变该默认路... 查看详情

helloworld入门程序(代码片段)

...,可以开发我们第一个Java程序了。Java程序开发三步骤:编写、编译、运行。编写Java源程序在任意目录新建文本文件,文件名修改为HelloWorld.java,其中文件名为HelloWorld,后缀名必须为.java。在文件中输入文本并保存,代码如下:... 查看详情

如何运行文本文档编程命令

...命令行来实现程序的执行等,那怎样在终端中利用命令行编写文本文件呢?下面就让学习啦小编教大家怎样用命令行编写文本文件吧。  用命令行编写文本文件的方法  打开虚拟机,在主文件夹里新建文件夹,比如此时新建... 查看详情

浏览器-启动本地程序(代码片段)

(未完成)使用自定义的协议打开应用程序编写程序将test.exe保存到c盘根目录#include<stdio.h>intmain(intargc,char**argv)inti;printf("helloworld!");for(i=0;i<argc;++i)printf("argv[%d]=%s",i,argv[i]);getchar();return0;注册表里添加协议test.reg保存到c盘根... 查看详情

浏览器-启动本地程序(代码片段)

(未完成)使用自定义的协议打开应用程序编写程序将test.exe保存到c盘根目录#include<stdio.h>intmain(intargc,char**argv)inti;printf("helloworld!");for(i=0;i<argc;++i)printf("argv[%d]=%s",i,argv[i]);getchar();return0;注册表里添加协议test.reg保存到c盘根... 查看详情

如何把java程序部署到tomcat里

1、最原始的做法是将java程序编译成.class文件,复制到tomcat中你的项目里的相应位置。2、现在很多开发工具可以方便的部署java程序到tomcat,比如eclipse通过add和remove来部署你的项目,通过publish来部署项目到tomcat你所设定的位置,... 查看详情

怎么把exe做成服务

...服务Instsrv.exe可以给系统安装和删除服务,Srvany.exe可以让程序以服务的方式运行。这两个软件都包含在WindowsNTResourceKit里.把这两个程序保存在一个方便的位置,例如C盘根目录下。我们举例来说明,把OE作为一个服务添加进WindowsXP... 查看详情