我想实现一个文件复制的小例子,但是有一个问题。源路径可以判断exists(),存在的话就去检查目标路径。如果目标路径输入的是个目录,并且该路径下不存在此目录,就创建此目录,就直接把文件写过去,文件名截取源路径中的文件名。如果是一个多级目录的话,我就循环的创建目录,然后再写文件while(TargetPath.getParentFile()!=null)
{
TargetPath.getParentFile().mkdir();
}现在最麻烦的是isDirectory()和isFile()没有办法判断出目录路径是文件路径还是目录路径,求解
{
TargetPath.getParentFile().mkdir();
}现在最麻烦的是isDirectory()和isFile()没有办法判断出目录路径是文件路径还是目录路径,求解
-f --from source directory
-t --target target directory
-h --help show this message
-v --version show version info
类似这样通过参数来指定
import java.io.OutputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.FileInputStream;
public class CopyTest
{
public static void main(String[] args) throws Exception
{
if(args.length==2)
{
File OriginalPath=new File(args[0]);
File TargetPath=new File(args[1]);
if(OriginalPath.exists())
{
if(OriginalPath.isFile())
{
InputStream input=new FileInputStream(OriginalPath);
OutputStream output=new FileOutputStream(TargetPath);
int temp=0;
while((temp=input.read())!=-1)
{
output.write(temp);
}
}
}
else
{
System.out.println("您复制的源文件不存在!");
}
}
else
{
System.out.println("命令格式不正确\n\t例如:java CopyTest c:\\123.txt d:\\456.txt ");
}
}
}
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;/*//5
* 相对于SimpleCopyFileAndCreateItem 正常情况可能出现很多问题
* 1.源文件不存在
* 2.源文件不可读
* 3.目标文件已经存在
* 4.目标路径不存在
* 5.目标路径不可写
* */public class CopyFile {
public static void main(String[] args) {
new CopyFile().copy("a.txt","b.txt",1);
}
/*
* type 0 不覆盖 1 覆盖
* */
@SuppressWarnings("finally")
private int copy(String source_name, String dest_name, int type) {
// TODO Auto-generated method stub
File source_file = new File(source_name);
File dest_flie = new File(dest_name);
FileInputStream source = null;
FileOutputStream destination = null;
byte[] buffer;
int bytes_read;
int result = 0;
try {
// 1 源文件不存在
if(! source_file.exists() || !source_file.isFile()){
throw new RuntimeException("FileCopy : no such source file :"+source_name);
}
// 2 源文件不可读
if(! source_file.canRead()){
throw new RuntimeException("FileCopy : source file is unreadable :"+source_name);
}
//3 目标路径存在
if (dest_flie.exists()) {
if(dest_flie.isFile()){
if(type==1){ // 覆盖
dest_flie.delete(); // 有问题 文件都删掉了!!怎么复制进去!!!
//但是 注意 destination = new FileOutputStream(dest_flie); 如果文件不存在,应该会在产生一个空文件!
result = 1;
}else { //不覆盖
result = 2;
return result;
}
}else {// 目标是目录而不是文件
throw new RuntimeException("FileCopy :destination "+"is not a file: "+ dest_name);
}
}else {
//4 目标路径不存在
//但是 注意 destination = new FileOutputStream(dest_flie); 如果文件不存在,应该会在产生一个空文件!所以路径不存在并不重要!
// 所以就要看看parent 路径是否存在,好在产生一个新的制定空的文件
File parentdir = new File(dest_flie.getParent());
if(! parentdir.exists()){
throw new RuntimeException("FileCopy : destination directory doesn't exist : "+ dest_name);
}
//5 目标路径不可写
if(! parentdir.canWrite()){
throw new RuntimeException("FileCopy : destination directory is unwriteable : "+dest_name);
}
}
// 复制文件
source =new FileInputStream(source_file);
destination = new FileOutputStream(dest_flie);
buffer = new byte[1024];
while(true){
bytes_read = source.read(buffer);
if(bytes_read==-1 ){
break;
}
destination.write(buffer, 0, bytes_read);
}
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}finally {
if(source != null){
try {
source.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (destination !=null) {
try {
destination.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return result;
}
}
}
源是文件则目标也表示文件,
这样会比较合理。(因为其实传入的仅仅是命令行参数的话,并不能确定目标到底是目录还是文件)。
如果以上建议不符合需求的话,应该考虑如8楼所说,在参数中间夹杂地送一些flag表示目标到底是目录还是文件。
我记得API有个函数可以直接创建所有父路径 楼主等下我找找
创建此抽象路径名指定的目录,包括创建必需但不存在的父目录。 这样甚至不用判断exists 自己写的实例 楼主参考一下
fc = new File(newPath ? path : "Error/" + path + new SimpleDateFormat("yyyy-MM-dd").format(new Date()) + ".txt");
fc.getParentFile().mkdirs(); //创建此抽象路径名指定的目录,包括创建必需但不存在的父目录。
fc.createNewFile();
import java.io.File;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
public class CreateDirectory
{
public static void main(String[] args) throws Exception
{
Copy(args);
}
public static void Copy(String args[]) throws Exception
{
File SourcePath=null;
File TargetPath=null;
if(args.length==2)
{
SourcePath=new File(args[0]);
TargetPath=new File(args[1]);
if(SourcePath.getParentFile()==null)
{
System.out.println("暂不支持整个分区的文件拷贝!");
}
else if(SourcePath.exists())
{
if(TargetPath.getName().indexOf(".")==-1)
{
TargetPath.mkdirs();
File tempFile=new File(TargetPath.getPath()+File.separator+SourcePath.getName());
readWriteFile(SourcePath,tempFile);
}
else
{
String s=TargetPath.getParent();
(new File(s)).mkdirs();
readWriteFile(SourcePath,TargetPath);
}
}
else
{
System.out.println("源文件不存在!");
}
}
else
{
System.out.println("您输入命令格式不正确!\n\t例如:java CreateDirectory c:\\123.txt d:\\");
}
}
public static void readWriteFile(File sourceFile,File targeFile) throws Exception
{
InputStream input=new FileInputStream(sourceFile);
OutputStream output=new FileOutputStream(targeFile);
int temp=0;
while((temp=input.read())!=-1)
{
output.write(temp);
}
input.close();
output.close();
}
}