现在要求是从现有的文件中,通过指定FileList将现有文件拷贝出来。
如:
c:\study\1.txt
c:\study\2.txt
c:\study\abc\3.txt
到
d:\study\1.txt
d:\study\2.txt
d:\study\abc\3.txt c:中可能还有很多别的文件!
如:
c:\study\1.txt
c:\study\2.txt
c:\study\abc\3.txt
到
d:\study\1.txt
d:\study\2.txt
d:\study\abc\3.txt c:中可能还有很多别的文件!
解决方案 »
- 根据猫扑同名漫画写的《自助投胎机》,欢迎下载自助投胎
- dxf 怎么显示在Web中 要求后台是java?
- netbeans怎用设置快捷键
- 如何设置session过期自动返回登录页面??
- 应该怎么样学习JAVA?
- EJB Remote使用完之后,没有被清除,会引发什么问题?
- -=》○● 求解 Java 基本型别存在之理 ●○《=-
- java可否调用win32API呢
- String s="星期日 2005-02-04 08:43:56" 怎么处理能得到"2005-02-04 08:43:56"?
- 我的app程序打包成了jar.如何让它在后台运行?
- 關於Graphics g = getGraphics();(新人第一次發帖,請不吝賜教:)
- Applet是不是无法对文件进行读写?
f.isFile(); ===> bool v;//这里如果是dir的话,要递归一下。copyFile//要自己实现,Java,Python,Ruby都没有提供copy文件的API,(Ruby的copy file还是字符流读取实现的,都是没有真正系统级别的CopyFile的)剩下的就简单了。
copyFile用In和Out流。
public static void main(String[] args){
if(args.length != 2){
System.err.println("Usage:java FileCopy <source> <destination>");
}else{
try{
copy(args[0],args[1]);
}
catch(IOException e){
System.err.println(e.getMessage());
}
}
}
private static void copy(String from_name,String to_name) throws IOException{
File from_file = new File(from_name);
File to_file = new File(to_name);
if(!from_file.exists())
abort("no such source file:" + from_name);
if(!from_file.isFile())
abort("can't copy directory:"+ from_name);
if(!from_file.canRead())
abort("source file is unreadable:"+ from_name);
if(to_file.isDirectory())
to_file = new File(to_file,from_file.getName());
if(to_file.exists()){
if(!to_file.canWrite()){
abort("destination file is unwirteable"+to_name);
}
System.out.println("Overwirte existing file"+to_file.getName()+"?(Y/N):");
System.out.flush();
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
String response = in.readLine();
if(!response.equals("Y") && !response.equals("y"))
abort("existing file was not overwritten");
}
else{
String parent = to_file.getParent();
if(parent==null){
parent = System.getProperty("user.dir");
}
File dir = new File(parent);
if (!dir.exists( ))
abort("destination directory doesn't exist: "+parent);
if (dir.isFile( ))
abort("destination is not a directory: " + parent);
if (!dir.canWrite( ))
abort("destination directory is unwriteable: " + parent);
}
FileInputStream from = null;
FileOutputStream to = null;
try{
from = new FileInputStream(from_file);
to = new FileOutputStream(to_file);
byte[] buffer = new byte[4096];
int bytes_read;
while((bytes_read = from.read(buffer))!=-1)
to.write(buffer,0,bytes_read);
}
finally{
if(from != null) try{from.close();}catch(IOException e){;}
if(to != null) try{to.close();}catch(IOException e){;}
}
}
private static void abort(String msg) throws IOException{
throw new IOException(msg);
}
}
InputStream inStream = new FileInputStream(from);
FileOutputStream foStream = new FileOutputStream(to);
BufferedOutputStream boStream = new BufferedOutputStream(foStream);
byte[] buffer = new byte[1440];
while ((byteRead = inStream.read(buffer)) != -1) {
boStream.write(buffer, 0, byteRead);
boStream.flush();
}
inStream.close();
foStream.close();
} public static void main(String[] args) throws IOException {
//get changeList
File fileList = new File("d://FileList.txt"); InputStream in = new FileInputStream(fileList);
DataInputStream dataInput = new DataInputStream(in); String path;
while ((path = dataInput.readLine()) != null) { if (path.length() > 0) {
if (path.lastIndexOf(":") == -1) {
String parentPath = System.getProperty("user.dir");
path = parentPath + "\\" + path;
}
File src = new File(path);
String CanonicalPath = src.getCanonicalPath();
String newPath = "d"
+ CanonicalPath
.substring(1, CanonicalPath.length());
File dst = new File(newPath);
if (dst.exists()) {
System.out.println(newPath + " is Alreadly exist");
continue;
}
String parent = dst.getParent();
File pathFile = new File(parent);
pathFile.mkdirs();
copyFile(src, dst);
}
}
}}