「javacopy类」java beancopy
今天给各位分享javacopy类的知识,其中也会对java beancopy进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!
本文目录一览:
- 1、求java copy类
- 2、用Copy.java编译生成的Copy类,将前面生成的文件复制一个新文件.
- 3、java中怎么实现实体类的拷贝
- 4、java 编写FileCopy类,要求将1个文件的内容同时复制成多个文件.使用命令行完成文件名的输入。
- 5、利用JAVA语言编写一个 名为copy的程序 实现文件的拷贝功能,应该怎样做?
求java copy类
你的目的是什么?是类的复制吗?建议你去看看 Object里有一个方法,clone()方法。
创建并返回此对象的一个副本,一般情况下:
x.clone().equals(x)为 true;
用Copy.java编译生成的Copy类,将前面生成的文件复制一个新文件.
public void copyFile(String src,String dest) throws IOException...{
FileInputStream in=new FileInputStream(src);
File file=new File(dest);
if(!file.exists())
file.createNewFile();
FileOutputStream out=new FileOutputStream(file);
int c;
byte buffer[]=new byte[1024];
while((c=in.read(buffer))!=-1)...{
for(int i=0;ic;i++)
out.write(buffer[i]);
}
in.close();
out.close();
}
别地方粘来的;你看是不是你要的
java中怎么实现实体类的拷贝
Java实现文件拷贝其实质上就是使用java提供的三种文件流操作,字节流,字符流,二进制流。
字节流:FileInputStream 与 FileOutputStream
使用示例:
void copyFile(File oldFile, File newFile){
FileOutputStream outputStream = new FileOutputStream(newFile,true);
FileInputStream inputStream = new FileInputStream(oldFile);
byte []wxj = new byte[1024];
int length = inputStream.read(wxj);
while(length!=-1){
outputStream.write(wxj,0,length);
length = inputStream.read(wxj);
}
}
字符流:FileReader 和 FileWriter
使用示例:
void copyFile(File oldFile, File newFile){
Writer writer = new FileWriter(newFile,true);
Reader reader = new FileReader(oldFile);
char []wxj = new char[1024];
int length = reader.read(wxj);
while(length!=-1){
writer.write(wxj,0,length);
length = reader.read(wxj);
}
}
二进制流:DataInputStream 和 DataOutputStream
使用示例:
void copyFile(File oldFile, File newFile){
FileOutputStream outputStream = new FileOutputStream(newFile,true);
FileInputStream inputStream = new FileInputStream(oldFile);
DataInputStream dataInput = new DataInputStream(inputStream);
DataOutputStream dataOutput = new DataOutputStream(outputStream);
byte []wxj = new byte[1024];
int length = dataInput.read(wxj);
while(length!=-1){
dataOutput.write(wxj,0,length);
length = dataInput.read(wxj);
}
}
总结一下:字节流读取文件的单位为字节,对于英语字母(只占一个字节)不受任何影响,而对于中文文字在unicode编码为两个字节(或者以上?)则可能会造成影响;字符流读取文件的单位为字符,没有上述字节流的弊端,而且其提供缓冲区读取/写入,更加方便与高效;二进制流本质上也属于字节流,但是它在读取/写入文件时把文件内容转化为二进制的方式读取/写入,不易出错而且极为高效,一般用于读取/写入视频等大文件信息。
java 编写FileCopy类,要求将1个文件的内容同时复制成多个文件.使用命令行完成文件名的输入。
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.nio.channels.FileChannel;
import java.util.Date;
import java.util.Scanner;
public class FileCopy {
public static void main(String[] args) throws Exception {
File f1 = new File("D:\\test\\test.txt");
String path = "D:\\test\\";
System.out.print("请输入要复制的文件个数:");
Scanner sc = new Scanner(System.in);
int cnt = sc.nextInt();
for(int i = 0 ; i cnt ; i++){
System.out.print("请输入第"+(i+1)+"个文件名:");
String newName = sc.next();
System.out.println("第"+(i+1)+"个文件的名字为:"+newName+".txt");
File f2 = new File(path+newName+".txt");
forTransfer(f1,f2);
}
}
/**
* @author Samsung
* @date 2017年4月20日15:20:25
* 实现文件内容的复制
*
* */
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++;
}
}
}
利用JAVA语言编写一个 名为copy的程序 实现文件的拷贝功能,应该怎样做?
import java.io.File;\x0d\x0aimport java.io.FileInputStream;\x0d\x0aimport java.io.FileNotFoundException;\x0d\x0aimport java.io.FileOutputStream;\x0d\x0aimport java.io.IOException;\x0d\x0apublic class Copy {\x0d\x0a/**\x0d\x0a* @param args\x0d\x0a*/\x0d\x0apublic static void main(String[] args) {\x0d\x0a// TODO Auto-generated method stub\x0d\x0aif(args.length!=2){\x0d\x0aSystem.out.print("没有输入正确数目的参数,程序退出!");\x0d\x0aSystem.exit(0);\x0d\x0a}\x0d\x0aFile fileS = new File("./"+args[0]);\x0d\x0aFile fileD = new File("./"+args[1]);\x0d\x0aif(fileD.exists())System.out.println("目标文件 "+args[1]+" 已存在!");\x0d\x0abyte[] temp = new byte[50];\x0d\x0aint totalSize = 0;\x0d\x0atry {\x0d\x0aFileInputStream fr = new FileInputStream(fileS);\x0d\x0aFileOutputStream fo = new FileOutputStream(fileD);\x0d\x0aint length = 0;\x0d\x0awhile((length = fr.read(temp, 0, temp.length)) != -1){\x0d\x0atotalSize += length;\x0d\x0afo.write(temp, 0, length);\x0d\x0a}\x0d\x0aSystem.out.println("文件 "+args[0]+" 有 "+totalSize+" 个字节");\x0d\x0aSystem.out.println("复制完成!");\x0d\x0a} catch (FileNotFoundException e) {\x0d\x0a// TODO Auto-generated catch block\x0d\x0ae.printStackTrace();\x0d\x0aSystem.out.println("源文件 "+args[0]+" 不存在!");\x0d\x0a} catch (IOException e) {\x0d\x0a// TODO Auto-generated catch block\x0d\x0ae.printStackTrace();\x0d\x0a}\x0d\x0a}\x0d\x0a}
javacopy类的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于java beancopy、javacopy类的信息别忘了在本站进行查找喔。