Package copy file;
Import Java.io.File;
Import Java.io.FileFilter;
Import Java.io.FileInputStream;
Import java.io.FileNotFoundException;
Import Java.io.FileOutputStream;
Import Java.io.FilenameFilter;
Import java.io.IOException;
Import Java.io.InputStream;
Import Java.io.OutputStream;
public class CopyFile {
public static void Main (string[] args) {
String Path = "D:/file";
Execute e = new execute ();
E.exe (path);
}
}
Executive Section
Class execute{
public void exe (String s) {
File F = new file (s);
Find a destination file
F.list (New FilenameFilter () {Filter files
Public Boolean Accept (File dir, String name) {
Find the file you are looking for//determine if it is a. txt end file
if (Name.endswith (". txt")) {
Gets the path that will be \ converted to/
String path = Dir.getpath (). replace ("\ \", "/");
SYSTEM.OUT.PRINTLN (path);
Copy section
File F1 = new file (path+ "/" +name);
InputStream is = null;
OutputStream OS = null;
try {
is = new FileInputStream (F1);
OS = new FileOutputStream ("f:/mm/" +name);//Writes the read-in file to this file
byte [] b = new byte[1024];
int len = 0;
while ((Len=is.read (b))!=-1) {
Os.write (b);
}
} catch (FileNotFoundException e) {
TODO auto-generated Catch block
E.printstacktrace ();
} catch (IOException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}finally{
try {
Close the stream
Is.close (); Os.close ();
} catch (IOException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
}
return true;
}
return false;
}
});
Determine if it is a folder and if it is a file enter the file
F.listfiles (New FileFilter () {
Public Boolean accept (File pathname) {
if (Pathname.isdirectory ()) {
EXE (Pathname.getpath (). replace ("\ \", "/"));
return true;
}
return false;
}
});
}
}
Using IO streams in Java to copy files from a folder