标签:文件 Java 自定义 批量 File IOException io flush new
import java.io.Closeable;
public class FileUtil {
/**
* 工具类关闭流
* 可变参数: ... 只能形参最后一个位置,处理方式与数组一致(可传递多个参数)
*/
public static void close(Closeable ... io){
for(Closeable temp:io){
try {
if (null != temp) {
temp.close();
}
} catch (Exception e) {
}
}
}
/**
* 使用泛型方法
*/
public static <T extends Closeable> void closeAll(T ... io){
for(Closeable temp:io){
try {
if (null != temp) {
temp.close();
}
} catch (Exception e) {
}
}
}
}
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Properties;
/**
1、建立联系 File对象 源头 目的地
2、选择流
文件输入流 InputStream FileInputStream
文件输出流 OutputStream FileOutputStream
3、操作 : 拷贝
byte[] flush =new byte[1024];
int len =0;
while(-1!=(len=输入流.read(flush))){
输出流.write(flush,0,len)
}
输出流.flush
4、释放资源 :关闭 两个流
* @author Administrator
*
*/
public class CopyFileDemo {
/**
* @param args
* @throws IOException
* @throws FileNotFoundException
*/
public static void main(String[] args) throws FileNotFoundException, IOException {
String src ="E:/xp/test";
String dest="e:/xp/test/4.jpg";
try {
copyFile(src,dest);
} catch (FileNotFoundException e) {
e.printStackTrace();
System.out.println("文件不存在");
} catch (IOException e) {
e.printStackTrace();
System.out.println("拷贝文件失败|关闭流失败");
}
}
/**
* 文件的拷贝
* @param 源文件路径
* @param 目录文件路径
* @throws FileNotFoundException,IOException
* @return
*/
public static void copyFile(String srcPath,String destPath)
throws FileNotFoundException,IOException {
//1、建立联系 源(存在且为文件) +目的地(文件可以不存在)
File src =new File(srcPath);
File dest =new File(destPath);
if(! src.isFile()){ //不是文件或者为null
System.out.println("只能拷贝文件");
throw new IOException("只能拷贝文件");
}
//2、选择流
InputStream is =new FileInputStream(src);
OutputStream os =new FileOutputStream(dest);
//3、文件拷贝 循环+读取+写出
byte[] flush =new byte[1024];
int len =0;
//读取
while(-1!=(len=is.read(flush))){
//写出
os.write(flush, 0, len);
}
os.flush(); //强制刷出
//关闭流
FileUtil.closeAll(os,is);
}
/**
* 1.7新特性 try --with --resource
* @param srcPath
* @param destPath
* @throws FileNotFoundException
* @throws IOException
*/
public static void copyFile2(String srcPath,String destPath)
throws FileNotFoundException,IOException {
//1、建立联系 源(存在且为文件) +目的地(文件可以不存在)
File src =new File(srcPath);
File dest =new File(destPath);
if(! src.isFile()){ //不是文件或者为null
System.out.println("只能拷贝文件");
throw new IOException("只能拷贝文件");
}
//2、选择流
try(
InputStream is =new
FileInputStream(src);
OutputStream os =new
FileOutputStream(dest);
){
//3、文件拷贝 循环+读取+写出
byte[] flush =new byte[1024];
int len =0;
//读取
while(-1!=(len=is.read(flush))){
//写出
os.write(flush, 0, len);
}
os.flush(); //强制刷出
}
}
}
标签:文件,Java,自定义,批量,File,IOException,io,flush,new 来源: https://blog.csdn.net/mengxianglong123/article/details/88350974
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。