Java implementation of File replication, mobile

Source: Internet
Author: User
Java implementation of File replication, mobile package com.file;
Import java.io.file;//introduced class import Java.io.FileInputStream;
Import java.io.FileNotFoundException;
Import Java.io.FileOutputStream;
Import java.io.IOException;

Import Java.io.InputStream; Implement simple processing of files, copy and move files, directories, public class Textcopyfileandmove {public static void Filemove (string from, String to) throws exc
			eption {//move all files in the specified folder try {file Dir = new file (from);
			file[] files = dir.listfiles ()//the file or folder into the file set if (Files = null)//To determine whether the file set is null return; File Movedir = new file (to);//Create target directory if (!movedir.exists ()) {//To determine whether the target directory exists movedir.mkdirs ();//does not exist create} for (int i = 0; i < files.length; i++) {//traverse the file Set if (Files[i].isdirectory ()) {//If it is a folder or directory, recursively call the Filemove method until you get the file Filemove in the directory (Files[i].getpath (), to + "\" + files[i].getname ());//Move the file recursively Files[i].delete (),//delete the file in the original directory, and file MoveFile = new file (movedir.getpat
				H () + "\ \"//Put the file directory into the moved directory + files[i].getname ()); if (movefile.exists ()) {//destination filefolder exists, delete Movefile.delete ();
			} files[i].renameto (MoveFile);//Move File System.out.println (Files[i] + "mobile success");
		The catch (Exception e) {throw e;
	The files (excluding this directory) in the directory are copied to the specified directory, along with subdirectories.
		public static void Copyfilefromdir (String topath, String frompath) {File File = new file (Frompath); CreateFile (Topath, False);/true: Create file false Create directory if (File.isdirectory ()) {//If directory Copyfiletodir (Topath, ListFile (fil
		e)); }///copy directory to specified directory, copy directories and directories and subdirectories to the destination directory public static void Copydir (String topath, String frompath) {file Targetfi Le = new file (topath);//Create Files CreateFile (TargetFile, false);//Create the directory file = new file (frompath);//Create Files if (targe Tfile.isdirectory () && file.isdirectory ()) {//If incoming is directory Copyfiletodir (Targetfile.getabsolutepath () + "/" + file. GetName (), listfile (file);//Copy files to specified directory}///Copy a set of files to the specified directory. TARGETDIR is the target directory, FilePath is the file path that needs to be replicated. public static void Copyfiletodir (String todir, string[] filePath) {if (Todir= = NULL | |
			"". Equals (Todir)) {//directory path is empty System.out.println ("parameter error, destination path cannot be null");
		Return
		} File TargetFile = new file (Todir); if (!targetfile.exists ()) {//If the specified directory does not exist Targetfile.mkdir ()//new Directory} else {if (!targetfile.isdirectory ()) {//If not is the directory System.out.println ("parameter error, the target path does not point to a directory.")
				");
			Return for (int i = 0; i < filepath.length; i++) {//traverse the file path you want to copy files = new file (Filepath[i]);//create file if (f Ile.isdirectory ()) {//To determine whether the directory Copyfiletodir (Todir + "/" + File.getname (), listfile (file));//Recursive call method gets the file in the directory Syste
			M.out.println ("Copy files" + file);  else {copyfiletodir (todir, File, "");//Copy file to specified directory}}} public static void Copyfiletodir (String todir, file
		File, String newName) {//Copy files to specified directory string newFile = ""; if (newName!= null &&! "".
		Equals (NewName)) {newFile = Todir + "/" + newName;
		else {newFile = Todir + "/" + file.getname ();
		} File Tfile = new file (newFile); CopyFile (tfile, file);//Invoke method copy textPieces} public static void CopyFile (file tofile, file FromFile) {//Copy file if (tofile.exists ()) {//To determine whether a file in the target directory exists System . OUT.PRINTLN ("file" + Tofile.getabsolutepath () +) already exists, skipping the file.
			");
		Return else {CreateFile (tofile, true);//Create File} System.out.println ("Copy file" + fromfile.getabsolutepath () + "to" + Tofi
		Le.getabsolutepath ()); try {InputStream is = new FileInputStream (fromfile);//create file input stream FileOutputStream fos = new FileOutputStream (tofile);
			File output stream byte[] buffer = new byte[1024];//byte array while (Is.read (buffer)!=-1) {//write file contents to file Fos.write (buffer); Is.close ()//input stream close fos.close ()//output stream shutdown} catch (FileNotFoundException e) {//capture file does not have an exception e.printstacktrace
		();
		catch (IOException e) {//catch exception e.printstacktrace (); } public static string[] ListFile (file dir) {//Get files absolute path string absolutpath = Dir.getabsolutepath ();//Acoustic string assignment is the route pass text The path string[] paths = dir.list ();//file name array string[] files = new string[paths.length];//Declare an array of strings, in lengthNumber of incoming files for (int i = 0; i < paths.length i++) {//traverse the display file absolute path files[i] = Absolutpath + "/" + paths[i];
	return to files; public static void CreateFile (String path, Boolean isfile) {//Create file or directory CreateFile (new file (path), isfile);//Call method Create new File or directory} public static void CreateFile (file file, Boolean isfile) {//Create file if (!file.exists ()) {//If file does not exist if (!fil
			E.getparentfile (). exists ()) {//If the file parent directory does not exist CreateFile (File.getparentfile (), false);
						else {//exists file parent directory if (isfile) {///Create file try {file.createnewfile ();//Create New file} catch (IOException e) {
					E.printstacktrace (); } else {File.mkdir ()///Create Directory}}} public static void Main (string[] args) {//Java program main entrance Strin G Frompath = "e:/createfile";//directory path String topath = "f:/createfile";//source Path System.out.println ("1. Moving files: From path" + Frompat
		H + "Move to Path" + Topath); try {filemove (Frompath, Topath);//Call method implementation file Move} catch (Exception e) {System.out.println ("Move file problem"+ e.getmessage ()); System.out.println ("2. Copy the directory" + topath + "file (excluding this directory) to the specified directory" + Frompath + ", will be copied together with the subdirectory of the past.
		"); Copyfilefromdir (Frompath, Topath);//Calling method implements directory replication System.out.println ("3. Copy directory + Frompath +" to specified directory "+ Topath +", the Directory
		and the files and subdirectories under the directory are all copied to the target directory "); Copydir (Topath, Frompath);//Invoke method implementation directory to copy all of the files and subdirectories under directory}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.