--------Android Training , Java training , look forward to communicating with you! --------
Java folder replication because you do not know how many folders inside the folder, so use recursive method to traverse all the folders and files inside,
If it is a file, it is copied directly to the destination folder, if it is a folder, then the target place to create the corresponding folder with the same name, because the file has a variety of types,
such as videos, documents, pictures, etc. so the main purpose is to use a byte stream object instead of a character stream object.
Some of the classes used here are
1.File class,
An abstract representation of the file and directory path names. (That is, it can be either a file or a folder, a delimited string)
Note: Instances of the file class are immutable; that is, once created, the abstract path name represented by the file object will never change.
The commonly used members are:
The static string separator field, the system-related default name delimiter, is represented as a string for convenience. The delimiter in UNIX is "/", while the delimiter in Windows is "\ \"
The file (string pathname) constructor that creates a new file instance by converting the given pathname string to an abstract pathname.
Boolean exists ()
Tests whether the file or directory represented by this abstract path name exists.
String GetName ()
Returns the name of the file or directory represented by this abstract path name. (Just the name, including the extension, not including the path, such as: xxxx file, xxxx folder)
String GetPath ()
Converts this abstract path name to a pathname string. (A string that includes a full name and a full path)
Boolean isdirectory ()
Tests whether the file represented by this abstract path name is a directory.
Boolean isfile ()
Tests whether the file represented by this abstract path name is a standard file.
File[] Listfiles ()
Returns an abstract path to the an array group that represents the files in the directory represented by this abstract path name. (including all files and folders under this folder)
Boolean mkdir ()
Creates the directory specified by this abstract path name. (Create a folder under the specified directory, if the creation succeeds, returns True, requires a parent directory)
Boolean mkdirs ()
Creates the directory specified by this abstract pathname, including all required but nonexistent parent directories. (Create a folder under the specified directory, if it is successful, returns true if there is no parent directory, create parent directory and parent directory ...)
2.FileInputStream class
Gets the input bytes from a file in the file system. Which files are available depends on the host environment. Used to read raw byte streams such as data. To read a character stream, consider using FileReader.
Common members are:
FileInputStream (File file)
Create a FileInputStream by opening a connection to the actual file, which is specified through the file-object files in the filesystem.
FileInputStream (String name)
Create a FileInputStream by opening a connection to the actual file, which is specified by the pathname name in the file system. (That is, the full path of a file)
int read (byte[] b)
Reads a maximum of b.length bytes of data from this input stream into a byte array. Returns 1 if there is no more data because the end of the file has been reached.
void Close ()
Closes this file input stream and frees all system resources related to this stream.
3.FileOutputStream class
The file output stream is the output stream used to write data to file or FileDescriptor. Whether a file is available or can be created depends on the underlying platform. In particular, some platforms allow only one fileoutputstream (or other file to write to the object) to open a file for writing at a time. In this case, if the file involved is already open, the construction method in this class will fail. A stream used to write raw bytes such as data. To write a character stream, consider using FileWriter.
Common members are:
FileOutputStream (File file)
Creates a file output stream that writes data to the file that is represented by the specified Files object.
void Write (byte[] b, int off, int len)
Writes Len bytes from offset off in the specified byte array to this file output stream.
void Close ()
Closes this file output stream and frees all system resources related to this stream.
The simple example code is as follows:
//import java.io.*;classfilemove{ Public Static voidMain (string[] args) {Try{ //Pass in two folder paths, where the delimiter used in Windows is "\ \"CopyFile ("d:\\5555","d:\\1234"); } Catch(Exception ex) {} System. out. println ("Copy File is over!"); } //How to copy a folder, starting with two folders Public Static voidCopyFile (String source,string destin) throws Exception//throw all kinds of anomalies here, and not deal with them.{File path1=NewFile (source); File path2=NewFile (destin); //if the source directory does not exist, then you do not have to copy it. if(Path1.exists ()) {//Create destination folder, if the target directory does not exist, creates the target directory because no directory files are copied if(!path2.exists ()) {Path2.mkdirs (); } //get all the files and folders under the source directory//can want a little better, with the buffer class Bufferedinputstream and Bufferedoutputstream, to deal with large files will be betterFile[] Items=Path1.listfiles (); FileInputStream FIS=NULL; FileOutputStream Fos=NULL; //after getting all the files and folders, traverse processing, if it is a file, copy the file, if it is a folder, then recursively folder the files and folders below for(File item:items) {//If the file is not copied if(Item.isfile ()) {//two common constructors for input and output streams, where a field file.separator is used to read the file with the input stream and then write the file to the target location with the output stream to complete the copy functionfis=NewFileInputStream (item); FOS=NewFileOutputStream (path2+file.separator+item.getname ()); byte[] b=New byte[1024x768]; for(intI=0;(I=fis.read (b))!=-1;) {Fos.write (b,0, i); Fos.flush (); } //Close the stream shut down the resources Ah, what unusual handling of not write, make up for yourselfFos.close (); Fis.close (); } //if it is a folder, the recursive folder Else{CopyFile (Item.getpath (), path2+file.separator+item.getname ()); } } } Else{System. out. println ("Source Path:"+source+"is not exists!"); } }}//
The above code is as simple as possible, of course, if the actual application should be all the exception to catch up, and the way to close the resources are not standard, but in order to facilitate the idea of keeping up with the eyes, superfluous things will not be written.
And if you want to copy large files, you can add buffer classes to buffer, such as: Class Bufferedinputstream and Bufferedoutputstream should experience a good point.
Dark Horse programmer-java to learn the copy of the folder and traverse all files