复制文件(夹)
来源:互联网 发布:英语翻译软件那个好 编辑:程序博客网 时间:2024/04/28 22:29
FileInputStream fis = new FileInputStream("D:\\test.jpg");
FileOutputStream fos = new FileOutputStream("D:\\test1.jpg");
int n = fis.available();
byte[]file = new byte[n];
fis.read(file);
fos.write(file);
fis.close();
fos.close();
2、按字节读取
FileInputStream fis = new FileInputStream("D:\\test.jpg");
FileOutputStream fos = new FileOutputStream("D:\\test1.jpg");
int c;
while((c=fis.read())!=-1){
fos.write(c);
}
fis.close();
fos.close();
FileInputStream fis = new FileInputStream("D:\\test.jpg");
FileOutputStream fos = new FileOutputStream("D:\\test1.jpg");
byte []b = new byte[128];
int len;
while((len=fis.read(b))!=-1){
fos.write(b,0,len);
}
fis.close();
fos.close();
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.channels.FileChannel;
publicclass test1{
publicvoid fileCopy(String inFileName,String outFileName) throws IOException{
File in = new File(inFileName);
File out = new File(outFileName);
//路径不存在时创建路径
if(out.getParentFile()!=null){
out.getParentFile().mkdirs();
}
FileChannel inChannel = new FileInputStream( in ).getChannel();
FileChannel outChannel = new FileOutputStream( out ).getChannel();
try{
//一次性全部复制
inChannel.transferTo(0, inChannel.size(), outChannel);
}
catch (IOException e) {
throw e;
}
finally{
if ( inChannel != null ){
inChannel.close();
}
if ( outChannel != null ){
outChannel.close();
}
}
}
publicstaticvoid main(String[]args) throws IOException{
test1 test = new test1();
//路径"E:\\IMImg\\2014\\10\\"要存在否则需要手动创建
test.fileCopy("E:\\IMImg\\2014\\9\\1414564179449xxx.jpg", "E:\\IMImg\\2014\\12\\111\\112\\113\\114\\aa.jpg");
}
}
try{
//最好不要太小
int maxCount = 1024;
long size = inChannel.size();
long position = 0;
while ( position < size )
{
position += inChannel.transferTo( position, maxCount, outChannel );
}
}
文件夹复制:
- 复制文件(夹)
- 文件(夹)复制、删除、重命名
- C#文件(夹)复制
- 文件复制(java)
- 复制文件(IO)
- 简易复制文件(IO)
- 文件复制(c语言)
- 常用的文件(夹)操作指令 复制 删除
- MFC文件(夹)创建、复制、删除及查找
- ubuntu 新建、移动、重命名、复制、删除文件(夹)
- Nodejs读写文件(复制文件)
- Python文件复制(txt文件)
- 文件复制
- 文件复制
- 文件复制
- 复制文件
- 文件复制
- 复制文件
- shell EOF注意点
- Java泛型总结(1)
- Vitual Router in The Cloud
- LintCode-LRU缓存策略
- socket长连接所用到的八大技术
- 复制文件(夹)
- 怎样修改项目jdk和web版本
- expect获取返回值
- Trie树 hdu4287 Intelligent IME
- LeetCode | Surrounded Regions
- lct (bzoj 2049 Cave 洞穴勘测)
- Java自带排序方法
- 大话Unix/Linux内核
- 10-0. 说反话 (20)