Java7中使用try-with-resource语法 实现两种复制文件方法
2016-03-03 19:01
639 查看
在一个工具类中实现两种文件复制方法:
package example.gloryzyf;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
public class FileUtil {
//工具类中提供的一般都是静态方法 把FileUtil构造私有化 不允许创建工具类的实例
private FileUtil(){
}
//普通IO方式
public static void copyFile(String res,String dest) throws IOException{
FileInputStream fis=null;
FileOutputStream fos=null;
try{
fis=new FileInputStream(res);
fos=new FileOutputStream(dest);
byte []buffer=new byte[1024];
int bytesToRead;
while((bytesToRead=fis.read(buffer))!=-1){
fos.write(buffer,0,bytesToRead);
}
}finally{
if(fis!=null)
fis.close();
if(fos!=null)
fos.close();
}
}
//使用try-with-resource语法 实现普通IO方式
public static void copyTWRFile(String res,String dest) throws IOException{
try(FileInputStream fis=new FileInputStream(res)){
try(FileOutputStream fos=new FileOutputStream(dest)){
byte []buffer=new byte[1024];
int bytesToRead;
while((bytesToRead=fis.read(buffer))!=-1){
fos.write(buffer, 0, bytesToRead);
}
}
}
}
//使用try-with-resource语法 实现NIO方式
public static void copyNIOFile(String res,String dest) throws IOException{
try(FileInputStream fis=new FileInputStream(res)){
try(FileOutputStream fos=new FileOutputStream(dest)){
FileChannel inChannel=fis.getChannel();
FileChannel outChannel=fos.getChannel();
ByteBuffer buffer=ByteBuffer.allocate(1024);
while(inChannel.read(buffer)!=-1){
buffer.flip();
outChannel.write(buffer);
buffer.clear();
}
}
}
}
}
package example.gloryzyf;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
public class FileUtil {
//工具类中提供的一般都是静态方法 把FileUtil构造私有化 不允许创建工具类的实例
private FileUtil(){
}
//普通IO方式
public static void copyFile(String res,String dest) throws IOException{
FileInputStream fis=null;
FileOutputStream fos=null;
try{
fis=new FileInputStream(res);
fos=new FileOutputStream(dest);
byte []buffer=new byte[1024];
int bytesToRead;
while((bytesToRead=fis.read(buffer))!=-1){
fos.write(buffer,0,bytesToRead);
}
}finally{
if(fis!=null)
fis.close();
if(fos!=null)
fos.close();
}
}
//使用try-with-resource语法 实现普通IO方式
public static void copyTWRFile(String res,String dest) throws IOException{
try(FileInputStream fis=new FileInputStream(res)){
try(FileOutputStream fos=new FileOutputStream(dest)){
byte []buffer=new byte[1024];
int bytesToRead;
while((bytesToRead=fis.read(buffer))!=-1){
fos.write(buffer, 0, bytesToRead);
}
}
}
}
//使用try-with-resource语法 实现NIO方式
public static void copyNIOFile(String res,String dest) throws IOException{
try(FileInputStream fis=new FileInputStream(res)){
try(FileOutputStream fos=new FileOutputStream(dest)){
FileChannel inChannel=fis.getChannel();
FileChannel outChannel=fos.getChannel();
ByteBuffer buffer=ByteBuffer.allocate(1024);
while(inChannel.read(buffer)!=-1){
buffer.flip();
outChannel.write(buffer);
buffer.clear();
}
}
}
}
}
相关文章推荐
- 如何让eclipse进行智能提示?
- Leetcode:21. Merge Two Sorted Lists(JAVA)
- Java内存分配
- TIJ-第三章:操作符
- [JAVA · 初级]:5.以我微观看变量
- java基础之一java环境的搭建
- The first day of learning java
- java回顾之错误处理机制
- 算法代码实现之二分法查找,Java实现
- ubantu14.0.1安装jdk1.7.1
- eclipse安装jetty插件
- TIJ-第一章:对象介绍
- JAVA设计模式之单例模式
- 有关JavaBean的基础知识
- java.io.IOException: Could not locate executable null\bin\winutils.exe in the Hadoop binaries.
- Java 技术体系图
- Java 抽象工厂模式
- spring和jsp的数据传递。。。
- IntelliJ使用指南—— 导入Eclipse的Web项目
- javaWeb2.1 XML解析方式之DOM解析