文件的复制
package com.briup.day23;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Scanner;
/**
* 用字节流进行文件的原格式复制
* 可以对上传的文件目录名称限制
* 判断该目录是否只为文件夹
*
* @author 姚
* 1、运行程序
* 2、输入路径文件
* 3、复制文件完成
*/
public class FileTest {
public static void t1() throws Exception {
InputStream is;
OutputStream os;
try {
Scanner sc = new Scanner(System.in);
System.out.println("请输入路径");
String path = sc.nextLine();
if (path.contains("lol")) {
System.out.println("就不准你上传这个目录");
System.exit(-1);
}
File file = new File(path);
if (file.isDirectory()) {
System.out.println("输入的是目录");
System.exit(-1);
}
String[] format = path.split("\\.");
String lastformat = format[format.length -1];//切割获取字符串中最后的文件格式
is = new FileInputStream(file);
os = new FileOutputStream("E:\\" + System.currentTimeMillis() + "." + lastformat);
int a;
byte[] bytes = new byte[1024];
while((a = is.read(bytes)) != -1) {
os.write(bytes , 0 , a);
}
os.flush();
os.close();
is.close();
System.out.println("复制成功!!");
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
System.out.println("该路径无效");
e.printStackTrace();
}
}
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
t1();
}
}