Java file upload and copy functions

Source: Internet
Author: User
Tags create directory

Package com.sitech.message.controller.task;
Import java.io.file;//Introduction 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, etc.
public class Textcopyfileandmove {
public static void Filemove (string from, string to) Throws Exception {//move all files within the specified folder
try {
File dir = new file (from);
file[] files = dir.listfiles ();//places a file or folder into a file set
if (files = = null)//Determines whether the set of files is empty
return;
File Movedir = new file (to),//Create target directory
if (!movedir.exists ()) {///To determine if the target directory exists
Movedir.mkdirs ();//does not exist create
} br>for (int i = 0; i < files.length; i++) {//traversal file Set
if (Files[i].isdirectory ()) {//) if it is a folder or directory, recursively calls the Filemove method until the Recorded Files
Filemove (Files[i].getpath (), to + "\ \" + files[i].getname ()),//recursively move files
Files[i].delete ();//delete files in the original directory

File MoveFile = new file (Movedir.getpath () + "\ \"//Files directory into the moved directory
+ files[i].getname ());
if (movefile.exists ()) {///target folder exists, delete
Movefile.delete ();
}
Files[i].renameto (moveFile);//Move File
System.out.println (Files[i] + "mobile success");
}
} catch (Exception e) {
throw E;
}
}

The files under the Copy directory (excluding 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 it is a directory
Copyfiletodir (Topath, listfile (file));
}
}

Copy directories to the specified directory, and copy all files and subdirectories in directories and directories to the target directory
public static void Copydir (String topath, String frompath) {
File targetfile = new Topath;//Create Files
CreateFile (TargetFile, false);//Create Directory
File File = new (Frompath);//Create Files
if (Targetfile.isdirectory () && file.isdirectory ()) {//If incoming is a directory
Copyfiletodir (Targetfile.getabsolutepath () + "/" + File.getname (),
ListFile (file));//Copy files to the specified directory
}
}

//Copy a set of files to the specified directory. TARGETDIR is the destination directory, FilePath is the file path that needs to be copied
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 empty");
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 it is not a directory
System.out.println ("parameter error, the target path is not a directory! ");
return;
}
}
for (int i = 0; i < filepath.length; i++) {//Traversal file path to be copied
File File = new file (Filepath[i]);//Create Files
if (file.isdirectory ()) {//Determines whether the directory
Copyfiletodir (Todir + "/" + File.getname (), listfile (file));//Recursive call method gets the text under the directory
System.out.println ("Copy files" + file);
} else {
Copyfiletodir (todir, File, "");//Copy files to the specified directory
}
}
}

public static void Copyfiletodir (string todir, File file, string newName) {//Copy files to the 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);//Call method copy file
}

public static void CopyFile (file tofile, file FromFile) {//Copy files
if (tofile.exists ()) {///To determine if a file exists in the destination directory
System. Out.println ("File" + tofile.getabsolutepath () + "already exists, skip the file! ");
return;
} else {
CreateFile (ToFile, true);//Create File
}
System.out.println ("Copy file" + fromfile.getabsolutepath () + "to" br>+ Tofile.getabsolutepath ());
try {
InputStream is = new FileInputStream (fromfile);//create file input stream
FileOutputStream fos = new FileOutputStream (to file);//output stream
byte[] buffer = new byte[1024];//byte array
while (is.read (buffer)! =-1) {//writes the contents of the file to a file
Fos.write (b Uffer);
}
Is.close ();//input stream close
Fos.close ();//output stream close
} catch (FileNotFoundException e) {//Capture file no exception
E.printsta Cktrace ();
} catch (IOException e) {//catch exception
E.printstacktrace ();
}
}

public static string[] ListFile (file dir) {//Get files absolute path
String absolutpath = Dir.getabsolutepath ();//The path of the pass-through string is assigned to an incoming file
string[] paths = dir.list ();//array of file names
string[] files = new string[paths.length];//declaration string array, length is the number of incoming files
for (int i = 0; i < paths.length; i++) {//Traverse show file Absolute path
Files[i] = Absolutpath + "/" + paths[i];
}
return files;
}

public static void CreateFile (String path, Boolean isfile) {//create file or directory
CreateFile (path), isfile);//Call method to create a new file or directory
}

public static void CreateFile (file file, Boolean isfile) {//Create files
if (!file.exists ()) {//If the file does not exist
if (!file.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 a new file
} catch (IOException e) {
E.printstacktrace ();
}
} else {
File.mkdir ();//Create Directory
}
}
}
}

public static void Main (string[] args) {//Java program main entrance
String Frompath = "E:/createfile/shafei.log";//directory path
String Topath = "F:/createfile/shafei.log";//source Path
System.out.println ("1. Move file: From path" + Frompath + "move to Path" + Topath);
File Frompaths = new file ("F:/createfile/createfile/test.log");
File Tofiles = new file ("F:/createfile/shafei/test.log");
CopyFile (tofiles,frompaths);
try {
Filemove (Frompath, Topath);//Call method to implement file movement
} catch (Exception e) {
System.out.println ("Problem with moving files" + e.getmessage ());
}
System.out.println ("2. Copy directory" + topath + "file (not including this directory) to the specified directory" + Frompath
+ "will copy the past together with subdirectories. ");
Copyfilefromdir (Frompath, Topath);//Call method to implement directory replication
System.out.println ("3. Copy directory" + Frompath + "to the specified directory" + Topath
+ ", copy all files and subdirectories in directory and directory to target directory");
Copydir (Topath, Frompath);//Call Method implementation directory to copy all files and subdirectories under directory
}
}

Java file upload and copy functions

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.